Ver código fonte

上传视频异常时,删除已下载的视频文件夹

wangkun 2 anos atrás
pai
commit
b6639bfea2
5 arquivos alterados com 33 adições e 10 exclusões
  1. 16 0
      main/common.py
  2. 16 1
      main/demo.py
  3. 1 0
      main/publish.py
  4. 0 6
      requirements.txt
  5. 0 3
      videos/__init__.py

+ 16 - 0
main/common.py

@@ -5,6 +5,7 @@
 公共方法,包含:生成log / 删除log / 获取session  / 下载方法 / 读取文件 / 统计下载数
 """
 import json
+import shutil
 from datetime import date, timedelta
 import datetime
 import os
@@ -82,6 +83,17 @@ class Common:
 
         # 删除 charles 缓存文件,只保留最近的两个文件
 
+    # 删除某个文件夹下所有文件
+    @classmethod
+    def del_files(cls, log_type):
+        filepath = "./videos/"
+        del_list = os.listdir(filepath)
+        for f in del_list:
+            file_path = os.path.join(filepath, f)
+            if os.path.isdir(file_path):
+                shutil.rmtree(file_path)
+        cls.logger(log_type).info("删除文件夹成功\n")
+
     # 封装下载视频或封面的方法
     @classmethod
     def download_method(cls, log_type, text, d_name, d_url):
@@ -91,6 +103,10 @@ class Common:
         视频封面,或视频播放地址:d_url
         下载保存路径:"./files/{d_title}/"
         """
+        videos_path = "./videos/"
+        if not os.path.exists(videos_path):
+            os.mkdir(videos_path)
+
         # 首先创建一个保存该视频相关信息的文件夹
         video_dir = "./videos/" + d_name + "/"
         if not os.path.exists(video_dir):

+ 16 - 1
main/demo.py

@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 # @Author: wangkun
 # @Time: 2022/7/12
+import os
 import shutil
 import ffmpeg
 
@@ -54,8 +55,22 @@ class Demo:
         print(title)
         print(len(title))
 
+    # 删除某个文件夹下所有文件
+    @classmethod
+    def del_files(cls):
+        filepath = "./videos/"
+        del_list = os.listdir(filepath)
+        print(f"del_list:{del_list}\n")
+        for f in del_list:
+            file_path = os.path.join(filepath, f)
+            print(f"file_path:{file_path}")
+            if os.path.isdir(file_path):
+                shutil.rmtree(file_path)
+        print(f"videos文件夹:{os.listdir(filepath)}\n")
+
 
 if __name__ == "__main__":
     demo = Demo()
     # demo.check_duration()
-    demo.check_title_len()
+    # demo.check_title_len()
+    demo.del_files()

+ 1 - 0
main/publish.py

@@ -257,3 +257,4 @@ class Publish:
                     Common.logger(log_type).warning('file not a dir = {}'.format(fi_d))
             except Exception as e:
                 Common.logger(log_type).error('upload_and_publish error', e)
+                Common.del_files(log_type)

+ 0 - 6
requirements.txt

@@ -1,6 +0,0 @@
-ffmpeg==1.4
-ffmpeg_python==0.2.0
-loguru==0.6.0
-oss2==2.15.0
-requests==2.27.1
-urllib3==1.26.9

+ 0 - 3
videos/__init__.py

@@ -1,3 +0,0 @@
-# -*- coding: utf-8 -*-
-# @Author: wangkun
-# @Time: 2022/7/12