|
@@ -12,6 +12,7 @@ from datetime import datetime
|
|
|
from PIL import Image
|
|
|
import io
|
|
|
from typing import List
|
|
|
+import threading
|
|
|
|
|
|
app = FastAPI()
|
|
|
origins = [
|
|
@@ -128,7 +129,7 @@ async def get_courses(tags:List[int]):
|
|
|
statement = 'SELECT * FROM course_tag '\
|
|
|
'INNER JOIN course on course_tag.course_id=course.id '\
|
|
|
'INNER JOIN tag on course_tag.tag_id = tag.id '\
|
|
|
- 'WHERE tag.id IN'+tag_str
|
|
|
+ 'WHERE tag.id IN'+tag_str+' ORDER BY clicks DESC'
|
|
|
|
|
|
for row in db.query(statement):
|
|
|
courses.append({'id':row['course_id'],'title':row['title'],'url':row['url']
|
|
@@ -136,7 +137,7 @@ async def get_courses(tags:List[int]):
|
|
|
,'cover_img':'https://'+serverADDR + row['cover_img'],'hours':row['hours'],'units':row['units']})
|
|
|
return courses
|
|
|
|
|
|
-@app.post("/courses")
|
|
|
+@app.post("/create_courses")
|
|
|
async def create_courses(tags: list,course:Course):
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/openTalk?charset=utf8mb4')
|
|
|
#add to course table
|
|
@@ -147,6 +148,8 @@ async def create_courses(tags: list,course:Course):
|
|
|
course_tag_table = db['course_tag']
|
|
|
for tag in tags:
|
|
|
course_tag_table.insert({'course_id':course.id,'tag_id':tag})
|
|
|
+ x = threading.Thread(target=to_sheet)
|
|
|
+ x.start()
|
|
|
return {'msg':'新增成功'}
|
|
|
|
|
|
@app.get("/del_course/{course_id}")
|
|
@@ -173,3 +176,26 @@ async def create_upload_file(file: UploadFile = File(...)):
|
|
|
return {"msg": serverADDR+cover_img_dir+img_name+'.jpg'}
|
|
|
|
|
|
|
|
|
+async def to_sheet():
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/openTalk?charset=utf8mb4')
|
|
|
+ statement = 'SELECT * FROM course ORDER BY clicks DESC'
|
|
|
+
|
|
|
+ df = pd.DataFrame()
|
|
|
+ for row in db.query(statement):
|
|
|
+ date_format = "%Y-%M-%d %H:%M:%S"
|
|
|
+
|
|
|
+ df = df.append({'serial id':row['id'],'標題':['Title'],'介紹':row['profile'],'url':row['url']
|
|
|
+ ,'圖片':row['cover_img'],'價錢':row['price'],"折扣後價錢":row['price_discount'],'時數':row['hours']
|
|
|
+ ,'單元數':row['units']}, ignore_index=True)
|
|
|
+ save_sheet(df,'deco_request','sheet1')
|
|
|
+
|
|
|
+def save_sheet(df,filename,tabname,startpos='A1'):
|
|
|
+ scope = ['https://spreadsheets.google.com/feeds',
|
|
|
+ 'https://www.googleapis.com/auth/drive']
|
|
|
+ credentials = ServiceAccountCredentials.from_json_keyfile_name('spread2.json', scope)
|
|
|
+
|
|
|
+ gc = gspread.authorize(credentials)
|
|
|
+ spread = Spread(filename,creds=credentials)
|
|
|
+ spread.df_to_sheet(df, index=False, sheet=tabname, start=startpos, replace=False)
|
|
|
+
|
|
|
+
|