source: trunk/StreamVision/StreamVision.py @ 191

Last change on this file since 191 was 191, checked in by Nicholas Riley, 16 years ago

StreamVision?.py: handle lack of artwork on remote tracks properly

File size: 4.0 KB
Line 
1#!/usr/bin/pythonw
2# -*- coding: utf-8 -*-
3
4from appscript import app, k
5from AppKit import NSApplication, NSBeep, NSSystemDefined, NSURL, NSWorkspace
6from PyObjCTools import AppHelper
7from Carbon.CarbonEvt import RegisterEventHotKey, GetApplicationEventTarget
8from Carbon.Events import cmdKey
9import struct
10import scrape
11import _StreamVision
12
13GROWL_APP_NAME = 'StreamVision'
14NOTIFICATION_TRACK_INFO = 'iTunes Track Info'
15NOTIFICATIONS_ALL = [NOTIFICATION_TRACK_INFO]
16
17kEventHotKeyPressedSubtype = 6
18kEventHotKeyReleasedSubtype = 9
19
20growl = app('GrowlHelperApp')
21
22growl.register(
23    as_application=GROWL_APP_NAME,
24    all_notifications=NOTIFICATIONS_ALL,
25    default_notifications=NOTIFICATIONS_ALL,
26    icon_of_application='iTunes.app')
27    # if we leave off the .app, we can get Classic iTunes's icon
28
29def growlNotify(title, description, **kw):
30    growl.notify(
31        with_name=NOTIFICATION_TRACK_INFO,
32        title=title,
33        description=description,
34        application_name=GROWL_APP_NAME,
35        **kw)
36       
37def radioParadiseURL():
38    session = scrape.Session()
39    session.go('http://www2.radioparadise.com/nowplay_3.php')
40    return session.region.firsttag('a', class_='sky')['href']
41
42def cleanStreamName(name):
43    name = name.split('. ')[0]
44    name = name.split(': ')[0]
45    name = name.split(' - ')
46    if len(name) > 1:
47        name = ' - '.join(name[:-1])
48    else:
49        name = name[0]
50    return name
51   
52def cleanStreamTitle(title):
53    if title == k.MissingValue:
54        return ''
55    title = title.replace('`', u'’')
56    return title
57   
58def iTunesApp():
59    return app(id='com.apple.iTunes')
60
61class StreamVision(NSApplication):
62
63    hotKeyActions = {}
64    hotKeys = []
65
66    def displayTrackInfo(self):
67        iTunes = iTunesApp()
68        if iTunes.player_state.get() == k.playing:
69            if iTunes.current_track.class_.get() == k.URL_track:
70                growlNotify(cleanStreamTitle(iTunes.current_stream_title.get()),
71                            cleanStreamName(iTunes.current_track.name.get()))
72            else:
73                kw = {}
74                artwork = iTunes.current_track.artworks.get()
75                if artwork:
76                    kw['pictImage'] = artwork[0].data.get()
77                growlNotify(iTunes.current_track.name.get(),
78                            iTunes.current_track.album.get() + "\n" +
79                            iTunes.current_track.artist.get(),
80                            **kw)
81        else:
82            trackName = ''
83            if iTunes.current_track.class_.get() != k.Property:
84                trackName = iTunes.current_track.name.get()
85            growlNotify("iTunes is not playing.", trackName)
86   
87    def goToSite(self):
88        iTunes = iTunesApp()
89        if iTunes.player_state.get() == k.playing:
90            url = iTunes.current_stream_URL.get()
91            if url:
92                if 'radioparadise.com' in url:
93                    url = radioParadiseURL()
94                NSWorkspace.sharedWorkspace().openURL_(NSURL.URLWithString_(url))
95                return
96        NSBeep()
97   
98    def registerHotKey(self, func, keyCode, mods=0):
99        hotKeyRef = RegisterEventHotKey(keyCode, mods, (0, 0),
100                                        GetApplicationEventTarget(), 0)
101        self.hotKeys.append(hotKeyRef)
102        self.hotKeyActions[_StreamVision.HotKeyAddress(hotKeyRef)] = func
103
104    def finishLaunching(self):
105        super(StreamVision, self).finishLaunching()
106        self.registerHotKey(self.displayTrackInfo, 100) # F8
107        self.registerHotKey(self.goToSite, 100, cmdKey) # cmd-F8
108        self.registerHotKey(lambda: iTunesApp().playpause(), 101) # F9
109        self.registerHotKey(lambda: iTunesApp().previous_track(), 109) # F10
110        self.registerHotKey(lambda: iTunesApp().next_track(), 103) # F11
111
112    def sendEvent_(self, theEvent):
113        if theEvent.type() == NSSystemDefined and \
114               theEvent.subtype() == kEventHotKeyPressedSubtype:
115            self.hotKeyActions[theEvent.data1()]()
116        super(StreamVision, self).sendEvent_(theEvent)
117
118if __name__ == "__main__":
119    AppHelper.runEventLoop()
Note: See TracBrowser for help on using the repository browser.