Merge lp:~jamalta/groovenotify/crossplatform into lp:groovenotify

Proposed by Jamal Fanaian
Status: Merged
Approved by: Jamal Fanaian
Approved revision: not available
Merge reported by: Jamal Fanaian
Merged at revision: not available
Proposed branch: lp:~jamalta/groovenotify/crossplatform
Merge into: lp:groovenotify
Diff against target: 105 lines (+49/-23)
1 file modified
src/groovenotify.py (+49/-23)
To merge this branch: bzr merge lp:~jamalta/groovenotify/crossplatform
Reviewer Review Type Date Requested Status
Jamal Fanaian Approve
Review via email: mp+17405@code.launchpad.net

This proposal supersedes a proposal from 2010-01-14.

Commit message

Removed dependencies on glib and gio to make it work on any platform. Added support for Growl.

To post a comment you must log in.
Revision history for this message
Jamal Fanaian (jamalta) : Posted in a previous version of this proposal
review: Approve
Revision history for this message
Jamal Fanaian (jamalta) : Posted in a previous version of this proposal
review: Needs Resubmitting
Revision history for this message
Jamal Fanaian (jamalta) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/groovenotify.py'
2--- src/groovenotify.py 2010-01-13 23:04:47 +0000
3+++ src/groovenotify.py 2010-01-14 19:45:23 +0000
4@@ -1,13 +1,33 @@
5-import gio
6-import glib
7 import os
8-import pynotify
9 import re
10-from urllib import quote
11+from urllib import quote_plus
12 from urllib2 import urlopen
13+from stat import ST_MTIME
14+from sys import exit
15+from time import sleep
16
17 CACHE_DIR = os.path.join(os.path.expanduser("~"), ".cache/groovenotify")
18
19+try:
20+ import pynotify
21+ pynotify.init("GrooveNotify")
22+ def notify(summary, content, icon):
23+ notify = pynotify.Notification(summary, content, icon)
24+ notify.show()
25+except ImportError:
26+ import Growl
27+ growl = Growl.GrowlNotifier(
28+ applicationName="GrooveNotify",
29+ notifications=["playing",]
30+ )
31+ growl.register()
32+ def notify(summary, content, icon):
33+ image = Growl.Image.imageFromPath(icon)
34+ g.notify("playing", summary, content, image)
35+except Exception, e:
36+ print "Could not initialize notifications: %s" % e
37+ exit(1)
38+
39
40 class GrooveNotify:
41
42@@ -15,21 +35,38 @@
43 pynotify.init("GrooveNotify")
44 self.last_read = []
45 self.art_regex = re.compile('src=["\'](.*?)["\'].*alt="Product Details"')
46- gfile = gio.File("%s/Documents/Grooveshark/currentSong.txt" % os.path.expanduser("~"))
47- self.monitor = gfile.monitor_file()
48- self.monitor.connect('changed', self.update)
49+ self.file = "%s/Documents/Grooveshark/currentSong.txt" % os.path.expanduser("~")
50+ self.loop()
51
52- def notify(self, summary, content, icon):
53- notify = pynotify.Notification(summary, content, icon)
54- notify.show()
55+ def loop(self):
56+ last_content = None
57+ last_mtime = os.stat(self.file)[ST_MTIME]
58+ while 1:
59+ try:
60+ mtime = os.stat(self.file)[ST_MTIME]
61+ if last_mtime < mtime:
62+ fh = open(self.file)
63+ content = fh.read().split("\t")
64+ fh.close()
65+ if last_content != content and len(content) >= 3:
66+ last_content = content
67+ art = self.get_artwork('"%s" "%s"' % (content[0], content[2]))
68+ notify("Now Playing", "%s\nBy %s" % (content[0], content[2]), art)
69+ sleep(2)
70+ except KeyboardInterrupt:
71+ try:
72+ fh.close()
73+ except:
74+ pass
75+ exit()
76
77 def get_artwork(self, query):
78- response = urlopen("http://www.amazon.com/s/ref=nb_ss?field-keywords=%s" % (quote(query)))
79+ response = urlopen("http://www.amazon.com/s/ref=nb_ss?url=search-alias=aps&field-keywords=%s" % (quote_plus(query)))
80 content = response.read()
81 matches = self.art_regex.findall(content)
82 if len(matches) >= 1:
83 img = urlopen(matches[0])
84- filename = os.path.join(CACHE_DIR, quote(query))
85+ filename = os.path.join(CACHE_DIR, quote_plus(query))
86 if not os.path.exists(filename):
87 if not os.path.exists(CACHE_DIR):
88 os.mkdir(CACHE_DIR)
89@@ -42,16 +79,5 @@
90
91 return None
92
93- def update(self, monitor, file, file2, evt_type):
94- if evt_type is gio.FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
95- content = file.read().read().split("\t")
96- if content[3] == 'playing' and self.last_read[0:3] != content[0:3]:
97- self.last_read = content
98- art = self.get_artwork("%s %s" % (content[0], content[2]))
99- self.notify("Now Playing", "%s\nBy %s" % (content[0], content[2]), art)
100-
101-
102 if __name__ == "__main__":
103 groovenotify = GrooveNotify()
104- loop = glib.MainLoop()
105- loop.run()

Subscribers

People subscribed via source and target branches

to all changes: