/
update_tracker.py
executable file
·205 lines (174 loc) · 6.09 KB
/
update_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import configparser
import hmac
import platform
from urllib.parse import urlparse, urlunparse, quote
import requests
import time
MAFREEBOX_API_URL = 'http://mafreebox.freebox.fr/api/v3/'
CONFIG_FILE = '.fbxconfig'
APP_ID = 't411-tracker-update'
APP_VERSION = '0.0.1'
OLD_TRACKER_HOSTS = (
't411.download:56969',
'tracker.t411.me:56969',
'tracker.t411.me:8880',
'tracker.t411.io:56969',
'tracker.t411.io:8880',
'46.246.117.194:56969',
)
NEW_TRACKER_HOST = 't411.download'
def get_api_result(rep):
if rep.status_code != 200:
print("http request failed %d / %s" % (rep.status_code, rep.content))
exit(1)
try:
res = rep.json()
except ValueError as e:
print("failed to parse response: %s / %s" % (rep.content, e))
exit(1)
return
if 'success' not in res or not res['success']:
print("failed to parse response")
exit(1)
if 'result' in res:
return res['result']
return None
def request_token():
payload = {
'app_id': APP_ID,
'app_name': 'T411 tracker updater',
'app_version': APP_VERSION,
'device_name': platform.node(),
}
rep = requests.post(MAFREEBOX_API_URL + "login/authorize/", json=payload)
result = get_api_result(rep)
if 'app_token' not in result or 'track_id' not in result:
print("Malformed response %s" % rep.content)
exit(1)
app_token = result['app_token']
track_id = result['track_id']
print("Please press the button on the freebox front panel to grand access to freebox config ...")
while True:
time.sleep(2)
print(" ... checking auth status ...")
rep = requests.get(MAFREEBOX_API_URL + "login/authorize/%d" % track_id)
result = get_api_result(rep)
print("result: %s" % result)
if 'status' not in result:
print("unable to get auth status %s" % result)
exit(1)
status = result['status']
if status == 'pending':
continue
if status == 'timeout':
print("... too late. you need to press the button on the freebox front panel !!!")
exit(1)
if status == 'granted':
print("... OK got app_token %s" % app_token)
return app_token
print("unexpected status %s" % status)
exit(1)
def get_freebox_token():
config = configparser.ConfigParser()
config.read(CONFIG_FILE)
app_token = None
if 'freebox' in config and 'app_token' in config['freebox']:
app_token = config['freebox']['app_token']
if not app_token:
print("need to request a token")
app_token = request_token()
# save in conf
config['freebox'] = {}
config['freebox']['app_token'] = app_token
with open(CONFIG_FILE, 'w') as configfile:
config.write(configfile)
return app_token
def get_challenge():
rep = requests.get(MAFREEBOX_API_URL + "login/")
res = get_api_result(rep)
if 'challenge' not in res:
print("failed to get challenge %s" % res)
exit(1)
return res['challenge']
def open_session(app_token):
challenge = get_challenge()
password = hmac.new(app_token.encode('utf-8'), msg=challenge.encode('utf-8'), digestmod='sha1').hexdigest()
rep = requests.post(MAFREEBOX_API_URL + "login/session/", json={
'password': password,
'app_id': APP_ID,
'app_verion': APP_VERSION,
})
res = get_api_result(rep)
if 'session_token' not in res:
print("failed to get session token %s" % res)
exit(1)
return res['session_token']
def get_downloads(session_token):
rep = requests.get(MAFREEBOX_API_URL + "downloads/", headers={
'X-Fbx-App-Auth': session_token
})
res = get_api_result(rep)
return res
def get_download_trackers(session_token, download):
rep = requests.get(MAFREEBOX_API_URL + "downloads/%d/trackers" % download['id'], headers={
'X-Fbx-App-Auth': session_token
})
res = get_api_result(rep)
return res
def tracker_need_update(announce_url):
parts = urlparse(announce_url)
if parts.netloc in OLD_TRACKER_HOSTS:
return True
return False
def update_tracker(session_token, download_id, tracker):
announce_url = tracker['announce']
parts = list(urlparse(announce_url))
parts[1] = NEW_TRACKER_HOST
new_announce = urlunparse(parts)
print("> UPDATE tracker %s ==> %s" % (announce_url, new_announce))
# add new tracker
url = MAFREEBOX_API_URL + ("downloads/%d/trackers" % download_id)
rep = requests.post(url, json={
'announce': new_announce,
'is_enabled': True
}, headers={
'X-Fbx-App-Auth': session_token
})
get_api_result(rep)
# remove prev tracker
url = MAFREEBOX_API_URL + ("downloads/%d/trackers/%s" % (download_id, quote(announce_url, safe='')))
rep = requests.delete(url, headers={
'X-Fbx-App-Auth': session_token
})
get_api_result(rep)
# active new tracker
url = MAFREEBOX_API_URL + ("downloads/%d/trackers/%s" % (download_id, quote(new_announce, safe='')))
rep = requests.delete(url, json={
'is_enabled': True
}, headers={
'X-Fbx-App-Auth': session_token
})
get_api_result(rep)
def update_trackers():
print("Getting app token")
app_token = get_freebox_token()
print("App token: %s" % app_token)
print("Opening session ...")
session_token = open_session(app_token)
print("got session token: %s" % session_token)
print("getting download list")
for d in get_downloads(session_token):
if 'type' not in d or d['type'] != 'bt':
print("> skip %s (not a torrent)" % d['name'])
continue
print("> processing torrent %s" % d['name'])
for t in get_download_trackers(session_token, d):
announce_url = t['announce']
if tracker_need_update(announce_url):
update_tracker(session_token, d['id'], t)
else:
print("> KEEP tracker %s" % announce_url)
if __name__ == '__main__':
update_trackers()