瀏覽代碼

修复下载未完成就开始移动问题

cauto 1 年之前
父節點
當前提交
b1f30b0d82
共有 4 個文件被更改,包括 73 次插入55 次删除
  1. 2 0
      .gitignore
  2. 41 28
      src/task/Aria2FileCopyTask.py
  3. 21 20
      src/task/RemoteDownloadTask.py
  4. 9 7
      tests/test.py

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+/venv/
+/.idea/

+ 41 - 28
src/task/Aria2FileCopyTask.py

@@ -26,6 +26,36 @@ class Aria2FileCopyTask:
         if self.thread:
             self.thread.join()
 
+    def copy_new_files(self, src_dir, dest_dir):
+        """复制新文件和目录,如果它们在目标目录中不存在"""
+        if not os.path.exists(dest_dir):
+            os.makedirs(dest_dir)
+        for item in os.listdir(src_dir):
+            src_item = os.path.join(src_dir, item)
+            dest_item = os.path.join(dest_dir, item)
+
+            # 如果是目录
+            if os.path.isdir(src_item):
+                # 检查目标目录是否存在
+                if not os.path.exists(dest_item):
+                    # 直接复制整个目录
+                    shutil.copytree(src_item, dest_item)
+                    logging.info(f"Copied directory from {src_item} to {dest_item}")
+                else:
+                    # 如果目标目录已存在,递归处理子目录内容
+                    self.copy_new_files(src_item, dest_item)
+            elif not item.endswith('.aria2'):  # 检查是否为 .aria2 文件
+                # 如果是文件,且不是 .aria2 文件,则进行复制
+                if not os.path.exists(dest_item):
+                    shutil.copy(src_item, dest_item)
+                    logging.info(f"Copied file from {src_item} to {dest_item}")
+                    # 添加复制完成的提示
+                    logging.info(f"Copy complete {dest_item} ")
+                    self.nas_sync()
+                    logging.info("Completed checking for new files to copy.")
+                else:
+                    logging.info(f"File already exists, skipping: {dest_item}")
+
     def monitor_aria2_and_move(self):
         logging.info("Starting monitoring aria2 is complete and move to " + self.destination_path)
         while self.is_running:
@@ -33,13 +63,15 @@ class Aria2FileCopyTask:
             downloads = self.aria2_api.get_downloads()
             for download in downloads:
                 if download.is_complete:
-                    # 检查下载路径是否存在并且里面是否有文件
-                    if os.path.exists(self.download_path) and os.listdir(self.download_path):
-                        # 无论 Aria2 是否有记录,都执行移动操作
-                        self.move_new_files(self.download_path, self.destination_path)
-
-                    else:
-                        logging.info("No new files to move.")
+                    # 检查下载的每个文件
+                    for file in download.files:
+                        if file.selected:
+                            filename = os.path.basename(file.path)
+                            file_path = os.path.join(self.download_path + '/movie', filename)
+                            temp_file = file_path + '.aria2'
+                            print(f'temp_file: {temp_file}')
+                            if os.path.exists(file_path) and not os.path.exists(temp_file):
+                                self.move_new_files(self.download_path, self.destination_path)
 
             time.sleep(10)
 
@@ -75,8 +107,8 @@ class Aria2FileCopyTask:
                 else:
                     # 如果目标目录已存在,递归处理子目录内容
                     self.move_new_files(src_item, dest_item)
-            else:
-                # 如果是文件,处理逻辑与之前相同
+            elif not item.endswith('.aria2'):  # 检查是否为 .aria2 文件
+                # 如果是文件,且不是 .aria2 文件,则进行移动
                 if not os.path.exists(dest_item):
                     shutil.move(src_item, dest_item)
                     logging.info(f"Moved file from {src_item} to {dest_item}")
@@ -86,22 +118,3 @@ class Aria2FileCopyTask:
                     logging.info("Completed checking for new files to move.")
                 else:
                     logging.info(f"File already exists, skipping: {dest_item}")
