|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Spoify-remote
|
|
|
|
@ -33,32 +33,32 @@ SPOTIFY_PROCESS_NAME = 'spotify'
|
|
|
|
|
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',
|
|
|
|
@ -69,16 +69,16 @@ class SpotifyNotify():
|
|
|
|
|
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', '/')
|
|
|
|
@ -90,19 +90,19 @@ class SpotifyNotify():
|
|
|
|
|
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',
|
|
|
|
@ -122,10 +122,10 @@ class SpotifyNotify():
|
|
|
|
|
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\
|
|
|
|
@ -133,7 +133,7 @@ class SpotifyNotify():
|
|
|
|
|
or 'iconfilename' not in self.prevMeta\
|
|
|
|
|
or self.prevMeta['artist'] != trackInfo['artist']\
|
|
|
|
|
or self.prevMeta['album'] != trackInfo['album']:
|
|
|
|
|
trackInfo['iconfilename'] = self.retrieveCoverImage(trackInfo)
|
|
|
|
|
trackInfo['iconfilename'] = self.retrieveCoverImage(trackInfo)
|
|
|
|
|
|
|
|
|
|
cover_image = ''
|
|
|
|
|
|
|
|
|
@ -145,9 +145,9 @@ class SpotifyNotify():
|
|
|
|
|
|
|
|
|
|
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',
|
|
|
|
@ -171,19 +171,19 @@ class SpotifyNotify():
|
|
|
|
|
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['artist'],
|
|
|
|
|
notifyText,
|
|
|
|
|
[],
|
|
|
|
|
{},
|
|
|
|
|
2000
|
|
|
|
|
)
|
|
|
|
|
#self.notifyid = self.notifyservice.Notify(
|
|
|
|
|
#"Spotify-notify",
|
|
|
|
|
#self.notifyid,
|
|
|
|
|
#cover_image,
|
|
|
|
|
#trackInfo['artist'],
|
|
|
|
|
#notifyText,
|
|
|
|
|
#[],
|
|
|
|
|
#{},
|
|
|
|
|
#2000
|
|
|
|
|
#)
|
|
|
|
|
|
|
|
|
|
def retrieveCoverImage(self, trackInfo):
|
|
|
|
|
if 'arturl' in trackInfo:
|
|
|
|
@ -201,13 +201,13 @@ class SpotifyNotify():
|
|
|
|
|
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(
|
|
|
|
@ -221,16 +221,16 @@ class SpotifyNotify():
|
|
|
|
|
)
|
|
|
|
|
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):
|
|
|
|
@ -238,12 +238,12 @@ class SpotifyNotify():
|
|
|
|
|
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()
|
|
|
|
@ -253,16 +253,16 @@ class SpotifyNotify():
|
|
|
|
|
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
|
|
|
|
@ -270,81 +270,81 @@ class SpotifyNotify():
|
|
|
|
|
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 DebugMe():
|
|
|
|
@ -353,11 +353,11 @@ class DebugMe():
|
|
|
|
|
self.output = True
|
|
|
|
|
else:
|
|
|
|
|
self.output = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def out(self, msg):
|
|
|
|
|
if not self.output:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(">> {0}".format(msg))
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
@ -387,27 +387,27 @@ if __name__ == "__main__":
|
|
|
|
|
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:
|
|
|
|
|
print "Spotify is not running"
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
SN = SpotifyNotify(Debug)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if options.action:
|
|
|
|
|
action = options.action
|
|
|
|
|
action = action[0:1].upper() + action[1:]
|
|
|
|
|
SN.executeCommand(action)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SpotifyNotify.preventDuplicate(Debug)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
indicateserver = indicate.indicate_server_ref_default()
|
|
|
|
|
indicateserver.set_type("music.spotify")
|
|
|
|
@ -417,4 +417,4 @@ if __name__ == "__main__":
|
|
|
|
|
pass
|
|
|
|
|
SN.pollChange()
|
|
|
|
|
print "Done"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|