This repository was archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_api_mirror.py
305 lines (239 loc) · 8.52 KB
/
create_api_mirror.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# -*- coding: utf-8 -*-
#
# This file is part of GetTor.
#
# :authors: Israel Leiva <[email protected]>
# see also AUTHORS file
#
# :copyright: (c) 2008-2015, The Tor Project, Inc.
# (c) 2015, Israel Leiva
#
# :license: This is Free Software. See LICENSE for license information.
import os
import re
import json
import codecs
import urllib2
import ConfigParser
from time import gmtime, strftime
"""Script to build a static mirror of GetTor RESTful API"""
# currently supported locales for Tor Browser
LC = ['ar', 'de', 'en-US', 'es-ES', 'fa', 'fr', 'it', 'ko', 'nl', 'pl',
'pt-PT', 'ru', 'tr', 'vi', 'zh-CN']
# https://gitweb.tpo/tor-browser-spec.git/tree/processes/VersionNumbers
# does not say anything about operating systems, so it's possible the
# notation might change in the future. We should always use the same three
# strings though: linux, windows, osx.
OS = {
'Linux': 'linux',
'Windows': 'windows',
'MacOS': 'osx'
}
# based on
# https://gitweb.tpo.org/tor-browser-spec.git/tree/processes/VersionNumbers
# except for the first one, which is based on current RecommendedTBBVersions
RE = {
'os': '(.*)-(\w+)',
'alpha': '\d\.\d(\.\d)*a\d+',
'beta': '\d\.\d(\.\d)*b\d+',
'stable': '\d\.\d(\.\d)*'
}
# strings to build names of packages depending on OS.
PKG = {
'windows': 'torbrowser-install-%s_%s.exe',
'linux': 'tor-browser-linux%s-%s_%s.tar.xz',
'osx': 'TorBrowser-%s-osx64_%s.dmg'
}
# bin and asc are used to build the download links for each version, os and lc
URL = {
'version': 'https://gettor.torproject.org/api/latest.json',
'mirrors': 'https://gettor.torproject.org/api/mirrors.json',
'providers': 'https://gettor.torproject.org/api/providers.json'
}
GETTOR_URL = 'http://gettor.torproject.org'
SERVER_URL = 'https://yourprivateserver.com'
API_TREE = '/home/ilv/Proyectos/tor/static_api/'
class ConfigError(Exception):
pass
class InternalError(Exception):
pass
class APIMirror(object):
""" Provide useful resources via RESTful API. """
def __init__(self, cfg=None):
self.url = SERVER_URL
self.tree = API_TREE
def _is_json(self, my_json):
""" Check if json generated is valid.
:param: my_json (string) data to ve verified.
:return: (bool) true if data is json-valid, false otherwise.
"""
try:
json_object = json.loads(my_json)
except ValueError, e:
return False
return True
def _write_json(self, path, content):
"""
"""
try:
with codecs.open(
path,
"w",
"utf-8"
) as jsonfile:
# Make pretty json
json.dump(
content,
jsonfile,
sort_keys=True,
indent=4,
separators=(',', ': '),
encoding="utf-8",
)
except IOError as e:
#logging.error("Couldn't write json: %s" % str(e))
print "Error building %s: %s" % (path, str(e))
print "%s built" % path
def _get_provider_name(self, p):
""" Return simplified version of provider's name.
:param: p (string) provider's name.
:return: (string) provider's name in lowercase and without spaces.
"""
p = p.replace(' ', '-')
return p.lower()
def _load_latest_version(self):
""" Load latest version data from GetTor API. """
response = urllib2.urlopen(URL['version'])
json_response = json.load(response)
self.lv = json_response
def _load_links(self):
""" Load links and providers data. """
response = urllib2.urlopen(URL['providers'])
json_response = json.load(response)
links = {}
for provider in json_response:
if provider != 'updated_at':
provider_url = json_response[provider]
provider_response = urllib2.urlopen("%s.json" % provider_url)
provider_content = json.load(provider_response)
json_response[provider] = provider_url.replace(
GETTOR_URL,
self.url
)
links[provider] = provider_content
self.providers = json_response
self.links = links
def _load_mirrors(self):
""" Load mirrors data from GetTor API. """
response = urllib2.urlopen(URL['mirrors'])
json_response = json.load(response)
self.mirrors = json_response
def _load_resources(self):
""" Load available resources data. """
self.resources = {
'providers': '%s/providers' % self.url,
'mirrors': '%s/mirrors' % self.url,
'latest_version': '%s/latest' % self.url,
'updated_at': strftime("%Y-%m-%d %H:%M:%S", gmtime())
}
def load_data(self):
""" Load all data."""
self._load_links()
self._load_mirrors()
self._load_resources()
self._load_latest_version()
def build(self):
""" Build RESTful API. """
print "Building API mirror"
# resources
self._write_json(
os.path.join(self.tree, 'api'),
self.resources
)
api_path = os.path.join(self.tree, 'api-content')
if not os.path.isdir(api_path):
os.mkdir(api_path)
# providers
self._write_json(
os.path.join(api_path, 'providers'),
self.providers
)
providers_path = os.path.join(api_path, 'providers-content')
if not os.path.isdir(providers_path):
os.mkdir(providers_path)
for provider in self.links:
if provider == 'updated_at':
continue
self._write_json(
os.path.join(providers_path, provider),
self.links[provider]
)
provider_path = os.path.join(
providers_path,
"%s-content" % provider
)
if not os.path.isdir(provider_path):
os.mkdir(provider_path)
for osys in self.links[provider]:
self._write_json(
os.path.join(provider_path, osys),
self.links[provider][osys]
)
provider_os_path = os.path.join(
provider_path, "%s-content" % osys
)
if not os.path.isdir(provider_os_path):
os.mkdir(provider_os_path)
for lc in self.links[provider][osys]:
self._write_json(
os.path.join(provider_os_path, lc),
self.links[provider][osys][lc]
)
# latest version
self._write_json(
os.path.join(api_path, 'latest'),
self.lv
)
lv_path = os.path.join(api_path, 'latest-content')
if not os.path.isdir(lv_path):
os.mkdir(lv_path)
for release in self.lv:
if release == 'updated_at':
continue
self._write_json(
os.path.join(lv_path, release),
self.lv[release]
)
release_path = os.path.join(
lv_path,
"%s-content" % release
)
if not os.path.isdir(release_path):
os.mkdir(release_path)
for osys in self.lv[release]['downloads']:
self._write_json(
os.path.join(release_path, osys),
self.lv[release]['downloads'][osys]
)
release_os_path = os.path.join(
release_path,
"%s-content" % osys
)
if not os.path.isdir(release_os_path):
os.mkdir(release_os_path)
for lc in self.lv[release]['downloads'][osys]:
self._write_json(
os.path.join(release_os_path, lc),
self.lv[release]['downloads'][osys][lc]
)
# mirrors
self._write_json(
os.path.join(api_path, 'mirrors'),
self.mirrors
)
def main():
api = APIMirror()
api.load_data()
api.build()
if __name__ == '__main__':
main()