-
-    def copy_new_files(self, src_dir, dest_dir):
-        """复制新文件,如果它们在目标目录中不存在"""
-        if not os.path.exists(dest_dir):
-            os.makedirs(dest_dir)
-        for item in os.listdir(src_dir):
-            src_item = os.path.join(src_dir, item)
-            dest_item = os.path.join(dest_dir, item)
-
-            if os.path.isdir(src_item):
-                # 如果是目录,则递归复制
-                self.copy_new_files(src_item, dest_item)
-            else:
-                # 如果是文件,则检查目标文件是否存在
-                if not os.path.exists(dest_item):
-                    shutil.copy(src_item, dest_item)
-                    logging.info(f"Copied new file from {src_item} to {dest_item}")
-                else:
-                    logging.info(f"File already exists, skipping: {dest_item}")

+ 21 - 20
src/task/RemoteDownloadTask.py

@@ -139,28 +139,29 @@ class RemoteDownloadTask:
             downloads = self.aria2_api.get_downloads()
             for download in downloads:
                 if download.is_complete:
+                    pass
                     # 获取下载完成的文件名
                     # completed_files = [os.path.basename(file.path) for file in download.files if file.selected]
 
-                    movie_name = self.radar_client.get_all_movie_names()
-                    for file_name in movie_name:
-                        # logging.info(f"Download completed: {file_name}")
-                        # 处理每个下载完成的文件
-                        # 调用方法并获取返回的电影 ID
-                        movie_id = self.radar_client.find_movie_id_by_filename(file_name)
-
-                        # 打印结果
-                        if movie_id is not None:
-                            print(f"Found movie ID for '{file_name}': {movie_id}")
-                        else:
-                            print(f"No movie found for '{file_name}'")
-
-                        # try:
-                        #     delete_info = self.radar_client.delete_movie(movie_id)
-                        #     if delete_info is not None:
-                        #         delete_info = json.dumps(delete_info, indent=4)
-                        #         print(f"delete_info: {delete_info}")
-                        # except Exception as e:
-                        #     print(f'Exception: {e}')
+                    # movie_name = self.radar_client.get_all_movie_names()
+                    # for file_name in movie_name:
+                    #     # logging.info(f"Download completed: {file_name}")
+                    #     # 处理每个下载完成的文件
+                    #     # 调用方法并获取返回的电影 ID
+                    #     movie_id = self.radar_client.find_movie_id_by_filename(file_name)
+                    #
+                    #     # 打印结果
+                    #     if movie_id is not None:
+                    #         print(f"Found movie ID for '{file_name}': {movie_id}")
+                    #     else:
+                    #         print(f"No movie found for '{file_name}'")
+                    #
+                    #     # try:
+                    #     #     delete_info = self.radar_client.delete_movie(movie_id)
+                    #     #     if delete_info is not None:
+                    #     #         delete_info = json.dumps(delete_info, indent=4)
+                    #     #         print(f"delete_info: {delete_info}")
+                    #     # except Exception as e:
+                    #     #     print(f'Exception: {e}')
 
             time.sleep(self.check_interval)

+ 9 - 7
tests/test.py

@@ -37,13 +37,15 @@ def test_find_movie_id_by_filename():
     downloads = aria2_api.get_downloads()
     for download in downloads:
         if download.is_complete:
-            completed_files = [os.path.basename(file.path) for file in download.files if file.selected]
-            print(f"completed_files: {completed_files}")
-            # 查找电影ID
-            for completed_file in completed_files:
-                ids = radar_client.find_movie_id_by_filename(completed_file)
-                if ids is not None:
-                    print(f"ids: {ids}")
+            # 检查下载的每个文件
+            for file in download.files:
+                if file.selected:
+                    completed_files = [os.path.basename(file.path) for file in download.files if file.selected]
+                    for completed_file in completed_files:
+
+                        file_path = os.path.join(config['ARIA2']['DESTINATION_PATH'], completed_file)
+                        temp_file = file_path + '.aria2'
+                        print(f'temp_file: {temp_file}')
 
 
 # 运行测试