ming преди 4 години
родител
ревизия
a647bbd9fe
променени са 1 файла, в които са добавени 31 реда и са изтрити 21 реда
  1. 31 21
      api/main.py

+ 31 - 21
api/main.py

@@ -157,7 +157,7 @@ async def make_video_slide(request: Request, response: Response, Authorize: Auth
 
 @app.post('/user_profile', response_class=HTMLResponse)
 async def user_profile(token: str = Depends(oauth2_scheme)):
-     
+    db_check()
     user_id = get_user_id(token)
     user_obj = first(db.query('SELECT * FROM users where id ="'+str(user_id)+'"'))
 
@@ -181,7 +181,7 @@ async def user_profile(token: str = Depends(oauth2_scheme)):
 
 @app.post('/edit_profile')
 async def edit_profile(userModel : util.models.UserProfile ,token: str = Depends(oauth2_scheme)):
-     
+    db_check()
     print(token)
     user_id = get_user_id(token)
     print(user_id)
@@ -197,7 +197,7 @@ async def get_login_and_register_page(request: Request):
 
 @app.post("/login")
 async def login_for_access_token(request: Request, form_data: OAuth2PasswordRequestForm = Depends(), Authorize: AuthJWT = Depends()):
-     
+    db_check()
     user = authenticate_user(form_data.username, form_data.password)
     if not user:
         raise HTTPException(
@@ -243,7 +243,7 @@ async def access_token(form_data: OAuth2PasswordRequestForm = Depends(), Authori
 #前後端分離完全實現後拔除
 @app.post("/register_old")
 async def register_old(request: Request):
-     
+    db_check()
     user = util.models.User(**await request.form())
     user_obj = first(db.query('SELECT * FROM users where username ="'+str(user.username)+'"'))
     if user_obj == None:
@@ -262,7 +262,7 @@ async def register_old(request: Request):
 
 @app.post("/register")
 async def register(request: util.models.register_req):
-     
+    db_check()
     user_obj = first(db.query('SELECT * FROM users where username ="'+str(request.username)+'"'))
     
     if user_obj == None:
@@ -307,7 +307,7 @@ async def get_role(token: str = Depends(oauth2_scheme)):
 
 @app.get('/verify_email')
 async def verify_email(code):
-     
+    db_check()
     veri_obj = first(db.query('SELECT * FROM register_veri_code where code ="'+str(code)+'"'))
     if veri_obj != None:
         db['register_veri_code'].delete(code=code)
@@ -323,6 +323,7 @@ async def reset_pwd_page():
 
 @app.get('/send_reset_pwd')
 async def send_reset_pwd(user_id,email):
+    db_check()
     code = str(time.time()).replace('.','')
      
     db['reset_pw_code'].insert({'code':code,'user_id':user_id,'email':email})
@@ -344,7 +345,7 @@ async def send_reset_pwd(user_id,email):
 
 @app.post('/reset_pwd')
 async def reset_password(req :util.models.reset_pwd):
-     
+    db_check()
     print(req.code)
     veri_obj = next(iter(db.query('SELECT * FROM reset_pw_code where code ="'+str(req.code)+'"')))
     print(veri_obj['user_id'])
@@ -453,6 +454,7 @@ async def make_anchor_video_gSlide(req:util.models.gSlide_req,token: str = Depen
 
 @app.post("/make_anchor_video_long")
 async def make_anchor_video_long(req:util.models.request,token: str = Depends(oauth2_scheme)):
+    db_check()
     left_tag = [m.start() for m in re.finditer('{', req.text_content[0])]
     if len(req.image_urls) != len(left_tag):
         return {'msg':{'eng':'number of subtitles and images(videos) should be the same','zh':'副標題數量、圖片(影片)數量以及台詞數量必須一致'}}
@@ -492,6 +494,7 @@ async def make_anchor_video_long(req:util.models.request,token: str = Depends(oa
 
 @app.post("/make_anchor_video")
 async def make_anchor_video(req:util.models.request,token: str = Depends(oauth2_scheme)):
+    db_check()
     if len(req.image_urls) != len(req.text_content):
         return {'msg':{'eng':'number of subtitles and images(videos) should be the same','zh':'副標題數量、圖片(影片)數量以及台詞數量必須一致'}}
     for idx in range(len(req.image_urls)):
@@ -528,6 +531,7 @@ async def make_anchor_video(req:util.models.request,token: str = Depends(oauth2_
 
 @app.post("/make_anchor_video_eng")
 async def make_anchor_video_eng(req:util.models.request_eng,token: str = Depends(oauth2_scheme)):
+    db_check()
     if len(req.image_urls) != len(req.sub_titles) or len(req.sub_titles) != len(req.text_content):
         return {'msg':{'eng':'number of subtitles and images(videos) should be the same','zh':'副標題數量、圖片(影片)數量以及台詞數量必須一致'}}
     for idx in range(len(req.image_urls)):
@@ -556,7 +560,7 @@ async def make_anchor_video_eng(req:util.models.request_eng,token: str = Depends
 
 @app.post("/save_draft")
 async def save_draft(req:util.models.video_draft,token: str = Depends(oauth2_scheme)):
-     
+    db_check()
     user_id = get_user_id(token)
     
     txt_content_seperate_by_dot = ''
@@ -582,7 +586,7 @@ async def save_draft(req:util.models.video_draft,token: str = Depends(oauth2_sch
 
 @app.post('/draft_list')
 async def draft_list(token: str = Depends(oauth2_scheme)):
-     
+    db_check()
     user_id = get_user_id(token)
     statement = 'SELECT * FROM draft WHERE user_id='+str(user_id)+' ORDER BY time_stamp DESC LIMIT 50'
     logs = []
@@ -593,7 +597,7 @@ async def draft_list(token: str = Depends(oauth2_scheme)):
 
 @app.post('/del_draft')
 async def del_draft(id_obj:util.models.id_obj,token: str = Depends(oauth2_scheme)):
-     
+    db_check()
     user_id = get_user_id(token)
     statement = 'SELECT * FROM draft WHERE user_id="'+str(user_id)+'" and id ="'+str(id_obj.id)+'"'
     if first(db.query(statement)) is not None:
@@ -607,6 +611,7 @@ async def del_draft(id_obj:util.models.id_obj,token: str = Depends(oauth2_scheme
 
 @app.get("/history_input_old")
 async def history_input_old(request: Request, Authorize: AuthJWT = Depends()):
+    db_check()
     Authorize.jwt_required()
     current_user = Authorize.get_jwt_subject()
 
@@ -622,7 +627,7 @@ async def history_input_old(request: Request, Authorize: AuthJWT = Depends()):
 
 @app.post("/history_input")
 async def history_input(token: str = Depends(oauth2_scheme)):
-     
+    db_check()
     user_id = get_user_id(token)
     user_obj = first(db.query('SELECT * FROM users where id ="'+str(user_id)+'"'))
     statement = 'SELECT * FROM history_input WHERE user_id="'+str(user_id)+'" ORDER BY timestamp DESC LIMIT 50'
@@ -646,7 +651,7 @@ def authjwt_exception_handler(request: Request, exc: AuthJWTException):
     )
 
 def get_user_id(token):
-     
+    db_check()
     credentials_exception = HTTPException(
         status_code=status.HTTP_401_UNAUTHORIZED,
         detail="Could not validate credentials",
@@ -667,7 +672,7 @@ def get_user_id(token):
     return user_id
 
 def check_user_exists(username):
-     
+    db_check()
     if int(next(iter(db.query('SELECT COUNT(*) FROM AI_anchor.users WHERE username = "'+username+'"')))['COUNT(*)']) > 0:
         return True
     else:
@@ -683,7 +688,7 @@ def get_user(username: str):
     return user
     
 def user_register(user):
-     
+    db_check()
     table = db['users']
     user.password = get_password_hash(user.password)
     id = table.insert(dict(user))
@@ -694,7 +699,7 @@ def get_password_hash(password):
 def verify_password(plain_password, hashed_password):
     return pwd_context.verify(plain_password, hashed_password)
 def authenticate_user(username: str, password: str):
-     
+    db_check()
     if not check_user_exists(username):  # if user don't exist
         return False
     user_dict = next(iter(db.query('SELECT * FROM AI_anchor.users where username ="'+username+'"')))
@@ -712,7 +717,7 @@ def create_access_token(data: dict, expires_delta):
     return encoded_jwt
 
 def save_history(req,name_hash,user_id):
-     
+    db_check()
     log_table = db['history_input']
     txt_content_seperate_by_dot = ''
     for txt in req.text_content:
@@ -743,7 +748,7 @@ def notify_group(msg):
 
 
 def gen_video_long_queue(name_hash,name,text_content, image_urls,avatar,multiLang,video_id,user_id):
-     
+    db_check()
     time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
     txt_content_seperate_by_dot = ''
     for txt in text_content:
@@ -801,7 +806,7 @@ def gen_video_long_queue(name_hash,name,text_content, image_urls,avatar,multiLan
         db['video_queue'].delete(id=top1['id'])
         db.query('UPDATE video_queue_status SET status = 0')
 def gen_video_queue(name_hash,name,text_content, image_urls,avatar,multiLang,video_id,user_id):
-     
+    db_check()
     time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
     txt_content_seperate_by_dot = ''
     for txt in text_content:
@@ -860,7 +865,7 @@ def gen_video_queue(name_hash,name,text_content, image_urls,avatar,multiLang,vid
         db.query('UPDATE video_queue_status SET status = 0')
 
 def gen_video_queue_eng(name_hash,name,text_content, image_urls,sub_titles,avatar,video_id):
-     
+    db_check()
     time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
     txt_content_seperate_by_dot = ''
     for txt in text_content:
@@ -899,7 +904,7 @@ def gen_video_queue_eng(name_hash,name,text_content, image_urls,sub_titles,avata
         db.query('UPDATE video_queue_status SET status = 0')
 
 def gen_avatar(name_hash, imgurl):
-     
+    db_check()
     db['avatar_queue'].insert({'name_hash':name_hash,'imgurl':imgurl})
     while True:
         statement = 'SELECT * FROM avatar_service_status'#only one row in this table, which is the id 1 one
@@ -955,7 +960,12 @@ async def make_voice(in_text:text_in):
     x = threading.Thread(target=call_voice, args=(in_text.text,))
     x.start()
     
-        
+def db_check():
+    try:
+        db.tables()
+    except:
+        db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
+