forked from metabrainz/listenbrainz-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.py
229 lines (192 loc) · 8.21 KB
/
user.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
# coding=utf-8
#
# listenbrainz-server - Server for the ListenBrainz project
# Copyright (C) 2017 Param Singh
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import listenbrainz.config as config
from listenbrainz import stats
def get_top_recordings(musicbrainz_id, time_interval=None):
""" Get top recordings of user with given MusicBrainz ID over a particular time interval
Args:
musicbrainz_id (str): The MusicBrainz ID of the user
time_interval (str): Interval in the BigQuery interval format
which can be seen here:
https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#timestamp_sub
Returns:
sorted list of the following format
[
{
'track_name' (str)
'recording_msid' (uuid)
'recording_mbid' (uuid)
'artist_name' (str)
'artist_msid' (uuid)
'artist_mbids' (string of comma-seperated uuids)
'listen_count' (int)
}
]
"""
filter_clause = ""
if time_interval:
filter_clause = "AND listened_at >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL {})".format(time_interval)
query = """SELECT artist_name
, artist_msid
, artist_mbids
, recording_msid
, recording_mbid
, track_name
, COUNT(recording_msid) as listen_count
FROM {dataset_id}.{table_id}
WHERE user_name = @musicbrainz_id
{time_filter_clause}
GROUP BY recording_msid, recording_mbid, track_name, artist_name, artist_msid, artist_mbids
ORDER BY listen_count DESC
LIMIT {limit}
""".format(
dataset_id=config.BIGQUERY_DATASET_ID,
table_id=config.BIGQUERY_TABLE_ID,
time_filter_clause=filter_clause,
limit=config.STATS_ENTITY_LIMIT
)
# construct the parameters that must be passed to the Google BigQuery API
# start with a list of parameters to pass and then convert it to standard format
# required by Google BigQuery
parameters = [
{
"type": "STRING",
"name": "musicbrainz_id",
"value": musicbrainz_id,
},
]
return stats.run_query(query, parameters)
def get_top_artists(musicbrainz_id, time_interval=None):
""" Get top artists for user with given MusicBrainz ID over a particular period of time
Args: musicbrainz_id (str): the MusicBrainz ID of the user
time_interval (str): the time interval over which top artists should be returned
(defaults to all time)
Returns: A sorted list of dicts with the following structure
[
{
'artist_name' (str),
'artist_msid' (uuid),
'artist_mbids' (string of comma-seperated uuids),
'listen_count' (int),
}
]
"""
filter_clause = ""
if time_interval:
filter_clause = "AND listened_at >= TIMESTAMP_SUB(CURRENT_TIME(), INTERVAL {})".format(time_interval)
query = """SELECT artist_name
, artist_msid
, artist_mbids
, COUNT(artist_msid) as listen_count
FROM {dataset_id}.{table_id}
WHERE user_name = @musicbrainz_id
{time_filter_clause}
GROUP BY artist_msid, artist_name, artist_mbids
ORDER BY listen_count DESC
LIMIT {limit}
""".format(
dataset_id=config.BIGQUERY_DATASET_ID,
table_id=config.BIGQUERY_TABLE_ID,
time_filter_clause=filter_clause,
limit=config.STATS_ENTITY_LIMIT,
)
parameters = [
{
'type': 'STRING',
'name': 'musicbrainz_id',
'value': musicbrainz_id
}
]
return stats.run_query(query, parameters)
def get_artist_count(musicbrainz_id, time_interval=None):
""" Get artist count for user with given MusicBrainz ID over a particular period of time
Args: musicbrainz_id (str): the MusicBrainz ID of the user
time_interval (str): the time interval over which artist count should be returned
(defaults to all time)
Returns: artist_count (int): total number of artists listened to by the user in that
period of time
"""
filter_clause = ""
if time_interval:
filter_clause = "AND listened_at >= TIMESTAMP_SUB(CURRENT_TIME(), INTERVAL {})".format(time_interval)
query = """SELECT COUNT(DISTINCT(artist_msid)) as artist_count
FROM {dataset_id}.{table_id}
WHERE user_name = @musicbrainz_id
{time_filter_clause}
""".format(
dataset_id=config.BIGQUERY_DATASET_ID,
table_id=config.BIGQUERY_TABLE_ID,
time_filter_clause=filter_clause,
)
parameters = [
{
'name': 'musicbrainz_id',
'type': 'STRING',
'value': musicbrainz_id,
}
]
return stats.run_query(query, parameters)['listen_count']
def get_top_releases(musicbrainz_id, time_interval=None):
""" Get top releases for user with given MusicBrainz ID over a particular period of time
Args: musicbrainz_id (str): the MusicBrainz ID of the user
time_interval (str): the time interval over which top releases should be returned
(defaults to all time)
Returns: A sorted list of dicts with the following structure
[
{
'artist_name' (str),
'artist_msid' (uuid),
'artist_mbids' (string of comma seperated uuids),
'release_name' (str),
'release_msid' (uuid),
'release_mbid' (uuid),
'listen_count' (int)
}
]
"""
filter_clause = ""
if time_interval:
filter_clause = "AND listened_at >= TIMESTAMP_SUB(CURRENT_TIME(), INTERVAL {})".format(time_interval)
query = """SELECT artist_name
, artist_msid
, artist_mbids
, release_name
, release_msid
, release_mbid
, COUNT(release_msid) as listen_count
FROM {dataset_id}.{table_id}
WHERE user_name = @musicbrainz_id
{time_filter_clause}
GROUP BY artist_msid, artist_mbids, artist_name, release_name, release_msid, release_mbid
ORDER BY listen_count DESC
LIMIT {limit}
""".format(
dataset_id=config.BIGQUERY_DATASET_ID,
table_id=config.BIGQUERY_TABLE_ID,
time_filter_clause=filter_clause,
limit=config.STATS_ENTITY_LIMIT,
)
parameters = [
{
'type': 'STRING',
'name': 'musicbrainz_id',
'value': musicbrainz_id
}
]
return stats.run_query(query, parameters)