-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrowl.py
executable file
·190 lines (170 loc) · 5.75 KB
/
growl.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# The MIT License
#
# Copyright (c) 2011 by Sorin Ionescu
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""Sends Growl notications on behalf of Pianobar."""
# Notify when a song starts.
NOTIFY_SONG_START = True
# Notify when a song ends.
NOTIFY_SONG_END = False
# Notify when a song is loved.
NOTIFY_SONG_LOVE = True
# Notify when a song is banned.
NOTIFY_SONG_BAN = True
# Notify when a song is shelved.
NOTIFY_SONG_SHELVE = True
# Notify when a song is bookmarked.
NOTIFY_SONG_BOOKMARK = True
# Notify when an artist is bookmarked.
NOTIFY_ARTIST_BOOKMARK = True
# Notify on program error.
NOTIFY_PROGRAM_ERROR = True
# Notify on network error.
NOTIFY_NETWORK_ERROR = True
# Imports
from gntp.notifier import GrowlNotifier
import socket
import os
import sys
# Script Arguments
argv = os.sys.argv
argc = len(argv)
# Stores Pianobar information.
info = {}
# Initalize a new Growl object (localhost does not require a password).
growl = GrowlNotifier(
applicationName=u'Pianobar',
notifications=[
u'Song Start',
u'Song End',
u'Song Love',
u'Song Ban',
u'Song Shelve',
u'Song Bookmark',
u'Artist Bookmark',
u'Program Error',
u'Network Error'])
# Register Pianobar with Growl.
try:
growl.register()
except socket.error:
# Be silent.
pass
# Parse stdin into the dictionary.
for line in sys.stdin:
key, delimiter, value = unicode(line).strip().partition(u'=')
if value.isdigit():
info[key] = int(value)
else:
info[key] = value
# The icon will be used when song cover art is missing.
info[u'programIcon'] = u'file://{0}/pandora.png'.format(
os.path.dirname(argv[0]))
# Show a heart next to a loved song title.
if info[u'rating'] == 1:
info[u'lovedIcon'] = u' ♥'
else:
info[u'lovedIcon'] = u''
# Set the cover art to the program icon when it's missing.
if len(info[u'coverArt']) == 0:
info[u'coverArt'] = info[u'programIcon']
try:
if argc < 1:
sys.exit(1)
elif argv[1] == u'songstart' and NOTIFY_SONG_START:
growl.notify(
noteType=u'Song Start',
icon=info[u'coverArt'],
title=u'{0}{1}'.format(
info[u'title'],
info[u'lovedIcon']),
description=u'{0}\n{1}'.format(
info[u'artist'],
info[u'album']))
elif argv[1] == u'songfinish' and NOTIFY_SONG_END:
growl.notify(
noteType=u'Song End',
icon=info[u'coverArt'],
title=u'{0}{1}'.format(
info[u'title'],
info[u'lovedIcon']),
description=u'{0}\n{1}'.format(
info[u'artist'],
info[u'album']))
elif argv[1] == u'songlove' and NOTIFY_SONG_LOVE:
growl.notify(
noteType='Song Love',
icon=info[u'coverArt'],
title=u'Song Loved',
description=u'{0}\n{1}\n{2}'.format(
info[u'title'],
info[u'artist'],
info[u'album']))
elif argv[1] == u'songban' and NOTIFY_SONG_BAN:
growl.notify(
noteType='Song Ban',
icon=info[u'coverArt'],
title=u'Song Banned',
description=u'{0}\n{1}\n{2}'.format(
info[u'title'],
info[u'artist'],
info[u'album']))
elif argv[1] == u'songshelf' and NOTIFY_SONG_SHELVE:
growl.notify(
noteType='Song Shelve',
icon=info[u'coverArt'],
title=u'Song Shelved',
description=u'{0}\n{1}\n{2}'.format(
info[u'title'],
info[u'artist'],
info[u'album']))
elif argv[1] == u'songbookmark' and NOTIFY_SONG_BOOKMARK:
growl.notify(
noteType='Song Bookmark',
icon=info[u'coverArt'],
title=u'Song Bookmarked',
description=u'{0}\n{1}\n{2}'.format(
info[u'title'],
info[u'artist'],
info[u'album']))
elif argv[1] == u'artistbookmark' and NOTIFY_ARTIST_BOOKMARK:
growl.notify(
noteType='Artist Bookmark',
icon=info[u'coverArt'],
title=u'Artist Bookmarked',
description=info[u'artist'])
elif info[u'pRet'] != 1 and NOTIFY_PROGRAM_ERROR:
growl.notify(
noteType=u'Program Error',
icon=info[u'programIcon'],
title=u'Pianobar Failed',
description=info[u'pRetStr'])
elif info[u'wRet'] != 1 and NOTIFY_NETWORK_ERROR:
growl.notify(
noteType=u'Network Error',
icon=info[u'programIcon'],
title=u'Network Failed',
description=info[u'wRetStr'])
except socket.error:
# Be silent.
pass