summaryrefslogtreecommitdiff
path: root/lastfm_similarity.py
blob: 73e705dd87f919bb88509764e3359dd90c65d074 (plain)
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
# -*- coding: utf-8 -*-
"""Last.fm similarity plugin for Quod Libet."""
import json
from urllib.parse import quote
from urllib.request import urlopen
from urllib.error import URLError
import random

from gi.repository import GLib, Gtk

from quodlibet import _, app, config
from quodlibet.plugins.events import EventPlugin
from quodlibet.qltk import Icons
from quodlibet.query import Query
from quodlibet.util.dprint import print_d


class LastFMSimilarity(EventPlugin):
    """Last.fm similarity event plugin."""

    PLUGIN_ID = "Last.fm Similarity"
    PLUGIN_NAME = _("Last.fm Similarity")
    PLUGIN_DESC = _("Finds a similar song using Last.fm's track similarity API"
                    " and adds it to the queue.")
    PLUGIN_ICON = Icons.NETWORK_WORKGROUP

    LAST_FM_API_URI = "http://ws.audioscrobbler.com/2.0/"
    API_KEY = "e94b09aa2c04ab264deb7a7ae02ecd05"

    LAST_FM_API_METHODS = {
        "similar_artists": "artist.getSimilar",
        "similar_tracks": "track.getSimilar",
    }

    def __init__(self):
        """Initialize the plugin."""
        self._blacklist_track_count = config.getint(
            "plugins", "lastfm_similarity_blacklist_tracks", 10)
        self._blacklist_artist_count = config.getint(
            "plugins", "lastfm_similarity_blacklist_artists", 10)
        self._similarity_strictness = config.getfloat(
            "plugins", "lastfm_similarity_strictness", 0.5)
        self._last_tracks = []
        self._last_artists = []

    def PluginPreferences(self, parent):
        """Plugin Preferences."""
        def blacklist_track_changed(entry):
            self._blacklist_track_count = int(entry.get_value())
            config.set("plugins", "lastfm_similarity_blacklist_tracks",
                       self._blacklist_track_count)

        def blacklist_artist_changed(entry):
            self._blacklist_artist_count = int(entry.get_value())
            config.set("plugins", "lastfm_similarity_blacklist_artist",
                       self._blacklist_artist_count)

        def strictness_changed(entry):
            self._similarity_strictness = entry.get_value() / 10
            config.set("plugins", "lastfm_similarity_strictness",
                       self._similarity_strictness)

        table = Gtk.Table(rows=3, columns=2)
        table.set_row_spacings(6)
        table.set_col_spacings(6)
        table.attach(
            Gtk.Label(
                label=_("Number of recently played tracks to blacklist:")),
            0, 1, 0, 1)
        track_entry = Gtk.SpinButton(adjustment=Gtk.Adjustment.new(
            self._blacklist_track_count, 0, 1000, 1, 10, 0))
        track_entry.connect("value-changed", blacklist_track_changed)
        table.attach(track_entry, 1, 2, 0, 1)
        table.attach(
            Gtk.Label(
                label=_("Number of recently played artists to blacklist:")),
            0, 1, 1, 2)
        artist_entry = Gtk.SpinButton(adjustment=Gtk.Adjustment.new(
            self._blacklist_artist_count, 0, 1000, 1, 10, 0))
        artist_entry.connect("value-changed", blacklist_artist_changed)
        table.attach(artist_entry, 1, 2, 1, 2)
        strictness_label = Gtk.Label(label=_("Similarity strictness:"))
        strictness_label.set_alignment(1.0, 0.0)
        table.attach(strictness_label, 0, 1, 2, 3,
                     xoptions=Gtk.AttachOptions.FILL)
        strictness_adj = Gtk.Adjustment(lower=0.00, upper=10.00,
                                        step_increment=0.1)
        strictness_hscale = Gtk.HScale(adjustment=strictness_adj)
        strictness_hscale.set_value(self._similarity_strictness * 10)
        strictness_hscale.set_draw_value(False)
        strictness_hscale.set_show_fill_level(False)
        strictness_hscale.connect("value_changed", strictness_changed)
        table.attach(strictness_hscale, 1, 2, 2, 3)

        return table

    def _check_artist_played(self, artist):
        for played_artist in self._last_artists:
            if artist.upper() == played_artist.upper():
                return True

        return False

    def _check_track_played(self, track):
        if track in self._last_tracks:
            return True
        else:
            return False

    def _add_played_artists(self, artists):
        for artist in artists:
            if artist not in self._last_artists:
                self._last_artists.append(artist)

    def _build_uri(self, request):
        return "".join((self.LAST_FM_API_URI, request, "&api_key=",
                        self.API_KEY, "&format=json"))

    def _find_similar_tracks(self, trackname, artistname, mbid=None, limit=50):
        request = "".join(("?method=",
                           self.LAST_FM_API_METHODS["similar_tracks"]))

        if mbid:
            print_d("Trying with mbid {}".format(mbid))
            request = "".join((request, "&mbid=", mbid))
        else:
            print_d("Trying with {} - {}".format(artistname.splitlines()[0],
                                                 trackname))
            request = "".join((request, "&track=", quote(trackname),
                               "&artist=", quote(artistname.splitlines()[0])))

        request = "".join((request, "&limit={}".format(limit)))

        uri = self._build_uri(request)

        stream = None

        try:
            stream = urlopen(uri)
        except URLError:
            return []

        if stream.getcode() == 200:
            similar_tracks = []
            track_weights = []

            try:
                response = json.loads(str(stream.read(), "utf-8"))

                for track in response["similartracks"]["track"]:
                    similarity_score = float(track["match"])

                    if similarity_score >= self._similarity_strictness:
                        similar_tracks.append(
                            (track["artist"]["name"], track["name"]))
                        track_weights.append(similarity_score)

                return similar_tracks, track_weights

            except KeyError:
                if mbid:
                    return self._find_similar_tracks(trackname, artistname)

                return []
            except (ValueError, OverflowError):
                return []

        else:
            return []

    def _find_similar_artists(self, artistname, mbid=None, limit=40):
        request = "".join(("?method=",
                           self.LAST_FM_API_METHODS["similar_artists"]))

        if mbid:
            print_d("Trying with artist mbid {}".format(mbid))
            request = "".join((request, "&mbid=", mbid))
        else:
            print_d("Trying with {}".format(artistname.splitlines()[0]))
            request = "".join((request, "&artist=",
                               quote(artistname.splitlines()[0])))

        request = "".join((request, "&limit={}".format(limit)))

        uri = self._build_uri(request)

        stream = None

        try:
            stream = urlopen(uri)
        except URLError:
            return []

        if stream.getcode() == 200:
            similar_artists = []
            artist_weights = []

            try:
                response = json.loads(str(stream.read(), "utf-8"))

                for artist in response["similarartists"]["artist"]:
                    similarity_score = float(artist["match"])

                    if similarity_score >= self._similarity_strictness:
                        similar_artists.append(artist["name"])
                        artist_weights.append(similarity_score)

                return similar_artists, artist_weights

            except KeyError:
                if mbid:
                    return self._find_similar_artists(artistname)

                return []
            except (ValueError, OverflowError):
                return []

        else:
            return []

    def on_change(self, song):
        """Find similar track on song change."""
        artist = song.get("artist").splitlines()[0]
        track = song.get("title")

        candidates = []
        weights = []

        try:
            mbid = song.get("musicbrainz_releasetrackid")

            candidates, weights = self._find_similar_tracks(
                track, artist, mbid)
        except KeyError:
            candidates, weights = self._find_similar_tracks(track, artist)

        found_tracks = []
        found_track_weights = []
        if candidates:
            for idx, candidate in enumerate(candidates):
                if not self._check_artist_played(candidate[0]):

                    query = Query.StrictQueryMatcher(
                        "&(artist = \"%s\", title = \"%s\")"
                        % (candidate[0], candidate[1]))
                    try:
                        results = list(filter(query.search, app.library))

                        if results:
                            song = results[0]

                            if self._check_track_played(song.get("~filename")):
                                continue

                            print_d("[similarity] found track match: %s - %s"
                                    % (candidate[0], candidate[1]))

                            found_tracks.append(song)
                            found_track_weights.append(weights[idx])
                    except AttributeError:
                        pass

        if found_tracks:
            selected_track = random.choices(
                found_tracks, found_track_weights, k=1)[0]
            app.window.playlist.enqueue([selected_track])
            return

        artist_candidates, artist_weights = self._find_similar_artists(artist)

        found_artists = []
        found_artist_weights = []

        for idx, artist in enumerate(artist_candidates):
            if not self._check_artist_played(artist):
                print_d("[similarity] found artist match: %s" % artist)

                found_artists.append(artist)
                found_artist_weights.append(artist_weights[idx])

        while True:
            if found_artists:
                artist = random.choices(
                    found_artists, found_artist_weights, k=1)[0]

                for idx, a in enumerate(found_artists):
                    if a == artist:
                        found_artist_weights.pop(idx)

                found_artists.remove(artist)

                query = Query.StrictQueryMatcher(
                    "&(artist = \"%s\", title != \"[silence]\")" % artist)
                try:
                    results = list(filter(query.search, app.library))

                    candidate_song_length = len(results)
                    for dummy in range(candidate_song_length):
                        idx = random.randint(0, (candidate_song_length - 1))
                        song = results[idx]

                        if not self._check_track_played(song.get("~filename")):
                            app.window.playlist.enqueue([song])
                            return

                except AttributeError:
                    pass
            else:
                return

    def plugin_on_song_started(self, song):
        """Append current track to last played tracks and artists."""
        self._last_tracks.append(song.get("~filename"))
        self._add_played_artists(song.get("artist").splitlines())

        GLib.idle_add(self.on_change, song)

    def plugin_on_song_ended(self, song, stopped):
        """Append current track to last played tracks and artists."""
        track_count = len(self._last_tracks)
        artist_count = len(self._last_artists)

        if track_count > self._blacklist_track_count:
            self._last_tracks = self._last_tracks[
                (track_count - self._blacklist_track_count):track_count]

        if artist_count > self._blacklist_artist_count:
            self._last_artists = self._last_artists[
                (artist_count - self._blacklist_artist_count):artist_count]