summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichaël Ball <michael.ball@gmail.com>2018-05-31 21:36:16 +0100
committerMichaël Ball <michael.ball@gmail.com>2018-05-31 22:54:03 +0100
commit4f421589b70015b1d737290d39aaf9e71e13f757 (patch)
tree3b32ddc03dd817090a5ccebba17ff1ec63426297
parent8711da9cc7b2af31ecf1353d052a2d4f861ec19d (diff)
Work with python 3 for Quodlibet 4.x
-rw-r--r--lastfm_similarity.py63
1 files changed, 43 insertions, 20 deletions
diff --git a/lastfm_similarity.py b/lastfm_similarity.py
index af46aa6..1300157 100644
--- a/lastfm_similarity.py
+++ b/lastfm_similarity.py
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
+"""Last.fm similarity plugin for Quod Libet."""
import json
-import urllib2
+from urllib.request import urlopen
+from urllib.error import URLError
import random
from gi.repository import GLib, Gtk
@@ -13,6 +15,8 @@ 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"
@@ -28,37 +32,51 @@ 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)
+ """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._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)
+ 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)
+ 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))
+ 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))
+ 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):
for played_artist in self._last_artists:
- if unicode(artist).upper() == played_artist.upper():
+ if artist.upper() == played_artist.upper():
return True
return False
@@ -98,15 +116,15 @@ class LastFMSimilarity(EventPlugin):
stream = None
try:
- stream = urllib2.urlopen(uri)
- except urllib2.URLError:
+ stream = urlopen(uri)
+ except URLError:
return []
if stream.getcode() == 200:
similar_tracks = []
try:
- response = json.load(stream)
+ response = json.loads(str(stream.read(), "utf-8"))
for track in response["similartracks"]["track"]:
similar_tracks.append(
@@ -142,15 +160,15 @@ class LastFMSimilarity(EventPlugin):
stream = None
try:
- stream = urllib2.urlopen(uri)
- except urllib2.URLError:
+ stream = urlopen(uri)
+ except URLError:
return []
if stream.getcode() == 200:
similar_artists = []
try:
- response = json.load(stream)
+ response = json.loads(str(stream.read(), "utf-8"))
for artist in response["similarartists"]["artist"]:
similar_artists.append(artist["name"])
@@ -167,6 +185,7 @@ class LastFMSimilarity(EventPlugin):
return []
def on_change(self, song):
+ """Find similar track on song change."""
artist = song.get("artist").splitlines()[0]
track = song.get("title")
@@ -190,7 +209,8 @@ class LastFMSimilarity(EventPlugin):
"&(artist = \"%s\", title = \"%s\")"
% (candidate[0], candidate[1]))
try:
- results = filter(query.search, app.library)
+ results = list(filter(query.search, app.library))
+ print_d("results: %s" % results)
if results:
song = results[0]
@@ -213,10 +233,11 @@ class LastFMSimilarity(EventPlugin):
query = Query.StrictQueryMatcher(
"&(artist = \"%s\", title != \"[silence]\")" % artist)
try:
- results = filter(query.search, app.library)
+ results = list(filter(query.search, app.library))
+ print_d("results: %s" % results)
candidate_song_length = len(results)
- for dummy in xrange(candidate_song_length):
+ for dummy in range(candidate_song_length):
idx = random.randint(0, (candidate_song_length - 1))
song = results[idx]
@@ -228,13 +249,14 @@ class LastFMSimilarity(EventPlugin):
pass
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)
@@ -245,3 +267,4 @@ class LastFMSimilarity(EventPlugin):
if artist_count > self._blacklist_artist_count:
self._last_artists = self._last_artists[
(artist_count - self._blacklist_artist_count):artist_count]
+