From c9c83cb65aed4213531d171518d6ff56aa61fb57 Mon Sep 17 00:00:00 2001 From: Cp0204 Date: Mon, 25 Nov 2024 01:20:34 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=96=B0=E5=A2=9E=20aria2=20?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=EF=BC=8C=E5=8F=AF=E5=88=9B=E5=BB=BA=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 提供 RPC 接口配置和任务处理功能 - 支持自动下载和自定义下载目录 --- plugins/_priority.json | 1 + plugins/aria2.py | 90 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 plugins/aria2.py diff --git a/plugins/_priority.json b/plugins/_priority.json index d24ee32..dbf851e 100644 --- a/plugins/_priority.json +++ b/plugins/_priority.json @@ -2,6 +2,7 @@ "alist", "alist_strm", "alist_strm_gen", + "aria2", "emby", "plex" ] \ No newline at end of file diff --git a/plugins/aria2.py b/plugins/aria2.py new file mode 100644 index 0000000..c01b4ee --- /dev/null +++ b/plugins/aria2.py @@ -0,0 +1,90 @@ +import os +import requests + + +class Aria2: + + default_config = { + "host_port": "172.17.0.1:6800", # Aria2 RPC地址 + "secret": "", # Aria2 RPC 密钥 + "dir": "/Downloads", # 下载目录,需要Aria2有权限访问 + } + default_task_config = { + "auto_download": False, # 是否开启自动下载 + } + is_active = False + rpc_url = None + + def __init__(self, **kwargs): + self.plugin_name = self.__class__.__name__.lower() + if kwargs: + for key, _ in self.default_config.items(): + if key in kwargs: + setattr(self, key, kwargs[key]) + else: + print(f"{self.plugin_name} 模块缺少必要参数: {key}") + if self.host_port and self.secret: + self.rpc_url = f"http://{self.host_port}/jsonrpc" + if self.get_version(): + self.is_active = True + + def run(self, task, **kwargs): + if task_config := task.get("addition", {}).get(self.plugin_name, {}): + if not task_config.get("auto_download"): + return + if (tree := kwargs.get("tree")) and (account := kwargs.get("account")): + for node in tree.all_nodes_itr(): + if not node.data.get("is_dir", True): + quark_path = node.data.get("path") + quark_fid = node.data.get("fid") + save_path = f"{self.dir}{quark_path}" + print(f"📥 Aria2下载: {quark_path}") + download_return, cookie = account.download([quark_fid]) + download_url = [ + item["download_url"] for item in download_return["data"] + ] + aria2_params = [ + download_url, + { + "header": [ + f"Cookie: {cookie}", + f"User-Agent: {account.common_headers().get('user-agent')}", + ], + "out": os.path.basename(save_path), + "dir": os.path.dirname(save_path), + }, + ] + self.add_uri(aria2_params) + + def _make_rpc_request(self, method, params=None): + """发出 JSON-RPC 请求.""" + jsonrpc_data = { + "jsonrpc": "2.0", + "id": "quark-auto-save", + "method": method, + "params": params or [], + } + if self.secret: + jsonrpc_data["params"].insert(0, f"token:{self.secret}") + try: + response = requests.post(self.rpc_url, json=jsonrpc_data) + response.raise_for_status() + return response.json() + except requests.exceptions.RequestException as e: + print(f"Aria2下载: 错误{e}") + return None + + def get_version(self): + """检查与 Aria2 的连接.""" + response = self._make_rpc_request("aria2.getVersion") + if response.get("result"): + print(f"Aria2下载: v{response['result']['version']}") + return True + else: + print(f"Aria2下载: 连接失败{response.get('error')}") + return False + + def add_uri(self, params=None): + """添加 URI 下载任务.""" + response = self._make_rpc_request("aria2.addUri", params) + return response.get("result") if response else None