alroyso 1 year ago
parent
commit
2d134e236c
3 changed files with 35 additions and 20 deletions
  1. 22 10
      alist.py
  2. 2 2
      main.py
  3. 11 8
      monitor_aria2.py

+ 22 - 10
alist.py

@@ -84,34 +84,46 @@ class AlistAPI:
         response = requests.post(f"{self.url}/fs/move", data=payload, headers=self.headers)
         return response.json()
 
-    def download_directory(self, path, download_path):
-        file_list = self.list_directory(path)
+    def download_directory(self, remote_path, local_base_path, current_sub_path=''):
+        file_list = self.list_directory(remote_path)
         if not file_list:
             return
         for file_info in file_list['data']['content']:
+            # 检查是否是目录
             if file_info['is_dir']:
-                self.download_directory(path + "/" + file_info['name'], download_path)
+                # 构建新的子路径
+                new_sub_path = os.path.join(current_sub_path, file_info['name'])
+                # 递归下载该目录
+                self.download_directory(remote_path + "/" + file_info['name'], local_base_path, new_sub_path)
             else:
-                file_download_url = self.url + "/d" + path + "/" + file_info['name']
+                file_download_url = self.url + "/d" + remote_path + "/" + file_info['name']
                 sign = file_info.get('sign')
                 if sign:
                     file_download_url += "?sign=" + sign
+                # 构建完整的本地下载路径
+                download_path = os.path.join(local_base_path, current_sub_path.lstrip('/'))
                 self.aria2_api.add_uris([file_download_url], options={"dir": download_path})
 
-    def debug_directory(self, path, download_path):
-        file_list = self.list_directory(path)
+    def debug_directory(self, remote_path, local_base_path, current_sub_path=''):
+        file_list = self.list_directory(remote_path)
         if not file_list:
             return
         for file_info in file_list['data']['content']:
+            # 检查是否是目录
             if file_info['is_dir']:
-                print(f"Directory: {path}/{file_info['name']}")
-                self.debug_directory(path + "/" + file_info['name'], download_path)
+                # 构建新的子路径
+                new_sub_path = os.path.join(current_sub_path, file_info['name'])
+                print(f"Directory: {remote_path}/{file_info['name']} -> Local: {local_base_path}/{new_sub_path}")
+                # 递归遍历子目录
+                self.debug_directory(remote_path + "/" + file_info['name'], local_base_path, new_sub_path)
             else:
-                file_download_url = self.url + "/d" + path + "/" + file_info['name']
+                file_download_url = self.url + "/d" + remote_path + "/" + file_info['name']
                 sign = file_info.get('sign')
                 if sign:
                     file_download_url += "?sign=" + sign
-                print(f"File: {file_download_url}, Save to: {download_path}")
+                # 显示将要下载到的本地路径
+                download_path = os.path.join(local_base_path, current_sub_path.lstrip('/'))
+                print(f"File: {file_download_url} -> Local: {download_path}/{file_info['name']}")
 
     # alist 下载完成复制
     def monitor_and_copy(self, download_path, destination_path, check_interval=10):

+ 2 - 2
main.py

@@ -6,8 +6,8 @@ if __name__ == '__main__':
     print(directory_info)
     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)
+    alist.download_directory('/box/media/moive', '/downloads/moive')
+    #print(lis_file)
     #
     # destination_folder = "/mnt/video/download/movie"
     # alist.monitor_and_move(destination_folder)

+ 11 - 8
monitor_aria2.py

@@ -9,7 +9,8 @@ class Aria2Monitor:
         self.aria2_client = Client(aria2_rpc_url, secret=aria2_rpc_secret)
         self.aria2_api = API(self.aria2_client)
 
-    def monitor_and_move_shutil(self, destination_folder, check_interval=10):
+
+    def monitor_and_move_shutil(self, source_folder, destination_folder, check_interval=10):
         while True:
             downloads = self.aria2_api.get_downloads()
             for download in downloads:
@@ -17,17 +18,17 @@ class Aria2Monitor:
                     all_files_moved = True
                     for file in download.files:
                         if file.selected:
-                            file_path = file.path
-                            file_name = os.path.basename(file_path)
+                            file_name = os.path.basename(file.path)
+                            source_path = os.path.join(source_folder, file_name)  # 使用传入的源目录地址
                             target_path = os.path.join(destination_folder, file_name)
-                            print(f"Prepare to move: {file_path} -> {target_path}")
+                            print(f"Prepare to move: {source_path} -> {target_path}")
 
                             try:
-                                if os.path.exists(file_path):
-                                    shutil.move(file_path, target_path)
-                                    print(f"Successfully moved: {file_path} -> {target_path}")
+                                if os.path.exists(source_path):
+                                    shutil.move(source_path, target_path)
+                                    print(f"Successfully moved: {source_path} -> {target_path}")
                                 else:
-                                    print(f"File not found: {file_path}")
+                                    print(f"File not found: {source_path}")
                                     all_files_moved = False
                             except Exception as e:
                                 print(f"Error moving file: {e}")
@@ -40,8 +41,10 @@ class Aria2Monitor:
             time.sleep(check_interval)
 
 
+
 if __name__ == '__main__':
     # 使用示例
     aria2_monitor = Aria2Monitor("http://192.168.88.10", "123456")
+    source_folder = "/mnt/data1/download/moive"
     destination_folder = "/mnt/video/sync/movie"
     aria2_monitor.monitor_and_move_shutil(destination_folder)