Browse Source

增加定时任务

zhangyong 1 year ago
parent
commit
07c4dbf292
2 changed files with 48 additions and 2 deletions
  1. 43 0
      timed_task.py
  2. 5 2
      video_capture/douyin/douyin_author/douyin_author.py

+ 43 - 0
timed_task.py

@@ -0,0 +1,43 @@
+import threading
+import schedule
+import time
+
+from common.db import MysqlHelper
+from video_capture.douyin.douyin_author.douyin_author import douyinAuthor
+from video_stitching.video_stitching import VideoStitching
+from datetime import datetime, timedelta
+
+
+def get_count():
+    current_time = datetime.now()
+    previous_time = current_time - timedelta(minutes=5)
+    previous_time_str = previous_time.strftime("%Y-%m-%d %H:%M")
+    select_user_sql = f"""select video_id from video_url where time >="{previous_time_str}";"""
+    count = MysqlHelper.get_values(select_user_sql, "prod")
+    if count:
+        return True
+    else:
+        return False
+
+def check_token():
+    while True:
+        count = get_count()
+        if count:
+            time.sleep(600)  # 每10分钟检查一次
+        else:
+            douyinAuthor.get_token()
+
+
+def start_video_stitching():
+    VideoStitching.video_stitching()
+
+# 创建并启动线程
+token_thread = threading.Thread(target=check_token)
+token_thread.start()
+
+# 每天下午4点定时启动 video_stitching 方法
+schedule.every().day.at("16:00").do(start_video_stitching)
+
+while True:
+    schedule.run_pending()
+    time.sleep(1)

+ 5 - 2
video_capture/douyin/douyin_author/douyin_author.py

@@ -1,9 +1,10 @@
 # -*- coding: utf-8 -*-
 # @Time: 2023/12/22
+import datetime
 import os
 import random
 import sys
-import time
+from datetime import datetime
 import requests
 import json
 import urllib3
@@ -33,7 +34,9 @@ class douyinAuthor():
     """
     @classmethod
     def insert_videoUrl(cls, video_id, account_id, oss_object_key):
-        insert_sql = f"""INSERT INTO video_url (video_id, account_id, oss_object_key) values ("{video_id}", "{account_id}", "{oss_object_key}")"""
+        current_time = datetime.now()
+        formatted_time = current_time.strftime("%Y-%m-%d %H:%M")
+        insert_sql = f"""INSERT INTO video_url (video_id, account_id, oss_object_key, time) values ("{video_id}", "{account_id}", "{oss_object_key}", "{formatted_time}")"""
         MysqlHelper.update_values(
             sql=insert_sql,
             env="prod",