-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
lastimport.py
292 lines (251 loc) · 9.19 KB
/
lastimport.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
# This file is part of beets.
# Copyright 2016, Rafael Bodill https://github.com/rafi
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
import pylast
from pylast import TopItem, _extract, _number
from beets import config, dbcore, plugins, ui
from beets.dbcore import types
API_URL = "https://ws.audioscrobbler.com/2.0/"
class LastImportPlugin(plugins.BeetsPlugin):
def __init__(self):
super().__init__()
config["lastfm"].add(
{
"user": "",
"api_key": plugins.LASTFM_KEY,
}
)
config["lastfm"]["api_key"].redact = True
self.config.add(
{
"per_page": 500,
"retry_limit": 3,
}
)
self.item_types = {
"play_count": types.INTEGER,
}
def commands(self):
cmd = ui.Subcommand("lastimport", help="import last.fm play-count")
def func(lib, opts, args):
import_lastfm(lib, self._log)
cmd.func = func
return [cmd]
class CustomUser(pylast.User):
"""Custom user class derived from pylast.User, and overriding the
_get_things method to return MBID and album. Also introduces new
get_top_tracks_by_page method to allow access to more than one page of top
tracks.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def _get_things(
self, method, thing, thing_type, params=None, cacheable=True
):
"""Returns a list of the most played thing_types by this thing, in a
tuple with the total number of pages of results. Includes an MBID, if
found.
"""
doc = self._request(self.ws_prefix + "." + method, cacheable, params)
toptracks_node = doc.getElementsByTagName("toptracks")[0]
total_pages = int(toptracks_node.getAttribute("totalPages"))
seq = []
for node in doc.getElementsByTagName(thing):
title = _extract(node, "name")
artist = _extract(node, "name", 1)
mbid = _extract(node, "mbid")
playcount = _number(_extract(node, "playcount"))
thing = thing_type(artist, title, self.network)
thing.mbid = mbid
seq.append(TopItem(thing, playcount))
return seq, total_pages
def get_top_tracks_by_page(
self, period=pylast.PERIOD_OVERALL, limit=None, page=1, cacheable=True
):
"""Returns the top tracks played by a user, in a tuple with the total
number of pages of results.
* period: The period of time. Possible values:
o PERIOD_OVERALL
o PERIOD_7DAYS
o PERIOD_1MONTH
o PERIOD_3MONTHS
o PERIOD_6MONTHS
o PERIOD_12MONTHS
"""
params = self._get_params()
params["period"] = period
params["page"] = page
if limit:
params["limit"] = limit
return self._get_things(
"getTopTracks", "track", pylast.Track, params, cacheable
)
def import_lastfm(lib, log):
user = config["lastfm"]["user"].as_str()
per_page = config["lastimport"]["per_page"].get(int)
if not user:
raise ui.UserError("You must specify a user name for lastimport")
log.info("Fetching last.fm library for @{0}", user)
page_total = 1
page_current = 0
found_total = 0
unknown_total = 0
retry_limit = config["lastimport"]["retry_limit"].get(int)
# Iterate through a yet to be known page total count
while page_current < page_total:
log.info(
"Querying page #{0}{1}...",
page_current + 1,
f"/{page_total}" if page_total > 1 else "",
)
for retry in range(0, retry_limit):
tracks, page_total = fetch_tracks(user, page_current + 1, per_page)
if page_total < 1:
# It means nothing to us!
raise ui.UserError("Last.fm reported no data.")
if tracks:
found, unknown = process_tracks(lib, tracks, log)
found_total += found
unknown_total += unknown
break
else:
log.error("ERROR: unable to read page #{0}", page_current + 1)
if retry < retry_limit:
log.info(
"Retrying page #{0}... ({1}/{2} retry)",
page_current + 1,
retry + 1,
retry_limit,
)
else:
log.error(
"FAIL: unable to fetch page #{0}, ",
"tried {1} times",
page_current,
retry + 1,
)
page_current += 1
log.info("... done!")
log.info("finished processing {0} song pages", page_total)
log.info("{0} unknown play-counts", unknown_total)
log.info("{0} play-counts imported", found_total)
def fetch_tracks(user, page, limit):
"""JSON format:
[
{
"mbid": "...",
"artist": "...",
"title": "...",
"playcount": "..."
}
]
"""
network = pylast.LastFMNetwork(api_key=config["lastfm"]["api_key"])
user_obj = CustomUser(user, network)
results, total_pages = user_obj.get_top_tracks_by_page(
limit=limit, page=page
)
return [
{
"mbid": track.item.mbid if track.item.mbid else "",
"artist": {"name": track.item.artist.name},
"name": track.item.title,
"playcount": track.weight,
}
for track in results
], total_pages
def process_tracks(lib, tracks, log):
total = len(tracks)
total_found = 0
total_fails = 0
log.info("Received {0} tracks in this page, processing...", total)
for num in range(0, total):
song = None
trackid = tracks[num]["mbid"].strip() if tracks[num]["mbid"] else None
artist = (
tracks[num]["artist"].get("name", "").strip()
if tracks[num]["artist"].get("name", "")
else None
)
title = tracks[num]["name"].strip() if tracks[num]["name"] else None
album = ""
if "album" in tracks[num]:
album = (
tracks[num]["album"].get("name", "").strip()
if tracks[num]["album"]
else None
)
log.debug("query: {0} - {1} ({2})", artist, title, album)
# First try to query by musicbrainz's trackid
if trackid:
song = lib.items(
dbcore.query.MatchQuery("mb_trackid", trackid)
).get()
# If not, try just album/title
if song is None:
log.debug(
"no album match, trying by album/title: {0} - {1}", album, title
)
query = dbcore.AndQuery(
[
dbcore.query.SubstringQuery("album", album),
dbcore.query.SubstringQuery("title", title),
]
)
song = lib.items(query).get()
# If not, try just artist/title
if song is None:
log.debug("no album match, trying by artist/title")
query = dbcore.AndQuery(
[
dbcore.query.SubstringQuery("artist", artist),
dbcore.query.SubstringQuery("title", title),
]
)
song = lib.items(query).get()
# Last resort, try just replacing to utf-8 quote
if song is None:
title = title.replace("'", "\u2019")
log.debug("no title match, trying utf-8 single quote")
query = dbcore.AndQuery(
[
dbcore.query.SubstringQuery("artist", artist),
dbcore.query.SubstringQuery("title", title),
]
)
song = lib.items(query).get()
if song is not None:
count = int(song.get("play_count", 0))
new_count = int(tracks[num].get("playcount", 1))
log.debug(
"match: {0} - {1} ({2}) " "updating: play_count {3} => {4}",
song.artist,
song.title,
song.album,
count,
new_count,
)
song["play_count"] = new_count
song.store()
total_found += 1
else:
total_fails += 1
log.info(" - No match: {0} - {1} ({2})", artist, title, album)
if total_fails > 0:
log.info(
"Acquired {0}/{1} play-counts ({2} unknown)",
total_found,
total,
total_fails,
)
return total_found, total_fails