-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.py
135 lines (113 loc) · 4.29 KB
/
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
# this is the magic interpreted by Sonata, referring to on_enable etc. below:
### BEGIN PLUGIN INFO
# [plugin]
# plugin_format: 0, 0
# name: Notify plugin
# version: 0, 0, 1
# description: A simple notify plugin using pynotify.
# author: Leo Iannacone
# author_email: [email protected]
# url: http://en.leoiannacone.com/2009/05/a-simple-plugin-for-sonata/
# license: GPL v3 or later
# [capabilities]
# enablables: on_enable
# playing_song_observers: on_song_change
### END PLUGIN INFO
# nothing magical from here on
import ConfigParser
import os
import pynotify
import re
from sonata.library import library_set_data
cur_songinfo = None
# this gets called when the plugin is loaded, enabled, or disabled:
def on_enable(state):
global notify, art_cache, art_location, music_dir
if state:
if pynotify.init("sonata"):
notify = pynotify.Notification('sonata')
filename = os.path.expanduser("~/.config/sonata/art_cache")
if os.path.exists(filename):
try:
f = open(filename, 'r')
r = f.read()
art_cache = eval(r)
f.close()
except:
art_cache = {}
else:
art_cache = {}
if os.path.isfile(os.path.expanduser('~/.config/sonata/sonatarc')):
conf = ConfigParser.ConfigParser()
conf.read(os.path.expanduser('~/.config/sonata/sonatarc'))
if conf.has_option('player','art_location'):
art_location = conf.get('player','art_location')
else:
art_location = None
if conf.has_option('profiles', 'musicdirs[0]'):
music_dir = conf.get('profiles', 'musicdirs[0]')
else:
music_dir = None
else:
print ('ALERT from "Notify plugin": Failed to load plugin notify. Module pynotify required.')
else:
notify = None
art_cache = None
conf = None
# this gets called when a new song is playing:
def on_song_change(songinfo):
global cur_songinfo
if songinfo == cur_songinfo:
return
else:
cur_songinfo = songinfo
if songinfo:
art_folder = ''
summary = "Current song:"
body = songinfo['file']
icon = "sonata"
icon_tmp = None
album = ''
artist = ''
genre = None
year = None
path = os.path.dirname(songinfo['file'])
if 'title' in songinfo:
summary = songinfo['title']
if not summary[0].isupper():
summary = summary.capitalize()
if 'artist' in songinfo:
body = 'by %s\n' % songinfo['artist']
artist = songinfo['artist']
if 'album' in songinfo:
body += 'from %s' % songinfo['album']
album = songinfo['album']
if 'year' in songinfo:
genre = songinfo['year']
if 'genre' in songinfo:
genre = songinfo['genre']
cache_key = library_set_data (album=album, artist=artist, genre=genre, year=year, path=path)
try:
icon = art_cache[cache_key]
except:
try:
cache_key = library_set_data (album=album, artist=artist, path=path)
icon = art_cache[cache_key]
except:
icon = "sonata"
if 'sonata-album.png' in icon:
icon = 'sonata'
if icon is 'sonata' and music_dir is not None:
song_dir = '%s/%s' % (music_dir, path)
# convert path like 'artist/album/CD 2' into 'artist/album'
if re.compile(r'(?i)cd\s*\d+').match(os.path.split(song_dir)[1]):
song_dir = os.path.split(song_dir)[0]
arts = [os.path.join(song_dir, name) for name in 'cover.jpg', 'artwork.jpg', 'album.jpg', 'folder.jpg']
arts.append(os.path.expanduser('~/.covers/%s-%s.jpg' % (artist, album)))
arts = filter(os.path.isfile, arts)
icon = arts and arts[0] or 'sonata'
notify.set_property('body', body)
notify.set_property('summary', summary)
notify.set_property('icon-name', icon)
if not notify.show():
print ('ALERT from "Notify plugin": Failed to send notification')