source: trunk/StreamVision/StreamVision.py@ 194

Last change on this file since 194 was 194, checked in by Nicholas Riley, 17 years ago

StreamVision.py: old changes for better name cleaning

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