-
Notifications
You must be signed in to change notification settings - Fork 6
/
spotify-notify.py
executable file
·480 lines (389 loc) · 14.4 KB
/
spotify-notify.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/usr/bin/python
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gobject, gtk, os, tempfile, sys, time, re, urllib2
from optparse import OptionParser
from subprocess import *
# The url to use when fetching spotify track information.
SPOTIFY_OPEN_URL = "http://open.spotify.com/track/"
# The path to this application's directory.
APPLICATION_DIR = sys.path[0] + "/"
# The file path to spotify. If empty, it will try to auto detect.
SPOTIFY_PROCESS_NAME = ''
# How often to check if spotify has been closed (in milliseconds).
SPOTIFY_CLOSED_CHECK = 20000
class SpotifyNotify():
spotifyPath = ''
tryToReconnect = False
tmpfile = False
def __init__(self, debugger):
self.debug = debugger
self.spotifyservice = False
self.prev = 0
self.new = False
self.prevMeta = {}
self.notifyid = 0
self.connect()
def __del__(self):
if SpotifyNotify and SpotifyNotify.tmpfile:
SpotifyNotify.tmpfile.close()
def connect(self):
self.debug.out("Connecting to spotify.")
self.bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
try:
self.spotifyservice = self.bus.get_object(
'com.spotify.qt',
'/org/mpris/MediaPlayer2'
)
SpotifyNotify.tryToReconnect = False
except Exception, e:
self.spotifyservice = False
self.debug.out("Failed to connect.")
self.debug.out(e)
def executeCommand(self, key):
if not key:
return
self.connect()
self.debug.out("Running command: {0}".format(key))
self.cmd = self.spotifyservice.get_dbus_method(key, 'org.mpris.MediaPlayer2.Player')
self.cmd()
def pollChange(self):
try:
self.spotifyservice = self.bus.get_object('com.spotify.qt', '/')
self.cmd = self.spotifyservice.get_dbus_method(
'GetMetadata',
'org.freedesktop.MediaPlayer2'
)
self.new = self.cmd()
except Exception, e:
self.debug.out('Spotify service not connected.')
SpotifyNotify.tryToReconnect = True
if (self.prev != self.new):
self.trackChange(self.new)
self.prev = self.new
return 1
def trackChange(self, *trackChange):
if not trackChange[0]:
return
self.prev = trackChange[0]
trackInfo = {}
trackMap = {
'artist' : 'xesam:artist',
'album' : 'xesam:album',
'title' : 'xesam:title',
'year' : 'xesam:contentCreated',
'trackhash' : 'mpris:trackid',
'arturl' : 'mpris:artUrl'
}
# Fetch the track information for the notification window.
for key in trackMap:
if not trackMap[key] in trackChange[0]:
continue
piece = trackChange[0][trackMap[key]]
if key == 'year':
piece = str(piece[:4])
elif isinstance(piece, list):
piece = ", ".join(piece)
if not isinstance(piece, str):
piece = str(piece)
trackInfo[key] = piece.encode('utf-8')
if not self.prevMeta\
or not SpotifyNotify.tmpfile\
or 'iconfilename' not in self.prevMeta\
or self.prevMeta['artist'] != trackInfo['artist']\
or self.prevMeta['album'] != trackInfo['album']:
trackInfo['iconfilename'] = self.retrieveCoverImage(trackInfo)
cover_image = ''
if 'iconfilename' in trackInfo:
cover_image = trackInfo['iconfilename']
elif 'iconfilename' in self.prevMeta:
cover_image = self.prevMeta['iconfilename']
trackInfo['iconfilename'] = cover_image
if cover_image == '':
cover_image = APPLICATION_DIR + 'icon_spotify.png'
self.prevMeta = trackInfo
# Connect to notification interface on DBUS.
self.notifyservice = self.bus.get_object(
'org.freedesktop.Notifications',
'/org/freedesktop/Notifications'
)
self.notifyservice = dbus.Interface(
self.notifyservice,
"org.freedesktop.Notifications"
)
notifyText = "{0}\n{1}".format(
trackInfo['artist'],
trackInfo['album']
)
if len(trackInfo['year']) > 0:
notifyText += " ({0})".format(trackInfo['year'])
# Send track change information to stdout
print "Changing track : {0} | {1} | {2} ({3})".format(
trackInfo['artist'],
trackInfo['title'],
trackInfo['album'],
trackInfo['year']
)
# The second param is the replace id, so get the notify id back,
# store it, and send it as the replacement on the next call.
self.notifyid = self.notifyservice.Notify(
"Spotify-notify",
self.notifyid,
cover_image,
trackInfo['title'],
notifyText,
[],
{},
-1
)
def retrieveCoverImage(self, trackInfo):
if 'arturl' in trackInfo:
self.debug.out("Simply retrieving image from {0}".format(trackInfo['arturl']))
iconfilename = self.fetchCoverImage(trackInfo['arturl'])
else:
#if (trackInfo['trackhash'][0:14] == 'spotify:local:'):
# self.debug.out("Track is a local file. No art available.")
# return ''
self.debug.out("Attempting to fetch image from spotify")
iconfilename = self.fetchCoverImageSpotify(
trackInfo['artist'],
trackInfo['album'],
trackInfo['trackhash']
)
return iconfilename
def fetchCoverImageSpotify(self, artist, album, trackhash):
try:
trackid = trackhash.split(":")[2]
url = SPOTIFY_OPEN_URL + trackid
tracksite = urllib2.urlopen(url).read()
# Attempt to get the image url from the open graph image meta tag.
imageurl = False
metaMatch = re.search(
'<meta\s[^\>]*property\s*=\s*["\']og:image["\'][^\>]*/?>',
tracksite
)
if metaMatch:
contentMatch = re.search(
'content\s*=\s*["\']([^\"\']*)["\']',
metaMatch.group(0)
)
if contentMatch:
imageurl = contentMatch.group(1)
if not imageurl:
self.debug.out("No cover available.")
raise()
return self.fetchCoverImage(imageurl)
except Exception, e:
self.debug.out("Couldn't fetch cover image.")
self.debug.out(e)
return ''
def fetchCoverImage(self, url):
# Close the temporary image file, we are going to make a new one.
if SpotifyNotify.tmpfile:
SpotifyNotify.tmpfile.close()
SpotifyNotify.tmpfile = False
try:
SpotifyNotify.tmpfile = tempfile.NamedTemporaryFile()
tmpfilename = SpotifyNotify.tmpfile.name
self.debug.out("Album art tmp filepath: {0}".format(tmpfilename))
coverfile = urllib2.urlopen(url)
SpotifyNotify.tmpfile.write(coverfile.read())
SpotifyNotify.tmpfile.flush()
return tmpfilename
except Exception, e:
self.debug.out("Couldn't fetch cover image.")
self.debug.out(e)
return ''
@staticmethod
def startSpotify(Debug):
if not SpotifyNotify.spotifyPath:
Debug.out("No spotify process identifier found.")
return
ident = SpotifyNotify.spotifyPath
Debug.out("Looking for spotify as: {0}".format(ident))
procs = SpotifyNotify.checkForProcess(
'ps x | grep "{0}" | grep -v grep'.format(ident),
Debug
)
if len(procs):
Debug.out("Spotify process found as: {0}".format(" ".join(procs[0])))
return
Debug.out("Starting new Spotify now.")
FNULL = open('/dev/null', 'w')
spid = Popen([ident], stdout=FNULL, stderr=FNULL).pid
if spid:
Debug.out("Spotify started, pid: {0}.".format(spid))
else:
Debug.out("Spotify could not be started.")
@staticmethod
def checkForClosedSpotify(SN, Debug):
if not SpotifyNotify.spotifyPath:
Debug.out("No spotify process identifier found.")
return False
ident = SpotifyNotify.spotifyPath
Debug.out("Looking for spotify as: {0}".format(ident))
procs = SpotifyNotify.checkForProcess(
'ps x | grep "{0}" | grep -v grep'.format(ident),
Debug
)
if len(procs):
Debug.out("Spotify process found as: {0}".format(" ".join(procs[0])))
if (SpotifyNotify.tryToReconnect):
SN.connect()
return True
if SpotifyNotify.tmpfile:
SpotifyNotify.tmpfile.close()
Debug.out("Spotify has been closed, therefore I die.")
exit(0)
@staticmethod
def preventDuplicate(Debug):
mypid = os.getpid()
Debug.out("My pid: {0}".format(mypid))
proc = SpotifyNotify.checkForProcess('ps -p {0}'.format(mypid), Debug)
if not proc[0][3]:
return
process = proc[0][3]
search = 'ps -C {0}'.format(process)
Debug.out("Looking for other processes named: {0}".format(process).strip())
if process == 'python':
if not sys.argv[0]:
Debug.out("Process started using python, cannot determine script name.")
return
search = 'ps ax | grep "python {0}" | grep -v grep'.format(sys.argv[0])
for line in SpotifyNotify.checkForProcess(search, Debug):
if int(line[0]) != mypid:
print("This program was already running.")
Debug.out("I am a duplicate. I shall end myself. ({0})".format(" ".join(line)))
exit(0)
@staticmethod
def checkForProcess(proc, Debug):
output = []
for line in Popen(proc, shell=True, stdout=PIPE).stdout:
fields = line.split()
if not fields[0].isdigit():
continue
output.append(fields)
return output
class MediaKeyHandler():
def __init__(self, spotifyNotify, debugger):
self.SN = spotifyNotify
self.debug = debugger
self.keys = {
"Play" : "PlayPause",
"Stop" : "Pause",
"Pause" : "Pause",
"Next" : "Next",
"Previous" : "Previous",
}
try:
self.bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
self.bus_object = self.bus.get_object(
'org.gnome.SettingsDaemon',
'/org/gnome/SettingsDaemon/MediaKeys'
)
except:
print "Gnome SettingsDaemon not founnd, multimedia keys will not interact with spotify"
return
try:
self.bus_object.GrabMediaPlayerKeys(
"Spotify",
0,
dbus_interface='org.gnome.SettingsDaemon.MediaKeys'
)
except:
pass
self.bus_object.connect_to_signal(
'MediaPlayerKeyPressed',
self.handle_mediakey
)
def handle_mediakey(self, *mmkeys):
for key in mmkeys:
if not key in self.keys or not self.keys[key]:
continue
self.SN.executeCommand(self.keys[key])
class DebugMe():
def __init__(self, toggle):
if toggle:
self.output = True
else:
self.output = False
def out(self, msg):
if not self.output:
return
print(">> {0}".format(msg))
if __name__ == "__main__":
parser = OptionParser()
parser.add_option(
'-a',
'--action',
dest = 'action',
default = None,
type = 'choice',
choices = ['playPause', 'play', 'pause', 'next', 'previous'],
help = 'Music player actions (playPause/play/pause/next/previous).'
)
parser.add_option(
'-n',
'--skip_notify',
dest = 'skipNotify',
action = 'store_true',
default = False,
help = 'Song change notifications will be turned off.'
)
parser.add_option(
'-m',
'--skip_media_keys',
dest = 'skipMediaKeys',
action = 'store_true',
default = False,
help = 'Multimedia keys will not interact with spotify.'
)
parser.add_option(
'-s',
'--skip_spotify',
dest = 'skipSpotify',
action = 'store_true',
default = False,
help = 'Spotify will not be opened if it is not running and spotify notify will not close when spotify is closed.'
)
parser.add_option(
'-d',
'--debug',
dest = 'debug',
action = 'store_true',
default = False,
help = 'Debug messages will be displayed.'
)
(options, args) = parser.parse_args()
Debug = DebugMe(options.debug)
print("Spotify-notify v0.6")
if SPOTIFY_PROCESS_NAME:
SpotifyNotify.spotifyPath = SPOTIFY_PROCESS_NAME
else:
for line in Popen('which spotify', shell=True, stdout=PIPE).stdout:
SpotifyNotify.spotifyPath = str(line).strip()
break
if options.skipSpotify:
Debug.out('Skipping spotify process check.')
else:
SpotifyNotify.startSpotify(Debug)
DBusGMainLoop(set_as_default=True)
SN = SpotifyNotify(Debug)
if options.action:
action = options.action
action = action[0:1].upper() + action[1:]
SN.executeCommand(action)
exit(0)
SpotifyNotify.preventDuplicate(Debug)
if not options.skipMediaKeys:
MH = MediaKeyHandler(SN, Debug)
loop = gobject.MainLoop()
if not options.skipSpotify:
gobject.timeout_add(SPOTIFY_CLOSED_CHECK, SN.checkForClosedSpotify, SN, Debug)
if not options.skipNotify:
gobject.timeout_add(500, SN.pollChange)
loop.run()