-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify-extras.py
221 lines (175 loc) · 7.42 KB
/
spotify-extras.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
#!/usr/bin/env python
"""
spotify-extras
Notification and media key support for qt Spotify.
Inspired by http://code.google.com/p/spotify-notify/, it has the following
improvements:
* Persists over multiple qt Spotify instances, so you can run
spotify-extras at log in and then open/close Spotify as many times as
you like.
* Deletes obsolete notifications so cycling through tracks doesn't create
a huge backlog.
* Fetches default icon from spotify website.
* Notifies on stop as well as start.
* Won't fetch the image for every track -- only once per album.
The files associated with this project are made available under version 2
of the GPL: http://www.gnu.org/licenses/gpl-2.0.html
"""
import base64
import logging
import os
import re
import urllib2
from dbus.mainloop.glib import DBusGMainLoop
from dbus.exceptions import DBusException
import dbus
import gobject
DEBUG_LEVEL = logging.DEBUG
class Application(object):
cache_dir = os.path.expanduser('~/.cache/spotify-extras')
default_icon_url = 'http://open.spotify.com/static/images/icon-48.png'
img_re = re.compile('<img.*?id="cover-art".*?src="(.*?)"')
spotify_track_url = 'http://open.spotify.com/track/%s'
def __init__(self):
self.bus = None
self.last_notification = 0
self.last_track = None
if not os.path.exists(self.cache_dir):
os.makedirs(self.cache_dir)
self.update_default_icon()
DBusGMainLoop(set_as_default=True)
self.loop = gobject.MainLoop()
def connect(self):
self.bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
def get_interface(self, bus_name, object_path, interface):
if self.bus is None:
self.connect()
obj = self.bus.get_object(bus_name, object_path)
return dbus.Interface(obj, interface)
def get_current_track(self):
interface = self.get_interface('com.spotify.qt', '/',
'org.freedesktop.MediaPlayer2')
return interface.GetMetadata()
def update_default_icon(self):
default_icon_path = self.get_icon_path('default')
if not os.path.exists(default_icon_path):
response = urllib2.urlopen(self.default_icon_url)
f = open(default_icon_path, 'wb')
f.write(response.read())
f.close()
def get_icon_path(self, icon):
return os.path.join(self.cache_dir, icon)
def get_track_icon_path(self, track):
raw_icon_name = '%s-%s' % (track['xesam:artist'], track['xesam:album'])
icon_name = base64.b64encode(raw_icon_name.encode('utf-8'))
return self.get_icon_path(icon_name)
def get_track_url(self, track):
track_id = track['mpris:trackid'].split(':')[-1]
return self.spotify_track_url % track_id
def update_track_icon(self, track):
track_icon_path = self.get_track_icon_path(track)
track_url = self.get_track_url(track)
response = urllib2.urlopen(track_url)
img_match = self.img_re.search(response.read())
if img_match:
img_url = img_match.group(1)
response = urllib2.urlopen(img_url)
img_f = open(track_icon_path, 'wb')
img_f.write(response.read())
img_f.close()
self.notify(*self.get_playback_info())
def _notify(self, summary, icon_path=None, body=""):
interface = self.get_interface('org.freedesktop.Notifications',
'/org/freedesktop/Notifications', 'org.freedesktop.Notifications')
if icon_path is None:
icon_path = self.get_icon_path('default')
if self.last_notification:
logging.debug('Closing notification %s.' % self.last_notification)
interface.CloseNotification(self.last_notification)
self.last_notification = interface.Notify('spotify-extras',
self.last_notification, icon_path, summary, body, [],
{}, 2)
def notify(self, status, track):
if status == "Stopped":
logging.info('Playback stopped')
self._notify("[stopped]")
return
artist = track['xesam:artist']
if isinstance(artist, dbus.Array):
artist = artist[0]
summary = artist
body = '%s\n%s (%s)' % (track['xesam:title'], track['xesam:album'],
track['xesam:contentCreated'][:4])
logging.info('Current track: %(xesam:title)r by %(xesam:artist)r' %
track)
track_icon_path = self.get_track_icon_path(track)
if os.path.exists(track_icon_path):
icon_path = track_icon_path
else:
icon_path = None
self._notify(summary, icon_path, body)
if icon_path != track_icon_path:
self.update_track_icon(track)
def get_playback_status(self):
interface = self.get_interface('org.mpris.MediaPlayer2.spotify',
'/org/mpris/MediaPlayer2', 'org.freedesktop.DBus.Properties')
return interface.Get("org.mpris.MediaPlayer2.Player", "PlaybackStatus")
def get_playback_info(self):
status = self.get_playback_status()
track = self.get_current_track()
return status, track
def update_track_display(self, sender=None, *args, **kwargs):
info = self.get_playback_info()
if info and info != self.last_track:
self.notify(*info)
self.last_track = info
def restart_notifications(self, sender, *args, **kwargs):
if sender == "org.mpris.MediaPlayer2.spotify":
logging.info("Connecting to new Spotify instance.")
self.start_notifications()
def start_notifications(self, persist=False):
try:
self.update_track_display()
interface = self.get_interface('org.mpris.MediaPlayer2.spotify',
'/org/mpris/MediaPlayer2', 'org.freedesktop.DBus.Properties')
interface.connect_to_signal('PropertiesChanged',
self.update_track_display)
except DBusException:
pass
if persist:
global_interface = self.get_interface('org.freedesktop.DBus',
'/org/freedesktop/DBus', 'org.freedesktop.DBus')
global_interface.connect_to_signal('NameOwnerChanged',
self.restart_notifications)
def player_command(self, command):
try:
interface = self.get_interface('org.mpris.MediaPlayer2.spotify',
'/org/mpris/MediaPlayer2', 'org.mpris.MediaPlayer2.Player')
except DBusException:
logging.debug("Not carrying out command '%s' because can't find "
"Spotify instance." % command)
return
method = getattr(interface, command)
method()
def media_player_key_pressed(self, sender, key, **kwargs):
command = {
"Next": "Next",
"Play": "PlayPause",
"Previous": "Previous",
}.get(key)
if command:
self.player_command(command)
def listen_for_keys(self):
interface = self.get_interface('org.gnome.SettingsDaemon',
'/org/gnome/SettingsDaemon/MediaKeys',
'org.gnome.SettingsDaemon.MediaKeys')
interface.connect_to_signal('MediaPlayerKeyPressed',
self.media_player_key_pressed)
def run(self):
self.start_notifications(persist=True)
self.listen_for_keys()
self.loop.run()
if __name__ == '__main__':
logging.getLogger().setLevel(DEBUG_LEVEL)
application = Application()
application.run()