-
Notifications
You must be signed in to change notification settings - Fork 0
/
transmission_maintenance.py
68 lines (55 loc) · 2.28 KB
/
transmission_maintenance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# gets the list of torrents that ought to be in progress, gets the list of torrents that are in progress,
# and resolves any discrepancies.
# transmission-daemon is expected to have rpc available at http://localhost:9091/transmission/rpc
import requests
from config import config
import os
import sys
import json
import base64
assert 'all_torrents_url' in config
assert 'torrent_url' in config
if 'server_ca' in config:
os.environ['REQUESTS_CA_BUNDLE'] = config['server_ca']
cert = config['client_cert'] if 'client_cert' in config else None
# get the list of current (active) torrents.
r = requests.get(config['all_torrents_url'], cert=cert)
active_torrents = set(r.text.splitlines())
active_torrents.discard('') # ensure any empty lines get thrown out
r2 = requests.post('http://localhost:9091/transmission/rpc')
headers = {'X-Transmission-Session-Id': r2.headers['X-Transmission-Session-Id']}
r2 = requests.post('http://localhost:9091/transmission/rpc', headers=headers,
json={'method': 'torrent-get', 'arguments': {'fields': ['hashString']}})
current_torrents = set([t['hashString'] for t in r2.json()['arguments']['torrents']])
to_remove = current_torrents - active_torrents
to_add = active_torrents - current_torrents
if len(active_torrents) < 100:
# fewer than 100 torrents? not on MY server.
print('Too few torrents from server. Not making changes', file=sys.stderr)
sys.exit(1)
print(json.dumps({'to_add': list(to_add), 'to_remove': list(to_remove)}, indent=4))
if current_torrents and len(to_remove)/len(current_torrents) > 0.1:
print('Tried to remove >10% of current torrents. Bailing', file=sys.stderr)
sys.exit(1)
transmission_message = {
'method': 'torrent-remove',
'arguments': {
'ids': list(to_remove),
'delete-local-data': True
}
}
r2 = requests.post('http://localhost:9091/transmission/rpc', headers=headers,
json=transmission_message)
for hashString in to_add:
r = requests.get(config['torrent_url'].format(hash=hashString), cert=cert)
data = r.content
if r.status_code != 200 or not data:
print('Invalid response for hash %s' % hashString, file=sys.stderr)
continue
message = {
'method': 'torrent-add',
'arguments': {
'metainfo': base64.b64encode(data).decode('ascii')
}
}
r2 = requests.post('http://localhost:9091/transmission/rpc', headers=headers, json=message)