From ce7c309e812bd6f0b02f7d5d0f68ae3b00d0f9ff Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sat, 27 Nov 2010 12:35:56 +0100 Subject: [PATCH] make media-cache config option --- README | 1 + pandora_client/__init__.py | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README b/README index a940ba0..dfa5f39 100644 --- a/README +++ b/README @@ -11,6 +11,7 @@ pandora client example: "username": "username", "password": "password", "cache": "~/.ox/client.sqlite", + "media-cache": "~/.ox/media", "volumes": { "volumename": "/media/2010/Movies" } diff --git a/pandora_client/__init__.py b/pandora_client/__init__.py index 4d663dc..519302d 100644 --- a/pandora_client/__init__.py +++ b/pandora_client/__init__.py @@ -20,9 +20,9 @@ import utils __version__ = '0.1' DEBUG = True -prefix = os.environ.get('oxMEDIA', os.path.expanduser('~/.ox/media')) +default_media_cache = os.environ.get('oxMEDIA', os.path.expanduser('~/.ox/media')) -def encode(filename, profile): +def encode(filename, prefix, profile): info = utils.avinfo(filename) oshash = info['oshash'] frames = [] @@ -49,10 +49,10 @@ class Client(object): def __init__(self, config): if isinstance(config, basestring): with open(config) as f: - self._config = json.loads(f.read()) + self._config = json.load(f) else: self.config = config - self.api = API(self._config['url']) + self.api = API(self._config['url'], media_cache=self.media_cache()) if 'username' in self._config: r = self.api.login({'username':self._config['username'], 'password':self._config['password']}) @@ -92,6 +92,9 @@ class Client(object): conn.text_factory = sqlite3.OptimizedUnicode return conn, conn.cursor() + def media_cache(self): + return os.path.expanduser(self._config.get('media-cache', default_media_cache)) + def get(self, key, default=None): conn, c = self._conn() c.execute('SELECT value FROM setting WHERE key = ?', (key, )) @@ -231,11 +234,11 @@ class Client(object): def clean(self): print "remove temp videos and stills" - if os.path.exists(prefix): - shutil.rmtree(prefix) + if os.path.exists(self.prefix()): + shutil.rmtree(self.prefix()) class API(object): - def __init__(self, url, cj=None): + def __init__(self, url, cj=None, media_cache=None): if cj: self._cj = cj else: @@ -243,6 +246,10 @@ class API(object): self._opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self._cj)) urllib2.install_opener(self._opener) + self.media_cache = media_cache + if not self.media_cache: + self.media_cache = default_media_cache + self.url = url r = self._request('api', {}) self._actions = r['data']['actions'] @@ -297,7 +304,7 @@ class API(object): return self._json_request(self.url, form) def uploadVideo(self, filename, data, profile): - i = encode(filename, profile) + i = encode(filename, self.media_cache, profile) #upload frames form = ox.MultiPartForm()