forked from thisfred/autoqueue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquodlibet_autoqueue.py
457 lines (396 loc) · 15.3 KB
/
quodlibet_autoqueue.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
"""
AutoQueue: an automatic queueing plugin for Quod Libet.
version 0.3
Copyright 2007-2012 Eric Casteleijn <[email protected]>
Naglis Jonaitis <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation
"""
from __future__ import print_function
from builtins import str
from collections import deque
from datetime import datetime
from autoqueue import AutoQueueBase
from autoqueue.player import PlayerBase, SongBase
from gi.repository import GLib, Gtk
from quodlibet import app, config
from quodlibet.plugins.events import EventPlugin
from quodlibet.qltk.entry import ValidatingEntry
from quodlibet.util import copool
INT_SETTINGS = {
'artist_block_time': {
'value': 1,
'label': 'block artist (days)'},
'desired_queue_length': {
'value': 4440,
'label': 'queue (seconds)'},
'number': {
'value': 40,
'label': 'number of tracks to look up'}}
BOOL_SETTINGS = {
'verbose': {
'value': False,
'label': 'log to console'},
'use_gaia': {
'value': True,
'label': 'use gaia similarity'},
'use_lastfm': {
'value': True,
'label': 'use last.fm similarity'},
'use_groupings': {
'value': True,
'label': 'use grouping similarity'},
'contextualize': {
'value': True,
'label': 'queue context appropriate tracks first.'},
'southern_hemisphere': {
'value': False,
'label': 'southern hemisphere'},
'whole_albums': {
'value': True,
'label': 'queue whole albums'},
'favor_new': {
'value': True,
'label': 'favor tracks that have never been played'}}
STR_SETTINGS = {
'restrictions': {
'value': '',
'label': 'restrict'},
'birthdays': {
'value': '',
'label': 'birthdays, comma separated list of name:mm/dd values'},
'location': {
'value': '',
'label': 'location ([City], [State] or [City], [Country])'},
'zipcode': {
'value': '',
'label': 'zipcode'},
'geohash': {
'value': '',
'label': 'geohash (see geohash.org)'},
'extra_context': {
'value': '',
'label': 'extra context'}}
def escape(the_string):
"""Double escape quotes."""
return the_string.replace('"', '\\"').replace("'", "\\'")
def remove_role(artist):
"""Remove performer role from string."""
if not artist.endswith(')'):
return artist
return artist.split('(')[0].strip()
class Song(SongBase):
"""A wrapper object around quodlibet song objects."""
def get_artist(self):
"""Return lowercase UNICODE name of artist."""
return self.song.comma("artist").lower()
def get_artists(self):
"""Return lowercase UNICODE name of artists and performers."""
artists = [artist.lower() for artist in self.song.list("artist")]
performers = [artist.lower() for artist in self.song.list("performer")]
if hasattr(self.song, '_song'):
for tag in self.song._song:
if tag.startswith('performer:'):
performers.extend(
[artist.lower() for artist in self.song.list(tag)])
else:
for tag in self.song:
if tag.startswith('performer:'):
performers.extend(
[artist.lower() for artist in self.song.list(tag)])
artists.extend([remove_role(p) for p in performers])
return list(set(artists))
def get_title(self, with_version=True):
"""Return lowercase UNICODE title of song."""
title = self.song.comma("title").lower()
if with_version:
version = self.song.comma("version").lower()
if version:
return "%s (%s)" % (title, version)
return title
def get_album(self):
"""Return lowercase UNICODE album of song."""
return self.song.comma("album").lower()
def get_album_artist(self):
"""Return lowercase UNICODE album of song."""
return self.song.comma("albumartist").lower()
def get_musicbrainz_albumid(self):
"""Return musicbrainz album_id if any."""
return self.song.comma('musicbrainz_albumid')
def get_tracknumber(self):
"""Get integer tracknumber."""
tracknumber = self.song('tracknumber')
if isinstance(tracknumber, int):
return tracknumber
tracknumber = tracknumber.split('/')
try:
return int(tracknumber[0])
except ValueError:
return 0
def get_discnumber(self):
"""Get disc number."""
try:
return int(self.song('discnumber').split('/')[0])
except ValueError:
return 1
def get_tags(self):
"""Get a list of tags for the song."""
return self.song.list("grouping")
def get_filename(self):
"""Get the filename of the song."""
return self.song("~filename")
def get_length(self):
"""Get the length in seconds of the song."""
return self.song("~#length")
def get_playcount(self):
"""Get the total playcount for the song."""
try:
playcount = int(self.song("~#playcount"))
except ValueError:
# XXX: WTF: playcount can be an empty string??
playcount = 0
try:
skipcount = int(self.song('~#skipcount'))
except ValueError:
# XXX: WTF: skipcount can be an empty string??
skipcount = 0
return playcount + skipcount
def get_added(self):
"""Get the date the song was added to the library."""
return self.song("~#added")
def get_last_started(self):
"""Get the datetime the song was last started."""
return self.song("~#laststarted")
def get_rating(self):
"""Get the rating for the song."""
return self.song("~#rating")
def get_date_string(self):
"""Get the rating for the song."""
return self.song("date")
def get_year(self):
"""Get the rating for the song."""
try:
return int(self.song("~year"))
except ValueError:
return None
class AutoQueue(EventPlugin, AutoQueueBase):
"""The actual plugin class."""
PLUGIN_ID = "AutoQueue"
PLUGIN_NAME = _("Auto Queue") # noqa
PLUGIN_VERSION = "0.2"
PLUGIN_DESC = ("Queue similar songs.")
__enabled = False
def __init__(self):
EventPlugin.__init__(self)
AutoQueueBase.__init__(self, Player())
self._generators = deque()
def enabled(self):
"""Handle user enabling the plugin."""
print("enabled")
self.__enabled = True
def disabled(self):
"""Handle user disabling the plugin."""
print("disabled")
self.__enabled = False
def plugin_on_song_ended(self, song, skipped):
"""Triggered when a song ends or is skipped."""
if not song:
return
ssong = Song(song)
GLib.idle_add(self.on_song_ended, ssong, skipped)
def plugin_on_song_started(self, song):
"""Triggered when a song starts."""
if not song:
return
ssong = Song(song)
GLib.idle_add(self.on_song_started, ssong)
def plugin_on_removed(self, songs):
"""Triggered when songs are removed from the library."""
GLib.idle_add(self.on_removed, [Song(s) for s in songs])
def PluginPreferences(self, parent): # pylint: disable=C0103
"""Set and unset preferences from gui or config file."""
def bool_changed(widget):
"""Boolean setting changed."""
if widget.get_active():
setattr(self.configuration, widget.get_name(), True)
else:
setattr(self.configuration, widget.get_name(), False)
config.set(
'plugins',
'autoqueue_%s' % widget.get_name(),
widget.get_active() and 'true' or 'false')
def str_changed(entry, key):
"""String setting changed."""
value = entry.get_text()
config.set('plugins', 'autoqueue_%s' % key, value)
setattr(self.configuration, key, value)
def int_changed(entry, key):
"""Integer setting changed."""
value = entry.get_text()
if value:
config.set('plugins', 'autoqueue_%s' % key, value)
setattr(self.configuration, key, int(value))
table = Gtk.Table()
table.set_col_spacings(3)
i = 0
j = 0
for setting in BOOL_SETTINGS:
button = Gtk.CheckButton(label=BOOL_SETTINGS[setting]['label'])
button.set_name(setting)
button.set_active(
config.get(
"plugins", "autoqueue_%s" % setting).lower() == 'true')
button.connect('toggled', bool_changed)
table.attach(button, i, i + 1, j, j + 1)
if i == 1:
i = 0
j += 1
else:
i += 1
for setting in INT_SETTINGS:
j += 1
label = Gtk.Label('%s:' % INT_SETTINGS[setting]['label'])
entry = Gtk.Entry()
table.attach(
label, 0, 1, j, j + 1,
xoptions=Gtk.AttachOptions.FILL | Gtk.AttachOptions.SHRINK)
table.attach(
entry, 1, 2, j, j + 1,
xoptions=Gtk.AttachOptions.FILL | Gtk.AttachOptions.SHRINK)
entry.connect('changed', int_changed, setting)
try:
entry.set_text(
config.get('plugins', 'autoqueue_%s' % setting))
except:
pass
for setting in STR_SETTINGS:
j += 1
label = Gtk.Label('%s:' % STR_SETTINGS[setting]['label'])
entry = ValidatingEntry()
table.attach(
label, 0, 1, j, j + 1,
xoptions=Gtk.AttachOptions.FILL | Gtk.AttachOptions.SHRINK)
table.attach(
entry, 1, 2, j, j + 1,
xoptions=Gtk.AttachOptions.FILL | Gtk.AttachOptions.SHRINK)
entry.connect('changed', str_changed, setting)
try:
entry.set_text(config.get('plugins', 'autoqueue_%s' % setting))
except:
pass
return table
class Player(PlayerBase):
def execute_async(self, method, *args, **kwargs):
"""Execute a method asynchronously."""
if 'funcid' not in kwargs:
kwargs['funcid'] = method.__name__ + str(datetime.now())
copool.add(method, *args, **kwargs)
def construct_album_search(self, album, album_artist=None, album_id=None):
""""Construct a search that looks for songs from this album."""
if not album:
return
search = 'album="%s"' % escape(album)
if album_artist:
search = '&(%s, albumartist="%s")' % (search, escape(album_artist))
if album_id:
search = (
'&(%s, |(musicbrainz_albumid="%s", musicbrainz_albumid=""))' %
(search, album_id))
return search
def construct_files_search(self, filenames):
"""Construct a search for songs with any of these filenames."""
return '~filename=|(%s)' % (
','.join(['"%s"' % escape(f) for f in filenames]),)
def construct_file_search(self, filename):
"""Construct a search that looks for songs with this filename."""
if not filename:
return
search = '~filename="%s"' % (escape(filename),)
return search
def construct_track_search(self, artist, title):
"""Construct an artist and title search."""
search = '&(artist = "%s", title = "%s", version="")' % (
escape(artist), escape(title))
if "(" in title:
split = title.split("(")
if not split[0]:
# (something) title [(version)]
nsplit = ["(".join(split[:2])]
nsplit.extend(split[2:])
split = nsplit
# title (version [(something)])
vtitle = split[0].strip()
version = "(".join(split[1:]).strip()[:-1]
versioned = '&(artist = "%s", title = "%s", version="%s")' % (
escape(artist),
escape(vtitle),
escape(version))
search = "|(%s, %s)" % (search, versioned)
return search
def construct_tag_search(self, tags):
"""Construct a tags search."""
search = ''
search_tags = []
for tag in tags:
stripped = escape(tag)
search_tags.append(
'|(grouping = "%s",grouping = "artist:%s",'
'grouping = "album:%s")' % (stripped, stripped, stripped))
search = "|(%s)" % (",".join(search_tags))
return search
def construct_artist_search(self, artist):
"""Construct a search that looks for songs with this artist."""
search = '|(artist = "%s",performer="%s")' % (
escape(artist), escape(artist))
return search
def set_variables_from_config(self, configuration):
"""Initialize user settings from the configuration storage."""
for key, vdict in INT_SETTINGS.items():
try:
setattr(configuration, key, config.getint(
"plugins", "autoqueue_%s" % key))
except:
setattr(configuration, key, vdict['value'])
config.set("plugins", "autoqueue_%s" % key, vdict['value'])
for key, vdict in BOOL_SETTINGS.items():
try:
setattr(configuration, key, config.get(
"plugins", "autoqueue_%s" % key).lower() == 'true')
except:
setattr(configuration, key, vdict['value'])
config.set("plugins", "autoqueue_%s" %
key, vdict['value'] and 'true' or 'false')
for key, vdict in STR_SETTINGS.items():
try:
setattr(
configuration, key, config.get(
"plugins", "autoqueue_%s" % key))
except:
setattr(configuration, key, vdict['value'])
config.set("plugins", "autoqueue_%s" % key, vdict['value'])
def get_queue_length(self):
"""Get the current length of the queue."""
if app.window is None:
return 0
playlist = app.window.playlist
return sum(
[row.get("~#length", 0) for row in playlist.q.get()])
def enqueue(self, song):
"""Put the song at the end of the queue."""
app.window.playlist.enqueue([song.song])
def search(self, search, restrictions=None):
"""Perform a player search."""
if restrictions:
search = '&(%s,%s)' % (search, restrictions)
try:
songs = app.library.query(search)
except Exception as e:
print(repr(search), repr(e))
return []
return [Song(song) for song in songs]
def get_songs_in_queue(self):
"""Return (wrapped) song objects for the songs in the queue."""
if app.window is None:
return []
return [Song(song) for song in app.window.playlist.q.get()]