Skip to content

Instantly share code, notes, and snippets.

@inertia42
Last active August 30, 2020 13:22
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save inertia42/f6120d118e47925095dbceb5e8e27272 to your computer and use it in GitHub Desktop.
Save inertia42/f6120d118e47925095dbceb5e8e27272 to your computer and use it in GitHub Desktop.
U2更新种子securekey(Transmission)
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -1. 感谢tongyifan,本脚本由他的qBittorrent版本修改而来,项目地址 https://gist.github.com/tongyifan/83220b417cffdd23528860ee0c518d15
# 0. 免责:仅在本人的Transmission v2.94上测试通过,本人不承担任何责任
# 1. 安装依赖: pip3 install requests transmissionrpc
# 2. 修改代码开头的apikey和transmission_config
# 3. 运行: python3 u2_update.py
# 4. 中间有报错就再运行,直到显示找到0个未被更新的种子为止
# 5. 请勿手动中断脚本运行!
import json
import os
import time
import requests
import transmissionrpc
# todo: edit me
apikey = ""
transmission_config = {
"host": "",
"port": 9091,
"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={}"
tc = transmissionrpc.Client(transmission_config['host'], port=transmission_config['port'], user=transmission_config['username'], password=transmission_config['password'])
def get_u2_torrents_hash():
torrents = list(filter(lambda x: 'dmhy' in x.trackers[0]['announce'], tc.get_torrents()))
u2_torrent_info_hash = []
for torrent in torrents:
u2_torrent_info_hash.append(
{"hash": torrent.hashString, "tracker": torrent.trackers[0]['announce'], "id": torrent.id}
)
return u2_torrent_info_hash
def main():
u2_torrent_info_hash = get_u2_torrents_hash()
if not os.path.exists("updated_torrents.json"):
cached_torrents = []
else:
with open("updated_torrents.json", "r") as fp:
cached_torrents = json.load(fp)
u2_torrent_info_hash = [
t for t in u2_torrent_info_hash if t["hash"] not in cached_torrents
]
batch_size = 100
print(f"找到了{len(u2_torrent_info_hash)}个未被更新的种子~")
updated_torrents = []
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 == 403:
print("APIKEY无效,请检查(注意APIKEY不是passkey)")
exit()
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 != 200:
print(f"意外的错误:{resp.status_code}")
response_data = resp.json()
for item in response_data:
if item.get("error"):
print(item.get("error"))
continue
tc.change_torrent(ids=_index[item["id"]]["id"], trackerReplace=(0, to_tracker.format(item["result"])))
updated_torrents.append(_index[item["id"]]["hash"])
with open("updated_torrents.json", "w") as fp:
json.dump(cached_torrents + updated_torrents, fp)
if __name__ == "__main__":
main()
@CNA-Bld
Copy link

CNA-Bld commented Aug 26, 2020

102 行之后需要更新一下 updated_torrents,否则不会保存已经替换过的种子~

@inertia42
Copy link
Author

102 行之后需要更新一下 updated_torrents,否则不会保存已经替换过的种子~

已修改,感谢提醒

@soleil0-0
Copy link

soleil0-0 commented Aug 30, 2020

36 行需要对 trackers 判空. 针对这个问题给 Loid 也提了个 PR

@inertia42
Copy link
Author

36 行需要对 trackers 判空. 针对这个问题给 Loid 也提了个 PR

感谢指出问题,之前确实没有考虑到这种可能性。不过现在已经有很多更好的替代品了,这个就这样吧。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment