Changeset 486 for trunk/StreamVision


Ignore:
Timestamp:
10/24/08 18:23:00 (15 years ago)
Author:
Nicholas Riley
Message:

StreamVision.py: Support Radio Paradise album cover retrieval.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/StreamVision/StreamVision.py

    r459 r486  
    44from appscript import app, k, its, CommandError
    55from AppKit import NSApplication, NSApplicationDefined, NSBeep, NSSystemDefined, NSURL, NSWorkspace
    6 from Foundation import NSDistributedNotificationCenter
     6from Foundation import NSDistributedNotificationCenter, NSSearchPathForDirectoriesInDomains, NSCachesDirectory, NSUserDomainMask
    77from PyObjCTools import AppHelper
    88from Carbon.CarbonEvt import RegisterEventHotKey, GetApplicationEventTarget
    99from Carbon.Events import cmdKey, shiftKey, controlKey
     10import httplib2
     11import os
    1012import struct
    1113import scrape
     
    98100    return AmuaApp().is_playing()
    99101
     102class OneFileCache(object):
     103    __slots__ = ('key', 'cache')
     104
     105    def __init__(self, cache):
     106        if not os.path.exists(cache):
     107            os.makedirs(cache)
     108        self.cache = os.path.join(cache, 'file')
     109        self.key = None
     110
     111    def get(self, key):
     112        if key == self.key:
     113            return file(self.cache, 'r').read()
     114
     115    def set(self, key, value):
     116        self.key = key
     117        file(self.cache, 'w').write(value)
     118
     119    def delete(self, key):
     120        if key == self.key:
     121            self.key = None
     122            os.remove(cache)
     123
    100124class StreamVision(NSApplication):
    101125
     
    129153                    AmuaApp().display_song_information()
    130154                return
     155            url = iTunes.current_stream_URL()
     156            kw = {}
     157            if url != k.missing_value and url.endswith('.jpg'):
     158                response, content = self.http.request(url)
     159                if response['content-type'].startswith('image/'):
     160                    file(self.imagePath, 'w').write(content)
     161                    kw['image_from_location'] = self.imagePath
    131162            growlNotify(cleanStreamTitle(iTunes.current_stream_title()),
    132                         cleanStreamTrackName(trackName))
     163                        cleanStreamTrackName(trackName), **kw)
    133164            return
    134165        if trackClass == k.property:
     
    269300    def finishLaunching(self):
    270301        super(StreamVision, self).finishLaunching()
     302
     303        caches = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
     304                                                     NSUserDomainMask, True)[0]
     305        cache = os.path.join(caches, 'StreamVision')
     306        self.http = httplib2.Http(OneFileCache(cache), 5)
     307        self.imagePath = os.path.join(cache, 'image')
     308
    271309        self.registerHotKey(self.displayTrackInfo, 100) # F8
    272310        self.registerHotKey(self.goToSite, 100, cmdKey) # cmd-F8
Note: See TracChangeset for help on using the changeset viewer.