diff options
author | Michaël Ball <michael.ball@gmail.com> | 2018-06-15 19:58:21 +0100 |
---|---|---|
committer | Michaël Ball <michael.ball@gmail.com> | 2018-06-15 19:58:21 +0100 |
commit | fd562c8f5fc3f21b809d56f00a36fdeaa4e138b8 (patch) | |
tree | 3c2f52e65e78b8801b96d0a4639a64db92d1ebd2 /lastfm_similarity.py | |
parent | 0ca2ce025817db215e78de297e2bac3380b369ab (diff) | |
parent | 57827d9ed06fefb15cc96a01098c38b7becb8f8c (diff) |
Merge branch 'feature/similarity-strictness' into develop
Diffstat (limited to 'lastfm_similarity.py')
-rw-r--r-- | lastfm_similarity.py | 45 |
1 files changed, 38 insertions, 7 deletions
diff --git a/lastfm_similarity.py b/lastfm_similarity.py index d2e04ba..f381d23 100644 --- a/lastfm_similarity.py +++ b/lastfm_similarity.py @@ -38,10 +38,11 @@ class LastFMSimilarity(EventPlugin): "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): @@ -54,7 +55,12 @@ class LastFMSimilarity(EventPlugin): config.set("plugins", "lastfm_similarity_blacklist_artist", self._blacklist_artist_count) - table = Gtk.Table(rows=2, columns=2) + 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( @@ -73,6 +79,19 @@ class LastFMSimilarity(EventPlugin): 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): @@ -107,8 +126,8 @@ class LastFMSimilarity(EventPlugin): else: print_d("Trying with {} - {}".format(artistname.splitlines()[0], trackname)) - request = "".join((request, "&track=", quote(trackname), "&artist=", - quote(artistname.splitlines()[0]))) + request = "".join((request, "&track=", quote(trackname), + "&artist=", quote(artistname.splitlines()[0]))) request = "".join((request, "&limit={}".format(limit))) @@ -128,8 +147,11 @@ class LastFMSimilarity(EventPlugin): response = json.loads(str(stream.read(), "utf-8")) for track in response["similartracks"]["track"]: - similar_tracks.append( - (track["artist"]["name"], track["name"])) + similarity_score = float(track["match"]) + + if similarity_score >= self._similarity_strictness: + similar_tracks.append( + (track["artist"]["name"], track["name"])) return similar_tracks @@ -138,6 +160,8 @@ class LastFMSimilarity(EventPlugin): return self._find_similar_tracks(trackname, artistname) return [] + except (ValueError, OverflowError): + return [] else: return [] @@ -172,7 +196,10 @@ class LastFMSimilarity(EventPlugin): response = json.loads(str(stream.read(), "utf-8")) for artist in response["similarartists"]["artist"]: - similar_artists.append(artist["name"]) + similarity_score = float(artist["match"]) + + if similarity_score >= self._similarity_strictness: + similar_artists.append(artist["name"]) return similar_artists @@ -181,6 +208,8 @@ class LastFMSimilarity(EventPlugin): return self._find_similar_artists(artistname) return [] + except (ValueError, OverflowError): + return [] else: return [] @@ -200,6 +229,8 @@ class LastFMSimilarity(EventPlugin): candidates = self._find_similar_tracks(track, artist) if candidates: + random.shuffle(candidates) + for candidate in candidates: if not self._check_artist_played(candidate[0]): |