-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyze_songs.py
64 lines (52 loc) · 1.94 KB
/
analyze_songs.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
"""Acoustic analysis songs plugin."""
import dbus
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib
from quodlibet import _
from quodlibet.plugins.songsmenu import SongsMenuPlugin
DBusGMainLoop(set_as_default=True)
def no_op(*args, **kwargs):
pass
def get_title(song):
"""Return lowercase UNICODE title of song."""
version = song.comma("version").lower()
title = song.comma("title").lower()
if version:
return "%s (%s)" % (title, version)
return title
class AnalyzeSongsPlugin(SongsMenuPlugin):
"""Acoustic song analysis."""
PLUGIN_ID = "Acoustic Analysis"
PLUGIN_NAME = _("Autoqueue Analyze") # noqa
PLUGIN_DESC = _("Perform acoustic analysis of the selected songs.") # noqa
PLUGIN_ICON = "gtk-find-and-replace"
PLUGIN_VERSION = "0.1"
def __init__(self, *args):
SongsMenuPlugin.__init__(self, *args)
bus = dbus.SessionBus()
sim = bus.get_object("org.autoqueue", "/org/autoqueue/Similarity")
self.similarity = dbus.Interface(
sim, dbus_interface="org.autoqueue.SimilarityInterface"
)
def plugin_songs(self, songs):
"""Add the work to the coroutine pool."""
print("added {} songs".format(len(songs)))
GLib.idle_add(self.doit, songs)
def doit(self, songs):
filenames = []
for song in songs:
filename = song("~filename")
try:
filename.encode("utf-8")
except UnicodeEncodeError:
try:
filename = filename.decode("utf-8")
except (UnicodeDecodeError, AttributeError):
print(
"Could not figure out filename encoding: %r" % song("~filename")
)
continue
filenames.append(filename)
self.similarity.analyze_tracks(
filenames, reply_handler=no_op, error_handler=no_op
)