[188] | 1 | #!/usr/bin/pythonw
|
---|
| 2 | # -*- coding: utf-8 -*-
|
---|
| 3 |
|
---|
| 4 | from appscript import app, k
|
---|
| 5 | from AppKit import NSApplication, NSBeep, NSSystemDefined, NSURL, NSWorkspace
|
---|
| 6 | from PyObjCTools import AppHelper
|
---|
| 7 | from Carbon.CarbonEvt import RegisterEventHotKey, GetApplicationEventTarget
|
---|
| 8 | from Carbon.Events import cmdKey
|
---|
| 9 | import struct
|
---|
| 10 | import scrape
|
---|
| 11 | import _StreamVision
|
---|
| 12 |
|
---|
| 13 | GROWL_APP_NAME = 'StreamVision'
|
---|
| 14 | NOTIFICATION_TRACK_INFO = 'iTunes Track Info'
|
---|
| 15 | NOTIFICATIONS_ALL = [NOTIFICATION_TRACK_INFO]
|
---|
| 16 |
|
---|
| 17 | kEventHotKeyPressedSubtype = 6
|
---|
| 18 | kEventHotKeyReleasedSubtype = 9
|
---|
| 19 |
|
---|
| 20 | growl = app('GrowlHelperApp')
|
---|
| 21 |
|
---|
| 22 | growl.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 |
|
---|
| 29 | def 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 |
|
---|
| 37 | def 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 |
|
---|
| 42 | def cleanStreamName(name):
|
---|
| 43 | name = name.split('. ')[0]
|
---|
| 44 | name = name.split(': ')[0]
|
---|
[190] | 45 | name = name.split(' - ')
|
---|
| 46 | if len(name) > 1:
|
---|
| 47 | name = ' - '.join(name[:-1])
|
---|
| 48 | else:
|
---|
| 49 | name = name[0]
|
---|
[188] | 50 | return name
|
---|
| 51 |
|
---|
| 52 | def cleanStreamTitle(title):
|
---|
| 53 | if title == k.MissingValue:
|
---|
| 54 | return ''
|
---|
| 55 | title = title.replace('`', u'’')
|
---|
| 56 | return title
|
---|
| 57 |
|
---|
| 58 | def iTunesApp():
|
---|
| 59 | return app(id='com.apple.iTunes')
|
---|
| 60 |
|
---|
| 61 | class 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:
|
---|
[191] | 73 | kw = {}
|
---|
| 74 | artwork = iTunes.current_track.artworks.get()
|
---|
| 75 | if artwork:
|
---|
| 76 | kw['pictImage'] = artwork[0].data.get()
|
---|
[188] | 77 | growlNotify(iTunes.current_track.name.get(),
|
---|
| 78 | iTunes.current_track.album.get() + "\n" +
|
---|
| 79 | iTunes.current_track.artist.get(),
|
---|
[191] | 80 | **kw)
|
---|
[188] | 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 |
|
---|
| 118 | if __name__ == "__main__":
|
---|
| 119 | AppHelper.runEventLoop() |
---|