소스 검색

修改逻辑

zhangyong 1 년 전
부모
커밋
c33ddcd888
3개의 변경된 파일84개의 추가작업 그리고 93개의 파일을 삭제
  1. 21 18
      main.py
  2. 3 0
      video_capture/douyin/douyin_author/douyin_author.py
  3. 60 75
      video_stitching/video_stitching.py

+ 21 - 18
main.py

@@ -1,29 +1,32 @@
 import os
 import sys
-import schedule
-import time
+import datetime
+
 
 sys.path.append(os.getcwd())
-from video_capture.douyin.douyin_author.douyin_author import douyinAuthor
+from datetime import datetime
+from common import MysqlHelper
 
 from video_stitching.video_stitching import VideoStitching
 
 
-def job_video_stitching():
-    VideoStitching.video_cookie()
-
-def job_douyin_data():
-    douyinAuthor.get_videoList()
-
-# 设置上午10点运行任务
-schedule.every().day.at("14:00").do(job_douyin_data)
-
-# 设置上午4点运行任务
-schedule.every().day.at("04:00").do(job_video_stitching)
-
-while True:
-    schedule.run_pending()
-    time.sleep(1)
 
+def get_account_id():
+    current_time = datetime.now()
+    formatted_time = current_time.strftime("%Y-%m-%d")
+    count = f"""SELECT COUNT(*) AS total_count
+                        FROM (
+                          SELECT audio, account_id
+                          FROM video_audio
+                          WHERE time = "{formatted_time}" 
+                          GROUP BY audio, account_id
+                        ) AS subquery;"""
+    count = MysqlHelper.get_values(count, "prod")
+    return count
+if __name__ == '__main__':
+    count = get_account_id()
+    count = str(count).replace('(', '').replace(')', '').replace(',', '')
+    if int(count) < 20:
+        VideoStitching.video_stitching()
 
 

+ 3 - 0
video_capture/douyin/douyin_author/douyin_author.py

@@ -155,3 +155,6 @@ class douyinAuthor():
                     except Exception as e:
                         Common.logger().warning(f"抓取单条视频异常:{e}\n")
                         continue
+
+if __name__ == '__main__':
+    douyinAuthor.get_videoList()

+ 60 - 75
video_stitching/video_stitching.py

@@ -1,20 +1,17 @@
 # -*- coding: utf-8 -*-
 # @Time: 2023/12/26
 import datetime
-import gc
 import random
 import os
 import sys
 import time
-import subprocess
 import resource
-
-import psutil
 import requests
 import urllib.parse
 
 
 sys.path.append(os.getcwd())
+from datetime import datetime
 from common import Feishu
 from common.aliyun_oss_uploading import Oss
 from common.common import Common
@@ -28,6 +25,7 @@ from moviepy import editor
 
 output_path = "./video_stitching/video/new_video.mp4"
 class VideoStitching():
+
     @classmethod
     def split_text(cls, text, max_length):
         words = text.split(' ')
@@ -51,8 +49,10 @@ class VideoStitching():
 
     @classmethod
     def insert_videoAudio(cls, audio_url, i):
+        current_time = datetime.now()
+        formatted_time = current_time.strftime("%Y-%m-%d")
         for j in audio_url:
