forked from TeamNewPipe/web-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
289 lines (216 loc) · 8.54 KB
/
api.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
#! /usr/bin/env python3
import json
import logging
import os
import random
import re
import string
import tornado.httpclient
import tornado.ioloop
import tornado.locks
import tornado.options
import tornado.web
from datetime import datetime, timedelta
import yaml
from lxml import html
from tornado import gen
from raven.contrib.tornado import AsyncSentryClient, SentryMixin
def random_string(length=20):
alphabet = string.ascii_letters + string.digits + "_/-.;:#+*?()$[]!"
return "".join((random.choice(alphabet) for _ in range(length)))
def fetch(url: str):
request = tornado.httpclient.HTTPRequest(url, headers={
"User-Agent": ""
})
http_client = tornado.httpclient.AsyncHTTPClient()
return http_client.fetch(request, raise_error=True)
@gen.coroutine
def get_github_flavor(repo_name: str):
url = "https://github.com/TeamNewPipe/{}/releases/".format(repo_name)
html_string = (yield fetch(url)).body
document = html.fromstring(html_string)
@gen.coroutine
def get_version_str() -> str:
tags = document.cssselect(".release .float-left ul li a.css-truncate > span.css-truncate-target")
return tags[0].text
gradle_template = "https://raw.githubusercontent.com/TeamNewPipe/{}/{}/app/build.gradle"
@gen.coroutine
def get_version_code() -> int:
tags = document.cssselect(".release .float-left ul li a code")
repo_hash = tags[0].text
gradle_file_data = (yield fetch(gradle_template.format(repo_name, repo_hash))).body
if isinstance(gradle_file_data, bytes):
gradle_file_data = gradle_file_data.decode()
version_codes = re.findall("versionCode(.*)", gradle_file_data)
return int(version_codes[0].split(" ")[-1])
@gen.coroutine
def get_apk_url() -> str:
tags = document.cssselect('.release-main-section details a[href$=".apk"]')
return "https://github.com" + tags[0].get("href")
return {
"stable": (yield gen.multi({
"version": get_version_str(),
"version_code": get_version_code(),
"apk": get_apk_url(),
}))
}
@gen.coroutine
def get_fdroid_flavor(package_name: str):
template = "https://gitlab.com/fdroid/fdroiddata/raw/master/metadata/{}.yml"
url = template.format(package_name)
version_data = (yield fetch(url)).body
if isinstance(version_data, bytes):
version_data = version_data.decode()
data = yaml.safe_load(version_data)
latest_version = data["Builds"][-1]
version = latest_version["versionName"]
version_code = latest_version["versionCode"]
apk_template = "https://f-droid.org/repo/{}_{}.apk"
apk_url = apk_template.format(package_name, version_code)
return {
"stable": {
"version": version,
"version_code": version_code,
"apk": apk_url,
}
}
def assemble_flavors():
return gen.multi({
"github": get_github_flavor("NewPipe"),
"fdroid": get_fdroid_flavor("org.schabi.newpipe"),
"github_legacy": get_github_flavor("NewPipe-legacy"),
"fdroid_legacy": get_fdroid_flavor("org.schabi.newpipelegacy"),
})
class DataJsonHandler(tornado.web.RequestHandler, SentryMixin):
# 1 hour as a timeout is neither too outdated nor requires bothering GitHub
# too often
_timeout = timedelta(hours=1)
_error_timeout = timedelta(minutes=6)
# initialize with datetime that is outdated for sure
_last_request = (datetime.now() - 2 * _timeout)
# cache for last returned data
_cached_response = None
# request GitHub only once when multiple requests are made in parallel
_lock = tornado.locks.Lock()
# make sure to not send too many requests to the GitHub API to not trigger
# the rate limit
_last_failed_request = (datetime.now() - 2 * _timeout)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.logger = logging.getLogger("tornado.general")
def add_default_headers(self):
self.add_header("Content-Type", "text/plain")
self.add_header("Access-Control-Allow-Origin", "newpipe.schabi.org")
@classmethod
def is_request_outdated(cls):
now = datetime.now()
if cls._cached_response is None:
return True
if (now - cls._last_request) > cls._timeout:
return True
return False
@gen.coroutine
def get(self):
# ensure that timeout is respected
now = datetime.now()
if self.__class__._last_failed_request is not None and \
(now - self.__class__._last_failed_request) < self.__class__._error_timeout:
self.logger.log(logging.INFO,
"Request failed recently, waiting for timeout")
self.add_default_headers()
self.send_error(500)
return
yield self.__class__._lock.acquire()
if self.is_request_outdated():
yield self.assemble_fresh_response()
else:
self.add_default_headers()
self.write(self._cached_response)
yield self.__class__._lock.release()
@gen.coroutine
def assemble_stats(self):
repo_url = "https://api.github.com/repos/TeamNewPipe/NewPipe"
contributors_url = "https://github.com/TeamNewPipe/NewPipe"
translations_url = "https://hosted.weblate.org/api/components/newpipe/" \
"strings/translations/"
repo_data, contributors_data, translations_data = \
[x.body for x in (yield gen.multi((
fetch(repo_url),
fetch(contributors_url),
fetch(translations_url),
)))]
repo = json.loads(repo_data.decode())
translations = json.loads(translations_data.decode())
# no idea why, but sometimes we receive different responses from GitHub
# might be some annoying A/B testing
# therefore we make this more fault-tolerant by sending a negative value if we can't fetch the data from GitHub
document = html.fromstring(contributors_data)
tags = document.cssselect(".numbers-summary a[href$=contributors] .num")
try:
contributors = int(tags[0].text)
except:
# log exception to sentry
self.captureException(exc_info=True)
# log entire response body to file
with open("/tmp/failed-contributors-response.{}.txt".format(datetime.now().isoformat()), "wb") as f:
f.write(contributors_data)
contributors = -1
return {
"stargazers": repo["stargazers_count"],
"watchers": repo["subscribers_count"],
"forks": repo["forks_count"],
"contributors": contributors,
"translations": int(translations["count"]),
}
@gen.coroutine
def assemble_fresh_response(self):
self.logger.log(logging.INFO, "Fetching latest release from GitHub")
data = None
# prove me wrong!
failure = True
try:
data = yield gen.multi({
"stats": self.assemble_stats(),
"flavors": assemble_flavors()
})
except tornado.httpclient.HTTPError as error:
yield gen.Task(self.captureException, "API error")
response = error.response
self.logger.log(
logging.ERROR,
"API error: {} -> {} ({})".format(response.effective_url, response.error, response.body),
)
except:
self.logger.exception("Unknown error occured, see next line")
yield gen.Task(self.captureException, exc_info=True)
else:
failure = False
if failure:
self.__class__._last_failed_request = datetime.now()
self.send_error(500)
return False
self.update_cache(data)
self.add_default_headers()
self.write(data)
self.finish()
@classmethod
def update_cache(cls, data):
cls._cached_response = data
now = datetime.now()
cls._last_request = now
def make_app():
app = tornado.web.Application([
(r"/data.json", DataJsonHandler),
])
sentry_url = os.environ.get("SENTRY_URL", None)
if sentry_url is not None:
print("Setting up Sentry integration")
app.sentry_client = AsyncSentryClient(sentry_url)
else:
app.sentry_client = AsyncSentryClient()
return app
if __name__ == "__main__":
tornado.options.parse_command_line()
app = make_app()
app.listen(3000)
tornado.ioloop.IOLoop.current().start()