source: trunk/StreamVision/StreamVision.py@ 190

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

StreamVision.py: Don't clean stream name to oblivion if it doesn't
have at least one ' - ' in it.

props: Ignore *.pyc.

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 artwork = iTunes.current_track.artworks.data.get()
74 if artwork:
75 artwork = artwork[0]
76 else:
77 artwork = None
78 growlNotify(iTunes.current_track.name.get(),
79 iTunes.current_track.album.get() + "\n" +
80 iTunes.current_track.artist.get(),
81 pictImage=artwork)
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
119if __name__ == "__main__":
120 AppHelper.runEventLoop()
Note: See TracBrowser for help on using the repository browser.