forked from bartaz/snapcraft-flask
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
282 lines (228 loc) · 7.68 KB
/
app.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
"""
A Flask application for snapcraft.io.
The web frontend for the snap store.
"""
import flask
import requests
import requests_cache
import datetime
import humanize
import re
import bleach
import urllib
import pycountry
import os
import socket
from dateutil import parser, relativedelta
from requests.packages.urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
app = flask.Flask(__name__)
# Setup session to retry requests 5 times
uncached_session = requests.Session()
retries = Retry(
total=5,
backoff_factor=0.1,
status_forcelist=[500, 502, 503, 504]
)
uncached_session.mount(
'https://api.snapcraft.io',
HTTPAdapter(max_retries=retries)
)
# The cache expires after 5 seconds
cached_session = requests_cache.CachedSession(expire_after=5)
# Requests should timeout after 2 seconds in total
request_timeout = 2
# Request only stable snaps
snap_details_url = (
"https://api.snapcraft.io/api/v1/snaps/details/{snap_name}"
"?channel=stable"
)
details_query_headers = {
'X-Ubuntu-Series': '16',
'X-Ubuntu-Architecture': 'amd64',
}
snap_metrics_url = "https://api.snapcraft.io/api/v1/snaps/metrics"
metrics_query_headers = {
'Content-Type': 'application/json'
}
# Error handlers
# ===
@app.errorhandler(404)
def page_not_found(error):
"""
For 404 pages, display the 404.html template,
passing through the error description.
"""
return flask.render_template(
'404.html', description=error.description
), 404
# Global tasks for all requests
# ===
@app.after_request
def apply_caching(response):
response.headers["X-Commit-ID"] = os.getenv('COMMIT_ID')
response.headers["X-Hostname"] = socket.gethostname()
return response
# Redirects
# ===
@app.route('/docs/', defaults={'path': ''})
@app.route('/docs/<path:path>')
def docs_redirect(path):
return flask.redirect('https://docs.snapcraft.io/' + path)
@app.route('/community/')
def community_redirect():
return flask.redirect('/')
@app.route('/create/')
def create_redirect():
return flask.redirect('https://docs.snapcraft.io/build-snaps')
# Normal views
# ===
@app.route('/')
def homepage():
return flask.render_template('index.html')
@app.route('/<snap_name>/')
def snap_details(snap_name):
"""
A view to display the snap details page for specific snaps.
This queries the snapcraft API (api.snapcraft.io) and passes
some of the data through to the snap-details.html template,
with appropriate sanitation.
"""
today = datetime.datetime.utcnow().date()
month_ago = today - relativedelta.relativedelta(months=1)
details_response = _get_from_cache(
snap_details_url.format(snap_name=snap_name),
headers=details_query_headers
)
details = details_response.json()
if details_response.status_code >= 400:
message = (
'Failed to get snap details for {snap_name}'.format(**locals())
)
if details_response.status_code == 404:
message = 'Snap not found: {snap_name}'.format(**locals())
flask.abort(details_response.status_code, message)
metrics_query_json = [
{
"metric_name": "installed_base_by_country_percent",
"snap_id": details['snap_id'],
"start": month_ago.strftime('%Y-%m-%d'),
"end": today.strftime('%Y-%m-%d')
}
]
metrics_response = _get_from_cache(
snap_metrics_url.format(snap_name=snap_name),
headers=metrics_query_headers,
json=metrics_query_json
)
geodata = metrics_response.json()[0]['series']
# Normalise geodata from API
users_by_country = {}
for country_percentages in geodata:
country_code = country_percentages['name']
percentages = []
for daily_percent in country_percentages['values']:
if daily_percent is not None:
percentages.append(daily_percent)
if len(percentages) > 0:
users_by_country[country_code] = (
sum(percentages) / len(percentages)
)
else:
users_by_country[country_code] = None
# Build up country info for every country
country_data = {}
for country in pycountry.countries:
country_data[country.numeric] = {
'name': country.name,
'code': country.alpha_2,
'percentage_of_users': users_by_country.get(country.alpha_2)
}
description = details['description'].strip()
paragraphs = re.compile(r'[\n\r]{2,}').split(description)
formatted_paragraphs = []
# Sanitise paragraphs
def external(attrs, new=False):
url_parts = urllib.parse.urlparse(attrs[(None, "href")])
if url_parts.netloc and url_parts.netloc != 'snapcraft.io':
if (None, "class") not in attrs:
attrs[(None, "class")] = "p-link--external"
elif "p-link--external" not in attrs[(None, "class")]:
attrs[(None, "class")] += " p-link--external"
return attrs
for paragraph in paragraphs:
callbacks = bleach.linkifier.DEFAULT_CALLBACKS
callbacks.append(external)
paragraph = bleach.clean(paragraph, tags=[])
paragraph = bleach.linkify(paragraph, callbacks=callbacks)
formatted_paragraphs.append(paragraph)
context = {
# Data direct from details API
'snap_title': details['title'],
'package_name': details['package_name'],
'icon_url': details['icon_url'],
'version': details['version'],
'revision': details['revision'],
'license': details['license'],
'publisher': details['publisher'],
'screenshot_urls': details['screenshot_urls'],
'prices': details['prices'],
'support_url': details.get('support_url'),
'summary': details['summary'],
'description_paragraphs': formatted_paragraphs,
# Transformed API data
'filesize': humanize.naturalsize(details['binary_filesize']),
'last_updated': (
humanize.naturaldate(
parser.parse(details.get('last_updated'))
)
),
# Data from metrics API
'countries': country_data,
# Context info
'details_api_error': details_response.old_data_from_error,
'metrics_api_error': metrics_response.old_data_from_error,
'is_linux': 'Linux' in flask.request.headers['User-Agent']
}
return flask.render_template(
'snap-details.html',
**context
)
def _get_from_cache(url, headers, json=None):
"""
Retrieve the response from the requests cache.
If the cache has expired then it will attempt to update the cache.
If it gets an error, it will use the cached response, if it exists.
"""
request_error = False
method = "POST" if json else "GET"
request = cached_session.prepare_request(
requests.Request(
method=method,
url=url,
headers=headers,
json=json
)
)
cache_key = cached_session.cache.create_key(request)
response, timestamp = cached_session.cache.get_response_and_time(
cache_key
)
if response:
age = datetime.datetime.utcnow() - timestamp
if age > cached_session._cache_expire_after:
try:
new_response = uncached_session.send(
request,
timeout=request_timeout
)
if response.status_code >= 500:
new_response.raise_for_status()
except:
request_error = True
else:
response = new_response
else:
response = cached_session.send(request)
response.old_data_from_error = request_error
return response