admin 1 year ago
parent
commit
049105eff5
3 changed files with 68 additions and 32 deletions
  1. 44 14
      alist.py
  2. 14 7
      main.py
  3. 10 11
      monitor_aria2.py

+ 44 - 14
alist.py

@@ -84,6 +84,20 @@ 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)
+    #     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)
+    #         else:
+    #             file_download_url = self.url + "/d" + path + "/" + file_info['name']
+    #             sign = file_info.get('sign')
+    #             if sign:
+    #                 file_download_url += "?sign=" + sign
+    #             self.aria2_api.add_uris([file_download_url], options={"dir": download_path})
+
     def download_directory(self, remote_path, local_base_path, current_sub_path=''):
         file_list = self.list_directory(remote_path)
         if not file_list:
@@ -96,13 +110,14 @@ class AlistAPI:
                 # 递归下载该目录
                 self.download_directory(remote_path + "/" + file_info['name'], local_base_path, new_sub_path)
             else:
-                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})
+                # 获取文件的详细信息
+                file_detail = self.get_file_or_directory_info(remote_path + "/" + file_info['name'])
+                if file_detail and file_detail['code'] == 200:
+                    raw_url = file_detail['data']['raw_url']
+                    # 将真实 URL 添加到 Aria2 下载队列
+                    self.aria2_api.add_uris([raw_url], options={"dir": current_sub_path})
+                else:
+                    print(f"Failed to get detailed information for {file_info['name']}")
 
     def debug_directory(self, remote_path, local_base_path, current_sub_path=''):
         file_list = self.list_directory(remote_path)
@@ -117,13 +132,28 @@ class AlistAPI:
                 # 递归遍历子目录
                 self.debug_directory(remote_path + "/" + file_info['name'], local_base_path, new_sub_path)
             else:
-                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('/'))
-                print(f"File: {file_download_url} -> Local: {download_path}/{file_info['name']}")
+                # 获取文件的详细信息
+                file_detail = self.get_file_or_directory_info(remote_path + "/" + file_info['name'])
+                if file_detail and file_detail['code'] == 200:
+                    raw_url = file_detail['data']['raw_url']
+                    print(f"File: {file_info['name']} -> Download URL: {raw_url}")
+                else:
+                    print(f"Failed to get detailed information for {file_info['name']}")
+
+    # def debug_directory(self, path, download_path):
+    #     file_list = self.list_directory(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)
+    #         else:
+    #             file_download_url = self.url + "/d" + 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}")
 
     # alist 下载完成复制
     def monitor_and_copy(self, download_path, destination_path, check_interval=10):

+ 14 - 7
main.py

@@ -1,21 +1,28 @@
+import json
+
 from alist import AlistAPI
 
 if __name__ == '__main__':
-    alist = AlistAPI('http://192.168.88.10:5244/api', 'http://192.168.88.10', '123456', 'admin', 'nokidc123@#')
+    alist = AlistAPI('http://box.szfa.xyz:5244/api', 'http://192.168.88.10', '4e34854d5d7390ef7801', 'admin',
+                     'nokidc123@#')
     directory_info = alist.get_directory()
     print(directory_info)
-    lis_file_debug = alist.debug_directory('/box/media/moive', '/downloads/moive')
+
+    # file_or_directory = json.dumps(alist.get_file_or_directory_info('/data/media/moive/War of the Worlds (2005) 1080p/War of the Worlds (2005) Bluray-1080p.mp4'), indent=4, )
+    # print(file_or_directory)
+
+    lis_file_debug = alist.debug_directory('/data/media/moive', '/mnt/data1/downloads/moive')
     print(lis_file_debug)
-    alist.download_directory('/box/media/moive', '/downloads/moive')
-    #print(lis_file)
+    alist.download_directory('/data/media/moive', '/mnt/data1/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)
 

+ 10 - 11
monitor_aria2.py

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