浏览代码

add line notify

tomoya 7 月之前
父节点
当前提交
e96956d33c
共有 2 个文件被更改,包括 99 次插入3 次删除
  1. 1 1
      backend/app/app/api/api_v1/api.py
  2. 98 2
      backend/app/app/api/api_v1/endpoints/videos.py

+ 1 - 1
backend/app/app/api/api_v1/api.py

@@ -10,7 +10,7 @@ api_router.include_router(login.router, tags=["login"])
 api_router.include_router(users.router, prefix="/users", tags=["users"])
 api_router.include_router(utils.router, prefix="/utils", tags=["utils"])
 api_router.include_router(videos.router, prefix="/videos", tags=["videos"])
-api_router.include_router(images.router, prefix="/images", tags=["iamges"])
+api_router.include_router(images.router, prefix="/images", tags=["images"])
 api_router.include_router(reputations.router, prefix="/reputations", tags=["reputations"])
 api_router.include_router(ser_no.router, prefix="/ser_nos", tags=["serial numbers"])
 api_router.include_router(ytviewspayment.router, prefix="/payment", tags=["yt views payment"])

+ 98 - 2
backend/app/app/api/api_v1/endpoints/videos.py

@@ -15,12 +15,14 @@ from app.core.config import settings
 from app.aianchor.utils2 import check_zip, VideoMakerError
 from pathlib import Path
 from app.db.session import SessionLocal
-
+import requests
 import asyncio
 
 BACKEND_ZIP_STORAGE = Path("/app").joinpath(settings.BACKEND_ZIP_STORAGE)
 LOCAL_ZIP_STORAGE = Path("/").joinpath(settings.LOCAL_ZIP_STORAGE)
 
+LINE_URL = 'https://notify-api.line.me/api/notify'
+LINE_TOKEN = '剛剛複製的權杖'
 
 router = APIRouter()
 video_clients = {}
@@ -131,6 +133,18 @@ def upload_plot(
     video_data['style'] = style
     video_data['lang'] = lang
     background_tasks.add_task(wait_finish, video_data)
+
+    headers = {
+        'Authorization': 'Bearer ' + LINE_TOKEN    # 設定權杖
+    }
+    data = {
+        'message':f'cloud.choozmo.com \
+                    Video \
+                    user: {current_user.id}\n \
+                    video: {video_data['id']}\n \
+                    state: queuing'     # 設定要發送的訊息
+    }
+    data = requests.post(LINE_URL, headers=headers, data=data) 
     return  JSONResponse(return_msg, background=background_tasks)
 
 async def wait_finish(video_data:dict): 
@@ -152,7 +166,18 @@ async def wait_finish(video_data:dict):
     db.close()
     msg_data = f"{video_data['stored_filename']}:STARTED"
     await publish(msg_data)
-
+    headers = {
+        'Authorization': 'Bearer ' + LINE_TOKEN    # 設定權杖
+    }
+    data = {
+        'message':f'cloud.choozmo.com \
+                    Video \
+                    user: {video_data['owner_id']}\n \
+                    membership: {video_data['membership_status']}\n \
+                    video: {video_data['id']}\n \
+                    state: start'     # 設定要發送的訊息
+    }
+    data = requests.post(LINE_URL, headers=headers, data=data) 
     while True:
         await asyncio.sleep(1)
         if task.state != "STARTED":
@@ -169,6 +194,18 @@ async def wait_finish(video_data:dict):
         db.commit()
         db.close()
         msg_data = f"{video_data['stored_filename']}:SUCCESS:{int(time)}"
+        headers = {
+            'Authorization': 'Bearer ' + LINE_TOKEN    # 設定權杖
+        }
+        data = {
+            'message':f'cloud.choozmo.com \
+                        Video \
+                        user: {video_data['owner_id']}\n \
+                        membership: {video_data['membership_status']}\n \
+                        video: {video_data['id']}\n \
+                        state: success'     # 設定要發送的訊息
+        }
+        data = requests.post(LINE_URL, headers=headers, data=data) 
 
     elif task.state == "FAILURE":
         db = SessionLocal()
@@ -178,6 +215,19 @@ async def wait_finish(video_data:dict):
         db.close()
         msg_data = f"{video_data['stored_filename']}:FAILURE"
 
+        headers = {
+            'Authorization': 'Bearer ' + LINE_TOKEN    # 設定權杖
+        }
+        data = {
+            'message':f'cloud.choozmo.com \
+                        Video \
+                        user: {video_data['owner_id']}\n \
+                        membership: {video_data['membership_status']}\n \
+                        video: {video_data['id']}\n \
+                        state: failure'     # 設定要發送的訊息
+        }
+        data = requests.post(LINE_URL, headers=headers, data=data) 
+
     await publish(msg_data)
 
 async def publish(data):
@@ -210,5 +260,51 @@ async def websocket_endpoint(websocket: WebSocket):
     except:
         # 接続が切れた場合、当該クライアントを削除する
         del video_clients[key]
+  
+# # mia write  
+# from pydantic import BaseModel
+        
+# class phone_json(BaseModel):
+#     title : str
+#     text : str
+
+# IMAGEDIR = "/tmp/"
+# IMAGEDIR_all = "/var/lib/docker/overlay2/1e9b63ea29cc42551035ae2c5ce1cdf0c44a3f254879175a8a37db08ac7154de/diff/tmp/"
 
 
+# @router.post("/phone_input_json")
+# async def phone_input_json(
+#      input_model:List[UploadFile],
+#      text: str
+# ):
+#     try:
+#         result = []
+        
+#         text_json = eval(text)
+        
+#         for item,text_tmp in zip(input_model,text_json):
+        
+#             # img_url = ''
+#             # if item != '':
+#             #    try:
+#             #        contents = await item.read()
+        
+#                     #save the file
+#             #        with open(f"{IMAGEDIR}{item.filename}", "wb") as f:
+#             #            f.write(contents)
+                
+#             #        img_url = f"{IMAGEDIR_all}{item.filename}"
+#             #    except Exception as e:
+#             #         img_url= str(e)
+        
+#             result.append(
+#                 {
+#                     "img" : item,
+#                     "title" :text_tmp["title"],
+#                     "text" : str(text_tmp["text"])
+#                 }
+#             )
+            
+#         return result
+#     except Exception as e:
+#         return {"msg": str(e), "code": 500}