-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspotify_notifications.py
More file actions
77 lines (59 loc) · 2.42 KB
/
spotify_notifications.py
File metadata and controls
77 lines (59 loc) · 2.42 KB
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
#!/usr/bin/env python
import dbus
import subprocess
import time
import inspect, os
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-d",action="store_true",dest="display",default=False) #can be used to display a new notification
global python_version
if sys.version_info[0] == 3:
python_version = 3
elif sys.version_info[0] == 2:
python_version = 2
else:
python_version = 1
results = parser.parse_args()
update_time = 3 # The time to wait between checks (in seconds)
expire_time = 4000 # The expiration time of the notifications (in milliseconds)
logo_abspath = 'spotify_80.png' # The path to a logo that will be displayed in the notification, this is not in the github repo due to copyright concerns
# logo_abspath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) + '/' + logo_abspath
thumb_path = '/tmp/spotify_notification_thumb'
wget_log_path = '/tmp/spotify_notification_wget.log'
global current_track
current_track = ""
global current_thumb
current_thumb = ""
def extract_str(dbus_str):
return dbus_str.encode('utf-8')
def update():
global current_thumb
bus = dbus.SessionBus()
player = bus.get_object('com.spotify.qt', '/')
iface = dbus.Interface(player, 'org.freedesktop.MediaPlayer2')
info = iface.GetMetadata()
try:
thumb_url = str(info['mpris:artUrl'])
if current_thumb != thumb_url:
current_thumb = thumb_url
subprocess.call(['wget', '-O', thumb_path, '-o', wget_log_path, thumb_url])
if python_version==2:
return "Title: \t\t" + extract_str(info['xesam:title'])+"\nArtist: \t\t" + extract_str(info['xesam:artist'][0]) + "\nAlbum: \t" + extract_str(info['xesam:album'])
elif python_version==3:
return "Title: \t\t" + str(info['xesam:title'])+"\nArtist: \t\t" + str(info['xesam:artist'][0]) + "\nAlbum: \t" + str(info['xesam:album'])
except KeyError: #Sometimes songs in other languages screw up
return ""
def notify():
global current_track
track_now = update()
if track_now != current_track:
current_track = track_now
subprocess.call(['notify-send', '--expire-time=' + str(expire_time), '--icon=' + thumb_path, track_now])
if __name__ == '__main__' :
if(results.display): #display a notification on demand
notify()
else:
while True:
notify()
time.sleep(update_time)