alroyso 1 year ago
parent
commit
91166557e7
2 changed files with 24 additions and 18 deletions
  1. 7 5
      main.py
  2. 17 13
      monitor_aria2.py

+ 7 - 5
main.py

@@ -4,16 +4,18 @@ if __name__ == '__main__':
     alist = AlistAPI('http://192.168.88.10:5244/api', 'http://192.168.88.10', '123456', 'admin', 'nokidc123@#')
     directory_info = alist.get_directory()
     print(directory_info)
-    # lis_file = alist.debug_directory('/box/media/moive', '/download/moive')
-    # print(lis_file)
+    lis_file_debug = alist.debug_directory('/box/media/moive', '/downloads/moive')
+    print(lis_file_debug)
+    lis_file = alist.download_directory('/box/media/moive', '/downloads/moive')
+    print(lis_file)
     #
     # destination_folder = "/mnt/video/download/movie"
     # alist.monitor_and_move(destination_folder)
 
-    download_path = "/data1/download/movie"
-    destination_path = "/video/sync/movie"
+    #download_path = "/data1/download/movie"
+    #destination_path = "/video/sync/movie"
     # 调用复制函数
-    alist.monitor_and_copy(download_path, destination_path)
+    #alist.monitor_and_copy(download_path, destination_path)
     # 调用移动函数
     # alist.monitor_and_move(download_path, destination_path)
 

+ 17 - 13
monitor_aria2.py

@@ -10,28 +10,32 @@ class Aria2Monitor:
         self.aria2_api = API(self.aria2_client)
 
     def monitor_and_move_shutil(self, destination_folder, check_interval=10):
-        """
-        Monitor active downloads and move completed downloads with multiple files to a destination folder.
-        """
         while True:
             downloads = self.aria2_api.get_downloads()
             for download in downloads:
                 if download.is_complete:
+                    all_files_moved = True
                     for file in download.files:
                         if file.selected:
                             file_path = file.path
                             file_name = os.path.basename(file_path)
-
-                            # 构建目标路径
                             target_path = os.path.join(destination_folder, file_name)
-
-                            # 移动文件
-                            if os.path.exists(file_path):
-                                shutil.move(file_path, target_path)
-                                print(f"Moved: {file_path} -> {target_path}")
-
-                    # 从 Aria2 中移除已完成的下载记录
-                    download.remove()
+                            print(f"Prepare to move: {file_path} -> {target_path}")
+
+                            try:
+                                if os.path.exists(file_path):
+                                    shutil.move(file_path, target_path)
+                                    print(f"Successfully moved: {file_path} -> {target_path}")
+                                else:
+                                    print(f"File not found: {file_path}")
+                                    all_files_moved = False
+                            except Exception as e:
+                                print(f"Error moving file: {e}")
+                                all_files_moved = False
+
+                    # 仅在所有文件都成功移动后删除记录
+                    if all_files_moved:
+                        download.remove()
 
             time.sleep(check_interval)