initial, and probably only, commit

This commit is contained in:
Lynne Megido 2020-08-20 19:57:20 +10:00
commit aacb4536ee
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
3 changed files with 77 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
config.json
__pycache__/

7
LICENSE Normal file
View file

@ -0,0 +1,7 @@
Copyright 2020 lynnesbian
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

68
main.py Executable file
View file

@ -0,0 +1,68 @@
#!/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!")