-
-
Notifications
You must be signed in to change notification settings - Fork 209
/
index.py
340 lines (263 loc) · 10.8 KB
/
index.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import locale
import os
import requests
from brainzutils import cache
from datetime import datetime
from dateutil.relativedelta import relativedelta
from typing import List
from flask import Blueprint, render_template, current_app, redirect, url_for, request, jsonify
from flask_login import current_user, login_required
from requests.exceptions import HTTPError
import orjson
from werkzeug.exceptions import Unauthorized, NotFound
import listenbrainz.db.user as db_user
from data.model.user_entity import EntityRecord
from listenbrainz.db.exceptions import DatabaseException
from listenbrainz.webserver.decorators import web_listenstore_needed
from listenbrainz.webserver import flash, db_conn
from listenbrainz.webserver.timescale_connection import _ts
from listenbrainz.webserver.redis_connection import _redis
from listenbrainz.webserver.views.user import delete_user
import listenbrainz.db.stats as db_stats
index_bp = Blueprint('index', __name__)
locale.setlocale(locale.LC_ALL, '')
STATS_PREFIX = 'listenbrainz.stats' # prefix used in key to cache stats
CACHE_TIME = 10 * 60 # time in seconds we cache the stats
NUMBER_OF_RECENT_LISTENS = 50
SEARCH_USER_LIMIT = 100 # max number of users to return in search username results
@index_bp.route("/")
def index():
if current_user.is_authenticated and request.args.get("redirect", "true") == "true":
return redirect(url_for("user.profile", user_name=current_user.musicbrainz_id))
if _ts:
try:
listen_count = _ts.get_total_listen_count()
except Exception as e:
current_app.logger.error('Error while trying to get total listen count: %s', str(e))
listen_count = 0
else:
listen_count = 0
artist_count = 0
try:
artist_stats = db_stats.get(db_stats.SITEWIDE_STATS_USER_ID, "artists", "all_time", EntityRecord)
if artist_stats is not None:
artist_count = artist_stats.count
except Exception as e:
current_app.logger.error('Error while trying to get total artist count: %s', str(e))
props = {
"listen_count": listen_count,
"artist_count": artist_count,
}
return render_template(
"index/index.html",
props=orjson.dumps(props).decode("utf-8")
)
@index_bp.route("/import/")
def import_data():
if current_user.is_authenticated:
return redirect(url_for("settings.index", path='import/'))
else:
return current_app.login_manager.unauthorized()
@index_bp.route("/download/")
def downloads():
return redirect(url_for('index.data'))
@index_bp.route("/data/")
def data():
return render_template("index/data.html",active_about_section="using-data")
@index_bp.route("/add-data/")
def add_data_info():
return render_template("index/add-data.html", active_about_section="add-listens")
@index_bp.route("/import-data/")
def import_data_info():
return render_template("index/import-data.html")
@index_bp.route("/lastfm-proxy/")
def proxy():
return render_template("index/lastfm-proxy.html")
@index_bp.route("/about/")
def about():
return render_template("index/about.html", active_about_section="about")
@index_bp.route("/terms-of-service/")
def terms_of_service():
return render_template("index/terms-of-service.html", active_about_section="terms-of-service")
@index_bp.route("/blog-data/")
def blog_data():
"""Proxy to the MetaBrainz blog to get recent posts so that user IP addresses are not leaked to wordpress"""
cache_key = "blog-feed"
cache_blog_expires = 60*60
cached_blog = cache.get(cache_key)
if cached_blog:
return jsonify(cached_blog)
url = "https://public-api.wordpress.com/rest/v1.1/sites/blog.metabrainz.org/posts/"
try:
r = requests.get(url, timeout=5)
r.raise_for_status()
blog_data = r.json()
cache.set(cache_key, blog_data, cache_blog_expires)
return jsonify(blog_data)
except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError):
return jsonify({}), 503
@index_bp.route("/current-status/")
@web_listenstore_needed
def current_status():
load = "%.2f %.2f %.2f" % os.getloadavg()
listen_count = _ts.get_total_listen_count()
try:
user_count = format(int(_get_user_count()), ',d')
except DatabaseException as e:
user_count = 'Unknown'
listen_counts_per_day: List[dict] = []
for delta in range(2):
try:
day = datetime.utcnow() - relativedelta(days=delta)
day_listen_count = _redis.get_listen_count_for_day(day)
except:
current_app.logger.error("Could not get %s listen count from redis", day.strftime('%Y-%m-%d'), exc_info=True)
day_listen_count = None
listen_counts_per_day.append({
"date": day.strftime('%Y-%m-%d'),
"listen_count": format(day_listen_count, ',d') if day_listen_count else "0",
"label": "today" if delta == 0 else "yesterday",
})
return render_template(
"index/current-status.html",
load=load,
listen_count=format(int(listen_count), ",d") if listen_count else "0",
user_count=user_count,
listen_counts_per_day=listen_counts_per_day,
active_about_section="site-status"
)
@index_bp.route("/recent/")
def recent_listens():
recent = []
for listen in _redis.get_recent_listens(NUMBER_OF_RECENT_LISTENS):
recent.append({
"track_metadata": listen.data,
"user_name" : listen.user_name,
"listened_at": listen.ts_since_epoch,
"listened_at_iso": listen.timestamp.isoformat() + "Z",
})
listen_count = _ts.get_total_listen_count()
try:
user_count = format(int(_get_user_count()), ',d')
except DatabaseException as e:
user_count = 'Unknown'
props = {
"listens": recent,
"globalListenCount": listen_count,
"globalUserCount": user_count
}
return render_template("index/recent.html", props=orjson.dumps(props).decode("utf-8"))
@index_bp.route('/feed/', methods=['GET', 'OPTIONS'])
@login_required
@web_listenstore_needed
def feed():
return render_template('index/feed.html')
@index_bp.route('/agree-to-terms/', methods=['GET', 'POST'])
@login_required
def gdpr_notice():
if request.method == 'GET':
return render_template('index/gdpr.html', next=request.args.get('next'))
elif request.method == 'POST':
if request.form.get('gdpr-options') == 'agree':
try:
db_user.agree_to_gdpr(db_conn, current_user.musicbrainz_id)
except DatabaseException as e:
flash.error('Could not store agreement to GDPR terms')
next = request.form.get('next')
if next:
return redirect(next)
return redirect(url_for('index.index'))
elif request.form.get('gdpr-options') == 'disagree':
return redirect(url_for('settings.index', path='delete'))
else:
flash.error('You must agree to or decline our terms')
return render_template('index/gdpr.html', next=request.args.get('next'))
@index_bp.route('/search/', methods=['GET', 'OPTIONS'])
def search():
search_term = request.args.get("search_term")
user_id = current_user.id if current_user.is_authenticated else None
if search_term:
users = db_user.search(db_conn, search_term, SEARCH_USER_LIMIT, user_id)
else:
users = []
return render_template("index/search-users.html", search_term=search_term, users=users)
@index_bp.route('/messybrainz/', methods=['GET', 'OPTIONS'])
def messybrainz():
return render_template("index/messybrainz.html")
@index_bp.route('/delete-user/<int:musicbrainz_row_id>')
def mb_user_deleter(musicbrainz_row_id):
""" This endpoint is used by MusicBrainz to delete accounts once they
are deleted on MusicBrainz too.
See https://tickets.metabrainz.org/browse/MBS-9680 for details.
Args: musicbrainz_row_id (int): the MusicBrainz row ID of the user to be deleted.
Returns: 200 if the user has been successfully found and deleted from LB
Raises:
NotFound if the user is not found in the LB database
Unauthorized if the MusicBrainz access token provided with the query is invalid
"""
_authorize_mb_user_deleter(request.args.get('access_token', ''))
user = db_user.get_by_mb_row_id(db_conn, musicbrainz_row_id)
if user is None:
raise NotFound('Could not find user with MusicBrainz Row ID: %d' % musicbrainz_row_id)
delete_user(user['id'])
return jsonify({'status': 'ok'}), 200
def _authorize_mb_user_deleter(auth_token):
headers = {'Authorization': 'Bearer {}'.format(auth_token)}
r = requests.get(current_app.config['MUSICBRAINZ_OAUTH_URL'], headers=headers)
try:
r.raise_for_status()
except HTTPError:
raise Unauthorized('Not authorized to use this view')
data = {}
try:
data = r.json()
except ValueError:
raise Unauthorized('Not authorized to use this view')
try:
# 2007538 is the row ID of the `UserDeleter` account that is
# authorized to access the `delete-user` endpoint
if data['sub'] != 'UserDeleter' or data['metabrainz_user_id'] != 2007538:
raise Unauthorized('Not authorized to use this view')
except KeyError:
raise Unauthorized('Not authorized to use this view')
def _get_user_count():
""" Gets user count from either the brainzutils cache or from the database.
If not present in the cache, it makes a query to the db and stores the
result in the cache for 10 minutes.
"""
user_count_key = "{}.{}".format(STATS_PREFIX, 'user_count')
user_count = cache.get(user_count_key, decode=False)
if user_count:
return user_count
else:
try:
user_count = db_user.get_user_count(db_conn)
except DatabaseException as e:
raise
cache.set(user_count_key, int(user_count), CACHE_TIME, encode=False)
return user_count
@index_bp.route("/similar-users/")
def similar_users():
return redirect(url_for("explore.similar_users"))
@index_bp.route("/listens-offline/")
def listens_offline():
"""
Show the "listenstore offline" message.
"""
return render_template("index/listens_offline.html")
@index_bp.route("/musicbrainz-offline/")
def musicbrainz_offline():
""" Show the "musicbrainz offline" message. """
return render_template("index/musicbrainz-offline.html")
@index_bp.route("/huesound/")
def huesound():
""" Redirect to /explore/huesound """
return redirect(url_for("explore.huesound"))
@index_bp.route("/statistics/charts/")
def charts():
""" Show the top sitewide entities. """
return render_template("index/charts.html")
@index_bp.route("/statistics/")
def stats():
""" Show sitewide stats """
return render_template("index/stats.html")