-            insert_sql = f"""INSERT INTO video_audio (audio, video_id, account_id, oss_object_key) values ("{i}", "{j[0]}", "{j[1]}", "{j[2]}")"""
+            insert_sql = f"""INSERT INTO video_audio (audio, video_id, account_id, oss_object_key, time, tag) values ("{i}", "{j[0]}", "{j[1]}", "{j[2]}", "{formatted_time}")"""
             MysqlHelper.update_values(
                 sql=insert_sql,
                 env="prod",
@@ -90,7 +90,8 @@ class VideoStitching():
     def insert_piaoquantv(cls, oss_object_key):
         code = 1
         list = ["66481136", "66481137", "66481140", "66481141", "66481142"]
-        for item in list:
+        for i in range(2):
+            item = random.choice(list)
             title = Material.get_title()
             url = "https://vlogapi.piaoquantv.com/longvideoapi/crawler/video/send"
             payload = dict(pageSource='vlog-pages/post/post-video-post', videoPath=oss_object_key, width='720',
@@ -271,76 +272,58 @@ class VideoStitching():
             return ""
 
     @classmethod
-    def video_stitching(cls, cookie):
-        count = 0
-        while True:
-            time.sleep(10)
-            # 获取音频
-            audioid = Material.get_audio()
-            # 获取已入库的用户id
-            account_id = cls.get_account_id()
-            audio_id = random.choice(audioid)
-            account = random.choice(account_id)
-            account = str(account).replace('(', '').replace(')', '').replace(',', '')
-            Common.logger().info(f"获取用户ID:{account}")
-            # 获取 未使用的视频链接
-            url_list = cls.get_url_list(audio_id, account)
-            # 获取音频url
-            audio = cls.get_audio_url(audio_id, cookie)
-            if audio == "":
-                continue
-            Common.logger().info(f"获取音频地址:{audio},获取用户id:{audio_id}")
-            videos = [list(item) for item in url_list]
-            videos = Oss.get_oss_url(videos)
-            # 视频截取
-            try:
-                audio_url, video_with_subtitles = cls.concatenate_videos(videos, str(audio), audio_id)
-                if len(audio_url) == 0:
-                    Common.logger().info(f"视频生成失败")
-                    continue
-                # 随机生成视频id
-                id = cls.random_id()
-                Common.logger().info(f"生成视频id为:{id}")
-                # 上传 oss
-                oss_object_key = Oss.stitching_sync_upload_oss(output_path, id)
-                status = oss_object_key.get("status")
-                # 获取 oss 视频地址
-                oss_object_key = oss_object_key.get("oss_object_key")
-                Common.logger().info(f"新拼接视频,oss发送成功,oss地址:{oss_object_key}")
-                if status == 200:
-                    time.sleep(10)
-                    # 发送成功 已使用视频存入数据库
-                    cls.insert_videoAudio(audio_url, audio)
-                    Common.logger().info(f"发送成功 已使用视频存入数据库完成")
-                    if os.path.isfile(output_path):
-                        os.remove(output_path)
-                        Common.logger().info(f"文件删除成功{output_path}")
-                    else:
-                        Common.logger().info(f"文件不存在{output_path}")
-                    piaoquantv = cls.insert_piaoquantv(oss_object_key)
-                    if piaoquantv:
-                        count += 1
-                        Common.logger().info(f"视频添加到对应用户成功")
-
-                        if count >= 20:
-                            break
-                        time.sleep(180)
-                        Common.logger().info(f"等待3分钟")
-                        # 释放视频对象
-                        video_with_subtitles.close()
-                        gc.collect()  # 手动回收垃圾
-
-            except Exception as e:
-                Common.logger().warning(f"新拼接视频发送oss失败:{e}\n")
-                continue
-            if count >= 20:
-                break
-
-    @classmethod
-    def video_cookie(cls):
-        # 获取后台cookie
+    def video_stitching(cls):
         cookie = Material.get_houtai_cookie()
-        cls.video_stitching(cookie)
+        # 获取音频
+        audioid = Material.get_audio()
+        # 获取已入库的用户id
+        account_id = cls.get_account_id()
+        audio_id = random.choice(audioid)
+        account = random.choice(account_id)
+        account = str(account).replace('(', '').replace(')', '').replace(',', '')
+        Common.logger().info(f"获取用户ID:{account}")
+        # 获取 未使用的视频链接
+        url_list = cls.get_url_list(audio_id, account)
+        # 获取音频url
+        audio = cls.get_audio_url(audio_id, cookie)
+        if audio == "":
+            Common.logger().info(f"获取音频地址为空")
+            return
+        Common.logger().info(f"获取音频地址:{audio},获取用户id:{audio_id}")
+        videos = [list(item) for item in url_list]
+        videos = Oss.get_oss_url(videos)
+        # 视频截取
+        try:
+            audio_url, video_with_subtitles = cls.concatenate_videos(videos, str(audio), audio_id)
+            if len(audio_url) == 0:
+                Common.logger().info(f"视频生成失败")
+            # 随机生成视频id
+            id = cls.random_id()
+            Common.logger().info(f"生成视频id为:{id}")
+            # 上传 oss
+            oss_object_key = Oss.stitching_sync_upload_oss(output_path, id)
+            status = oss_object_key.get("status")
+            # 获取 oss 视频地址
+            oss_object_key = oss_object_key.get("oss_object_key")
+            Common.logger().info(f"新拼接视频,oss发送成功,oss地址:{oss_object_key}")
+            if status == 200:
+                time.sleep(10)
+                # 发送成功 已使用视频存入数据库
+                cls.insert_videoAudio(audio_url, audio)
+                Common.logger().info(f"发送成功 已使用视频存入数据库完成")
+                if os.path.isfile(output_path):
+                    os.remove(output_path)
+                    Common.logger().info(f"文件删除成功{output_path}")
+                else:
+                    Common.logger().info(f"文件不存在{output_path}")
+                piaoquantv = cls.insert_piaoquantv(oss_object_key)
+                if piaoquantv:
+                    Common.logger().info(f"视频添加到对应用户成功")
+                    # 释放视频对象
+                    video_with_subtitles.close()
+        except Exception as e:
+            Common.logger().warning(f"新拼接视频发送oss失败:{e}\n")
+            VideoStitching.video_stitching()
 
 
 
@@ -348,3 +331,5 @@ class VideoStitching():
 
 if __name__ == '__main__':
     VideoStitching.video_stitching()
+
+