· 4 years ago · Jul 12, 2021, 09:22 PM
1from clutch import Client
2import json
3import requests
4import urllib.request
5
6GGn_API_URL = "https://gazellegames.net/api.php"
7GGn_API_KEY = "PUT YOUR API KEY HERE"
8GGn_HEADER = {"X-API-Key": GGn_API_KEY}
9TORRENT_DIR = '' # leave empty to download to current dir
10
11# update following for your transmission server
12TRANSMISSION_HOST = "http://localhost:9091/transmission/rpc"
13
14
15def trumpReplacer():
16 client = Client(address=TRANSMISSION_HOST)
17 response = client.torrent.accessor(all_fields=True)
18
19 count = 1
20 print("All Torrents:")
21 for torrent in response.arguments.torrents:
22 print(count, torrent.status.name, torrent.name)
23 count += 1
24
25 print("Seeding Torrents:")
26 count = 1
27 for torrent in response.arguments.torrents:
28 if torrent.status.name == 'SEED':
29 print(count, torrent.status.name, torrent.name)
30 count += 1
31
32 print("Error Torrents:")
33 count = 1
34 for torrent in response.arguments.torrents:
35 # torrent.error is not dependant on torrent.status
36 if torrent.error != 0:
37 print(count, torrent.status.name, torrent.download_dir, torrent.name, torrent.error, torrent.error_string)
38 if "trump" in torrent.error_string.lower():
39 print("Torrent has been trumped! Try to replace this!")
40
41 # use GGn api to find latest torrent
42 if 'gazellegames' in torrent.comment:
43 # check api status
44 response1 = requests.get(GGn_API_URL, headers=GGn_HEADER)
45 api_info = json.loads(response1.content)
46 if api_info['status'] == 'success':
47
48 # check user_info
49 response2 = requests.get(GGn_API_URL + '?request=quick_user', headers=GGn_HEADER)
50 user_info = json.loads(response2.content)
51 if user_info['status'] == 'success':
52
53 # needed for generating download url?
54 authkey = user_info['response']['authkey']
55 passkey = user_info['response']['passkey']
56
57 # torrent.comment like 'https://gazellegames.net/torrents.php?torrentid=153127'
58 torrent_id = torrent.comment.split('torrentid=')[-1]
59 payload = {'id': torrent_id}
60 response3 = requests.get(GGn_API_URL + '?request=torrent', params=payload, headers=GGn_HEADER)
61 new_torrent_info = json.loads(response3.content)
62
63 if new_torrent_info['status'] == 'success':
64 # new version exists. Download it.
65 download_url = "https://gazellegames.net/torrents.php?action=download&id=" + torrent_id + \
66 "&authkey=" + authkey + \
67 "&torrent_pass=" + passkey
68 new_torrent_file = TORRENT_DIR + torrent.name + ".torrent"
69 urllib.request.urlretrieve(download_url, new_torrent_file)
70 print("downloaded new torrent!")
71
72 # found new torrent before deleting old
73 delete_response = client.torrent.remove(torrent.id, delete_local_data=False)
74
75 new_torrent_arguments = {
76 "download_dir": torrent.download_dir, # keep same download folder
77 "filename": new_torrent_file,
78 "paused": True,
79 }
80 add_response = client.torrent.add(new_torrent_arguments)
81 count += 1
82
83
84# Press the green button in the gutter to run the script.
85if __name__ == '__main__':
86 trumpReplacer()
87
88