source: trunk/StreamVision/StreamVision.py @ 188

Last change on this file since 188 was 188, checked in by Nicholas Riley, 14 years ago

StreamVision?

File size: 3.9 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 = ' - '.join(name.split(' - ')[:-1])
46    return name
47   
48def cleanStreamTitle(title):
49    if title == k.MissingValue:
50        return ''
51    title = title.replace('`', u'’')
52    return title
53   
54def iTunesApp():
55    return app(id='com.apple.iTunes')
56
57class StreamVision(NSApplication):
58
59    hotKeyActions = {}
60    hotKeys = []
61
62    def displayTrackInfo(self):
63        iTunes = iTunesApp()
64        if iTunes.player_state.get() == k.playing:
65            if iTunes.current_track.class_.get() == k.URL_track:
66                growlNotify(cleanStreamTitle(iTunes.current_stream_title.get()),
67                            cleanStreamName(iTunes.current_track.name.get()))
68            else:
69                artwork = iTunes.current_track.artworks.data.get()
70                if artwork:
71                    artwork = artwork[0]
72                else:
73                    artwork = None
74                growlNotify(iTunes.current_track.name.get(),
75                            iTunes.current_track.album.get() + "\n" +
76                            iTunes.current_track.artist.get(),
77                            pictImage=artwork)
78        else:
79            trackName = ''
80            if iTunes.current_track.class_.get() != k.Property:
81                trackName = iTunes.current_track.name.get()
82            growlNotify("iTunes is not playing.", trackName)
83   
84    def goToSite(self):
85        iTunes = iTunesApp()
86        if iTunes.player_state.get() == k.playing:
87            url = iTunes.current_stream_URL.get()
88            if url:
89                if 'radioparadise.com' in url:
90                    url = radioParadiseURL()
91                NSWorkspace.sharedWorkspace().openURL_(NSURL.URLWithString_(url))
92                return
93        NSBeep()
94   
95    def registerHotKey(self, func, keyCode, mods=0):
96        hotKeyRef = RegisterEventHotKey(keyCode, mods, (0, 0),
97                                        GetApplicationEventTarget(), 0)
98        self.hotKeys.append(hotKeyRef)
99        self.hotKeyActions[_StreamVision.HotKeyAddress(hotKeyRef)] = func
100
101    def finishLaunching(self):
102        super(StreamVision, self).finishLaunching()
103        self.registerHotKey(self.displayTrackInfo, 100) # F8
104        self.registerHotKey(self.goToSite, 100, cmdKey) # cmd-F8
105        self.registerHotKey(lambda: iTunesApp().playpause(), 101) # F9
106        self.registerHotKey(lambda: iTunesApp().previous_track(), 109) # F10
107        self.registerHotKey(lambda: iTunesApp().next_track(), 103) # F11
108
109    def sendEvent_(self, theEvent):
110        if theEvent.type() == NSSystemDefined and \
111               theEvent.subtype() == kEventHotKeyPressedSubtype:
112            self.hotKeyActions[theEvent.data1()]()
113        super(StreamVision, self).sendEvent_(theEvent)
114
115if __name__ == "__main__":
116    AppHelper.runEventLoop()
Note: See TracBrowser for help on using the repository browser.