transckers/main.py

69 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python3
# a shitty python script to automatically add trackers to torrents in transmission
import requests
import json, sys
cfg = {
"url": "127.0.0.1",
"port": "9091",
"path": "/transmission/rpc",
"username": "",
"password": ""
}
try:
cfg.update(json.load(open("config.json", "r")))
except FileNotFoundError:
open("config.json", "w").write("{}")
rpc_path = f"http://{cfg['url']}:{cfg['port']}{cfg['path']}"
def do_rpc(method: str = "session-stats", arguments = None, updating_id = 0):
data = {
"arguments": arguments,
"method": method
}
r = s.post(rpc_path, json = data)
if r.status_code == 409:
# update headers and try again
if updating_id > 5:
print("Too many retries - exiting")
sys.exit(1)
print("Updating Transmission session ID...")
s.headers.update(
{'X-Transmission-Session-Id': r.headers.get('X-Transmission-Session-Id')}
)
return do_rpc(method, arguments, updating_id = updating_id + 1)
return r
s = requests.Session()
s.auth = (cfg['username'], cfg['password'])
s.headers.update(
{
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Transmission-Session-Id': 'henlo'
}
)
print("Downloading tracker list...")
trackers = requests.get('https://ngosang.github.io/trackerslist/trackers_best.txt').text.replace("\n\n", "\n").split()
print("Checking for incomplete torrents...")
targets = do_rpc("torrent-get", {"fields": ["id", "name", "status"]}).json()
targets = [info['id'] for info in targets['arguments']['torrents'] if info['status'] in [3, 4]]
print("Adding trackers...")
x = do_rpc("torrent-set", {"ids": targets, "trackerAdd": trackers})
print("Done!")