summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lastfm_similarity.py121
1 files changed, 75 insertions, 46 deletions
diff --git a/lastfm_similarity.py b/lastfm_similarity.py
index 4e73d9d..3df6241 100644
--- a/lastfm_similarity.py
+++ b/lastfm_similarity.py
@@ -1,23 +1,17 @@
# -*- coding: utf-8 -*-
import json
import urllib2
+import random
from gi.repository import GLib
-from quodlibet import _
-from quodlibet import app
+from quodlibet import _, app, config
from quodlibet.plugins.events import EventPlugin
-from quodlibet.plugins import PluginConfig
from quodlibet.qltk import Icons
from quodlibet.query import Query
from quodlibet.util.dprint import print_d
-pconfig = PluginConfig("notify")
-pconfig.defaults.set("blacklist_track_count", 10)
-pconfig.defaults.set("blacklist_artist_count", 10)
-
-
class LastFMSimilarity(EventPlugin):
PLUGIN_ID = "Last.fm Similarity"
PLUGIN_NAME = _("Last.fm Similarity")
@@ -34,14 +28,40 @@ class LastFMSimilarity(EventPlugin):
}
def __init__(self):
+ 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._last_tracks = []
self._last_artists = []
+
+ def PluginPreferences(self, parent):
+ 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)
+
+ table = Gtk.Table(rows=2, 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)
+ return table
+
def _check_artist_played(self, artist):
- if artist in self._last_artists:
- return True
- else:
- return False
+ for played_artist in self._last_artists:
+ if unicode(artist).upper() == played_artist.upper():
+ return True
+
+ return False
def _check_track_played(self, track):
if track in self._last_tracks:
@@ -49,19 +69,27 @@ class LastFMSimilarity(EventPlugin):
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=20):
+ 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=", trackname, "&artist=",
- artistname))
+ artistname.splitlines()[0]))
request = "".join((request, "&limit={}".format(limit)))
@@ -87,19 +115,25 @@ class LastFMSimilarity(EventPlugin):
return similar_tracks
except KeyError:
+ if mbid:
+ return self._find_similar_tracks(trackname, artistname)
+
return []
else:
return []
- def _find_similar_artists(self, artistname, mbid=None, limit=20):
+ 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:
- request = "".join((request, "&artist=", artistname))
+ print_d("Trying with {}".format(artistname.splitlines()[0]))
+ request = "".join((request, "&artist=",
+ artistname.splitlines()[0]))
request = "".join((request, "&limit={}".format(limit)))
@@ -124,13 +158,16 @@ class LastFMSimilarity(EventPlugin):
return similar_artists
except KeyError:
+ if mbid:
+ return self._find_similar_artists(artistname)
+
return []
else:
return []
def on_change(self, song):
- artist = song.get("artist")
+ artist = song.get("artist").splitlines()[0]
track = song.get("title")
candidates = []
@@ -149,10 +186,6 @@ class LastFMSimilarity(EventPlugin):
print_d("[similarity] found track match: %s - %s"
% (candidate[0], candidate[1]))
- if (len(self._last_tracks)
- == pconfig.getint("blacklist_track_count")):
- del self._last_tracks[0]
-
query = Query.StrictQueryMatcher(
"&(artist = \"%s\", title = \"%s\")"
% (candidate[0], candidate[1]))
@@ -165,15 +198,6 @@ class LastFMSimilarity(EventPlugin):
if self._check_track_played(song.get("~filename")):
continue
- self._last_tracks.append(song.get("~filename"))
-
- if (len(self._last_artists)
- == pconfig.getint(
- "blacklist_artist_count")):
- del self._last_artists[0]
-
- self._last_artists.append(song.get("artist"))
-
app.window.playlist.enqueue([song])
return
@@ -191,32 +215,37 @@ class LastFMSimilarity(EventPlugin):
try:
results = filter(query.search, app.library)
- for song in results:
- if self._check_track_played(song.get("~filename")):
- continue
+ candidate_song_length = len(results)
+ for dummy in xrange(candidate_song_length):
+ idx = random.randint(0, (candidate_song_length - 1))
+ song = results[idx]
- if (len(self._last_artists)
- == pconfig.getint("blacklist_artist_count")):
- del self._last_artists[0]
-
- self._last_artists.append(song.get("artist"))
- self._last_tracks.append(song.get("~filename"))
+ if not self._check_track_played(song.get("~filename")):
+ app.window.playlist.enqueue([song])
+ return
- app.window.playlist.enqueue([song])
- return
except AttributeError:
pass
def plugin_on_song_started(self, song):
self._last_tracks.append(song.get("~filename"))
- self._last_artists.append(song.get("artist"))
+ self._add_played_artists(song.get("artist").splitlines())
GLib.idle_add(self.on_change, song)
def plugin_on_song_ended(self, song, stopped):
- if len(self._last_tracks) == pconfig.getint("blacklist_track_count"):
- del self._last_tracks[0]
+ 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 len(self._last_artists) == pconfig.getint("blacklist_artist_count"):
- del self._last_artists[0]
+ if artist_count > self._blacklist_artist_count:
+ self._last_artists = self._last_artists[
+<<<<<<< HEAD
+ (artist_count - max_artist_count):artist_count]
+=======
+ (artist_count - self._blacklist_artist_count):artist_count]
+>>>>>>> 8362451... Add preferences