~jamalta/groovenotify/trunk

« back to all changes in this revision

Viewing changes to groovenotify/groovenotify.py

  • Committer: Jamal Fanaian
  • Date: 2010-01-20 15:43:43 UTC
  • Revision ID: jamal.fanaian@gmail.com-20100120154343-0tcnebizqe2u3eip
Fixed bug #508127 by checking metadata for duplicate notifications and not including the status. Cleaned up the code a bit and added some documentation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 
9
9
CACHE_DIR = os.path.join(os.path.expanduser("~"), ".cache/groovenotify")
10
10
 
 
11
# Initialize notification module
11
12
try:
12
13
    import pynotify
13
14
    pynotify.init("GrooveNotify")
36
37
        self.last_read = []
37
38
        self.art_regex = re.compile('src=["\'](.*?)["\'].*alt="Product Details"')
38
39
        self.file = "%s/Documents/Grooveshark/currentSong.txt" % os.path.expanduser("~")
 
40
        self.last_content = []
 
41
        self.last_mtime = os.stat(self.file)[ST_MTIME]
39
42
        self.loop()
40
43
 
41
44
    def loop(self):
42
 
        last_content = None
43
 
        last_mtime = os.stat(self.file)[ST_MTIME]
44
 
        while 1:
 
45
        """
 
46
        Main loop that calls check_updates and waits for a 
 
47
        KeyboardInterrupt.
 
48
        """
 
49
        try:
 
50
            self.check_update()
 
51
            sleep(1)
 
52
            self.loop()
 
53
        except KeyboardInterrupt:
45
54
            try:
46
 
                mtime = os.stat(self.file)[ST_MTIME]
47
 
                if last_mtime < mtime:
48
 
                    fh = open(self.file)
49
 
                    content = fh.read().split("\t")
50
 
                    fh.close()
51
 
                    if last_content != content and len(content) >= 3:
52
 
                        last_content = content
53
 
                        art = self.get_artwork('"%s" "%s"' % (content[0], content[2]))
54
 
                        notify("Now Playing", "%s\nBy %s" % (content[0], content[2]), art)
55
 
                sleep(2)
56
 
            except KeyboardInterrupt:
57
 
                try:
58
 
                    fh.close()
59
 
                except:
60
 
                    pass
61
 
                exit()
 
55
                fh.close()
 
56
            except:
 
57
                pass
 
58
 
 
59
    def check_update(self):
 
60
        """
 
61
        Checks currentSong.txt for changes, and uses the content to
 
62
        display a play notification.
 
63
        """
 
64
        mtime = os.stat(self.file)[ST_MTIME]
 
65
        if self.last_mtime < mtime:
 
66
            fh = open(self.file)
 
67
            content = fh.read().split("\t")
 
68
            fh.close()
 
69
 
 
70
            # Ignore the status field
 
71
            if len(content) >= 3 and self.last_content[0:3] != content[0:3]:
 
72
                self.last_content = content
 
73
                art = self.get_artwork('"%s" "%s"' % (content[1], content[2]))
 
74
                notify("Now Playing", "%s\nBy %s" % (content[0], content[2]), art)
62
75
 
63
76
    def get_artwork(self, query):
 
77
        """
 
78
        Attempts to retrieve album artwork based on passed query.
 
79
        """
64
80
        response = urlopen("http://www.amazon.com/s/ref=nb_ss?url=search-alias=aps&field-keywords=%s" % (quote_plus(query)))
65
81
        content = response.read()
66
82
        matches = self.art_regex.findall(content)