source: trunk/StreamVision/StreamVision.py@ 192

Last change on this file since 192 was 192, checked in by Nicholas Riley, 19 years ago

StreamVision.py: added distributed notification support for iTunes track changes

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