-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange-tracker.py
executable file
·58 lines (53 loc) · 2.1 KB
/
change-tracker.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
#!/usr/bin/python
#Quick and dirty script used to replace a tracker adress by another for a list of torrents on a Transmission server.
#Adapt the parameters in the main section to your installation of Transmission.
import httplib
import base64
import json
def getAuthorization(user,password):
credentials = base64.b64encode(b"{}:{}".format(user,password)).decode("ascii")
return { 'Authorization':'Basic {}'.format(credentials) }
def getToken(server,port,authorization):
headers = authorization
connection = httplib.HTTPConnection(server,port)
connection.request("GET","/transmission/rpc",headers=headers)
response = connection.getresponse()
return response.getheader("x-transmission-session-id")
if __name__ == '__main__':
#Adapt these parameters for your configuration !
SERVER = "127.0.0.1"
PORT = 9091
USER = "XXXX"
PASSWORD = "XXXX"
#The old tracker adress to replace
OLD_TRACKER = "XXXX"
#The new tracker to use
NEW_TRACKER = "XXXX"
authorization = getAuthorization(USER,PASSWORD)
token = getToken(SERVER,PORT,authorization)
headers = authorization
headers['X-Transmission-Session-Id'] = '{}'.format(token)
data = {}
data["method"] = "torrent-get"
data["arguments"] = {}
data["arguments"]["fields"] = ["id","trackers"]
encoded_data = json.dumps(data)
connection = httplib.HTTPConnection(SERVER,PORT)
connection.request("POST","/transmission/rpc",encoded_data,headers)
response = connection.getresponse()
decoded_response = json.load(response)
for torrent in decoded_response['arguments']['torrents']:
for tracker in torrent["trackers"]:
if tracker["announce"] == OLD_TRACKER:
print("For torrent {}, change tracker {}".format(torrent["id"],tracker["id"]))
data = {}
data["method"] = "torrent-set"
data["arguments"] = {}
data["arguments"]["id"] = torrent['id']
data["arguments"]["trackerReplace"] = (tracker["id"],NEW_TRACKER)
encoded_data = json.dumps(data)
print(encoded_data)
connection = httplib.HTTPConnection(SERVER,PORT)
connection.request("POST","/transmission/rpc",encoded_data,headers)
response = connection.getresponse()
print(str(response.read()))