Last active
March 28, 2022 06:07
-
-
Save xNihil0/2f6c9d07217ecbf16ff45e4a6fe742b1 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 -*- | |
import qbittorrentapi | |
import json | |
import requests | |
import time | |
import sys | |
# 运行过程中遇到部分种子更新失败是正常现象, 请多次运行至所有种子均已更新完成 | |
# 使用之前请先执行 pip3 install qbittorrent-api requests 安装依赖 | |
# api 填写你的 API URL, 可以在 https://u2.dmhy.org/privatetorrents.php 获取 | |
# host 填写你的 qBittorrent WebUI 地址, 默认本地 localhost | |
# port 填写你的 qBittorrent WebUI 端口 | |
# username 填写你的 qBittorrent WebUI 用户名 | |
# password 填写你的 qBittorrent WebUI 密码 | |
api = r'https://u2.dmhy.org/jsonrpc_torrentkey.php?apikey=' | |
host = r'localhost' | |
port = 8080 | |
username = r'' | |
password = r'' | |
def update_tracker(data): | |
json_data = json.dumps(data) | |
flag = False | |
while flag is False: | |
try: | |
req = requests.post(api, data=json_data) | |
except Exception as ex: | |
time.sleep(10) | |
continue | |
if req.status_code == 200: | |
flag = True | |
res = json.loads(req.text) | |
for i in range(len(res)): | |
result = res[i] | |
torrent_hash = data[i]['params'][0] | |
if 'error' in result.keys(): | |
print("Error: " + result["error"]["message"] + " Torrent hash: " + torrent_hash) | |
elif 'result' in result.keys(): | |
tracker_url = r"https://daydream.dmhy.best/announce?secure=" + result["result"] | |
try: | |
old_url = '' | |
for tracker in qbt_client.torrents_trackers(torrent_hash): | |
if 'dmhy' in tracker['url']: | |
old_url = tracker['url'] | |
qbt_client.torrents_edit_tracker(torrent_hash, old_url, tracker_url) | |
print("Success. Torrent Hash: " + torrent_hash) | |
except Exception as ex: | |
print('Error: ' + str(ex)) | |
elif req.status_code == 403: | |
print("Error: Invalid API key") | |
sys.exit(-1) | |
elif req.status_code == 503: | |
delay = int(req.headers["Retry-After"]) | |
print("Error: " + req.text + " Retry after " + str(delay) + " seconds.") | |
time.sleep(delay) | |
continue | |
time.sleep(3) | |
if __name__ == "__main__": | |
qbt_client = qbittorrentapi.Client(host, port, username, password) | |
try: | |
qbt_client.auth_log_in() | |
except qbittorrentapi.LoginFailed as e: | |
print(e) | |
cnt = 0 | |
data = [] | |
for torrent in qbt_client.torrents_info(): | |
need_update = False | |
for tracker in torrent.trackers: | |
if any(entry in tracker['url'] for entry in ['daydream.dmhy.best', 'tracker.dmhy.org']) and 'Invalid key' in tracker['msg']: | |
need_update = True | |
if need_update is True: | |
cnt += 1 | |
curr = {'jsonrpc': '2.0', 'method': 'query', 'params': [torrent.hash], 'id': cnt} | |
data.append(curr) | |
if cnt == 100: | |
update_tracker(data) | |
cnt = 0 | |
data = [] | |
update_tracker(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Successfully installed attrdict-2.0.1 certifi-2020.6.20 chardet-3.0.4 enum34-1.1.10 idna-2.10 qbittorrent-api-2020.8.8 requests-2.24.0 urllib3-1.25.10
root@NAS:~# python qB_updatesecure.py
Traceback (most recent call last):
File "qB_updatesecure.py", line 4, in
import qbittorrentapi
ImportError: No module named qbittorrentapi