|
@@ -47,7 +47,7 @@ import util.user
|
|
|
pymysql.install_as_MySQLdb()
|
|
|
|
|
|
app = FastAPI()
|
|
|
-
|
|
|
+db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
mode = 'run'
|
|
|
app.add_middleware(
|
|
|
CORSMiddleware,
|
|
@@ -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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ 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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ 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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ db_check()
|
|
|
user = authenticate_user(form_data.username, form_data.password)
|
|
|
if not user:
|
|
|
raise HTTPException(
|
|
@@ -226,7 +226,7 @@ async def login_for_access_token(request: Request, form_data: OAuth2PasswordRequ
|
|
|
|
|
|
@app.post("/token")
|
|
|
async def access_token(form_data: OAuth2PasswordRequestForm = Depends(), Authorize: AuthJWT = Depends()):
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+
|
|
|
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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ 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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ db_check()
|
|
|
user_obj = first(db.query('SELECT * FROM users where username ="'+str(request.username)+'"'))
|
|
|
|
|
|
if user_obj == None:
|
|
@@ -285,21 +285,21 @@ def logout(request: Request, Authorize: AuthJWT = Depends()):
|
|
|
|
|
|
@app.post('/get_role')
|
|
|
async def get_role(token: str = Depends(oauth2_scheme)):
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+
|
|
|
user_id = util.user.get_user_id(token)
|
|
|
roles = util.user.get_user_role(user_id)
|
|
|
return roles
|
|
|
|
|
|
@app.post('/create_role')
|
|
|
async def get_role(token: str = Depends(oauth2_scheme)):
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+
|
|
|
user_id = get_user_id(token)
|
|
|
|
|
|
return user_id
|
|
|
|
|
|
@app.post('/get_avatar_by_role')
|
|
|
async def get_role(token: str = Depends(oauth2_scheme)):
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+
|
|
|
user_id = util.user.get_user_id(token)
|
|
|
roles = util.user.get_user_role(user_id)
|
|
|
|
|
@@ -307,7 +307,7 @@ async def get_role(token: str = Depends(oauth2_scheme)):
|
|
|
|
|
|
@app.get('/verify_email')
|
|
|
async def verify_email(code):
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ 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,8 +323,9 @@ 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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+
|
|
|
db['reset_pw_code'].insert({'code':code,'user_id':user_id,'email':email})
|
|
|
msg = '請至點擊網址以重設密碼 : https://www.choozmo.com:8887/reset_pwd_page 通行碼為 '+ code
|
|
|
print(msg)
|
|
@@ -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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ 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':'副標題數量、圖片(影片)數量以及台詞數量必須一致'}}
|
|
@@ -481,7 +483,7 @@ async def make_anchor_video_long(req:util.models.request,token: str = Depends(oa
|
|
|
video_id = save_history(req,name_hash,user_id)
|
|
|
x = threading.Thread(target=gen_video_long_queue, args=(name_hash,req.name, req.text_content, req.image_urls,int(req.avatar),req.multiLang,video_id,user_id))
|
|
|
x.start()
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+
|
|
|
returnMsg = ''
|
|
|
if first(db.query('SELECT COUNT(1) FROM video_queue'))['COUNT(1)'] >= 3:
|
|
|
returnMsg = {'msg':{'eng':'There are many videos have been processing, please wait.','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)):
|
|
@@ -519,7 +522,7 @@ async def make_anchor_video(req:util.models.request,token: str = Depends(oauth2_
|
|
|
video_id = save_history(req,name_hash,user_id)
|
|
|
x = threading.Thread(target=gen_video_queue, args=(name_hash,req.name, req.text_content, req.image_urls,int(req.avatar),req.multiLang,video_id,user_id))
|
|
|
x.start()
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+
|
|
|
if first(db.query('SELECT COUNT(1) FROM video_queue'))['COUNT(1)'] >= 3:
|
|
|
return {'msg':{'eng':'There are many videos have been processing, please wait.','zh':'目前有多部影片處理中,煩請耐心等候'}}
|
|
|
else:
|
|
@@ -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)):
|
|
@@ -547,7 +551,7 @@ async def make_anchor_video_eng(req:util.models.request_eng,token: str = Depends
|
|
|
video_id = save_history(req,name_hash, user_id)
|
|
|
x = threading.Thread(target=gen_video_queue_eng, args=(name_hash,req.name, req.text_content, req.image_urls,req.sub_titles,int(req.avatar),video_id))
|
|
|
x.start()
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+
|
|
|
if first(db.query('SELECT COUNT(1) FROM video_queue'))['COUNT(1)'] >= 3:
|
|
|
return {'msg':{'eng':'There are many videos have been processing, please wait.','zh':'目前有多部影片處理中,煩請耐心等候'}}
|
|
|
else:
|
|
@@ -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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ 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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ 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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ 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,10 +611,11 @@ 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()
|
|
|
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+
|
|
|
user_id = first(db.query('SELECT * FROM users where username="' + current_user +'"'))['id']
|
|
|
statement = 'SELECT * FROM history_input WHERE user_id="'+str(user_id)+'" ORDER BY timestamp DESC LIMIT 50'
|
|
|
|
|
@@ -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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ 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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ db_check()
|
|
|
credentials_exception = HTTPException(
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
detail="Could not validate credentials",
|
|
@@ -667,14 +672,14 @@ def get_user_id(token):
|
|
|
return user_id
|
|
|
|
|
|
def check_user_exists(username):
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ db_check()
|
|
|
if int(next(iter(db.query('SELECT COUNT(*) FROM AI_anchor.users WHERE username = "'+username+'"')))['COUNT(*)']) > 0:
|
|
|
return True
|
|
|
else:
|
|
|
return False
|
|
|
|
|
|
def get_user(username: str):
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+
|
|
|
if not check_user_exists(username): # if user don't exist
|
|
|
return False
|
|
|
user_dict = next(
|
|
@@ -683,7 +688,7 @@ def get_user(username: str):
|
|
|
return user
|
|
|
|
|
|
def user_register(user):
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ 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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ 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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ 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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ 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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ 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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ 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 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
+ 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')
|
|
|
+
|
|
|
|
|
|
|
|
|
|