Last active
September 2, 2021 01:54
-
-
Save tongyifan/83220b417cffdd23528860ee0c518d15 to your computer and use it in GitHub Desktop.
U2更新种子securekey(qBittorrent)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
# 0. 免责:仅在本人的qBittorrent v4.1.8上测试通过,本人不承担任何责任 | |
# 1. 安装依赖: pip3 install requests python-qbittorrent | |
# 2. 修改代码开头的apikey和qbittorrent_config | |
# 3. 运行: python3 u2.py | |
# 4. 等待运行完成 | |
import time | |
import requests | |
from qbittorrent import Client | |
# todo: edit me | |
apikey = "" | |
qbittorrent_config = { | |
"host": "", | |
"username": "", | |
"password": "", | |
} | |
# 此行往下不用修改 | |
endpoint = "https://u2.dmhy.org/jsonrpc_torrentkey.php" | |
u2_tracker = ["daydream.dmhy.best", "tracker.dmhy.org"] | |
to_tracker = "https://daydream.dmhy.best/announce?secure={}" | |
qb = Client(qbittorrent_config["host"]) | |
qb.login(qbittorrent_config["username"], qbittorrent_config["password"]) | |
def get_u2_torrents_hash(): | |
print("种子越多,等待时间越长,去倒杯水吧(笑") | |
torrents = qb.torrents() | |
u2_torrent_info_hash = [] | |
for torrent in torrents: | |
info_hash = torrent.get("hash") | |
torrent_trackers = [ | |
tracker.get("url") | |
for tracker in qb.get_torrent_trackers(info_hash) | |
if tracker.get("tier") == 0 | |
] | |
for torrent_tracker in torrent_trackers: | |
if any([tracker in torrent_tracker for tracker in u2_tracker]): | |
u2_torrent_info_hash.append( | |
{"hash": info_hash, "tracker": torrent_tracker} | |
) | |
return u2_torrent_info_hash | |
def main(): | |
u2_torrent_info_hash = get_u2_torrents_hash() | |
updated_torrents = [] | |
while len(u2_torrent_info_hash) > 0: | |
batch_size = 100 | |
print(f"找到了{len(u2_torrent_info_hash)}个未被更新的种子~") | |
print("成功更新的种子是无回显的,所以显示出来的只有报错,不必焦虑xD") | |
for i in range(0, len(u2_torrent_info_hash), batch_size): | |
print( | |
f"正在获取第{i}到第{i + len(u2_torrent_info_hash[i:i + batch_size])}个种子的新secret key" | |
) | |
request_data = [] | |
_index = {} | |
for index, torrent in enumerate(u2_torrent_info_hash[i : i + batch_size]): | |
request_data.append( | |
{ | |
"jsonrpc": "2.0", | |
"method": "query", | |
"params": [torrent["hash"]], | |
"id": index, | |
} | |
) | |
_index[index] = torrent | |
resp = requests.post(endpoint, params={"apikey": apikey}, json=request_data) | |
if resp.status_code == 503: | |
while resp.status_code == 503: | |
wait_second = int(resp.headers["Retry-After"]) + 5 | |
print(f"速度过快,将在{wait_second}秒后重试") | |
time.sleep(wait_second) | |
resp = requests.post( | |
endpoint, params={"apikey": apikey}, json=request_data | |
) | |
if resp.status_code == 403: | |
print("APIKEY无效,请检查(注意APIKEY不是passkey)") | |
exit() | |
if resp.status_code != 200: | |
print(f"意外的错误:{resp.status_code}") | |
response_data = resp.json() | |
for item in response_data: | |
if item.get("error"): | |
print(f"出错啦,稍后会自动重试:{item.get('error')}") | |
continue | |
qb._post( | |
"torrents/editTracker", | |
data={ | |
"hash": _index[item["id"]]["hash"], | |
"origUrl": _index[item["id"]]["tracker"], | |
"newUrl": to_tracker.format(item["result"]), | |
}, | |
) | |
updated_torrents.append(_index[item["id"]]["hash"]) | |
time.sleep(2) | |
u2_torrent_info_hash = [ | |
t for t in u2_torrent_info_hash if t["hash"] not in updated_torrents | |
] | |
if len(u2_torrent_info_hash): | |
print(f"还有{len(u2_torrent_info_hash)}个种子没完成更新,正在自动重试") | |
print("更新完成,撒花") | |
if __name__ == "__main__": | |
main() |
当某个item报错误-10003(种子不存在)时,那个item的id就会是空的,所以我没法判断它对应哪个种子(看别人的反馈API返回的是顺序的,但我并不敢赌),因此就没法处理失效种子了
我的意思是如果发现种子失效,可以跳过它(自动或手动选择),而不是必须清理掉所有失效种子才能继续运行。这些种子可以暂时维持原样,如有需要可以在这些种子有效后(可能再恢复有效吗?)再运行一次这个脚本。
用docker部署的qb,怎么/json/torrents是404啊 OTL
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
当某个item报错误-10003(种子不存在)时,那个item的id就会是空的,所以我没法判断它对应哪个种子(看别人的反馈API返回的是顺序的,但我并不敢赌),因此就没法处理失效种子了