From ab00769f7d662c7388c962f5b27aa14f2c9bf5d4 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 29 Dec 2011 01:26:35 +0530 Subject: [PATCH] enable offline extraction, use profile from config if provided --- bin/pandora_client | 6 +++++- config.example.json | 1 + pandora_client/__init__.py | 25 ++++++++++++++----------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/bin/pandora_client b/bin/pandora_client index 8ccd639..6a4731b 100755 --- a/bin/pandora_client +++ b/bin/pandora_client @@ -31,6 +31,10 @@ if __name__ == '__main__': action = args[0] - c = pandora_client.Client(opts.config) + if action == 'extract': + offline = True + else: + offline = False + c = pandora_client.Client(opts.config, offline) getattr(c, action)() diff --git a/config.example.json b/config.example.json index e938f38..5649b32 100644 --- a/config.example.json +++ b/config.example.json @@ -1,5 +1,6 @@ { "url": "http://pandora_url/api/", + "profile": "480p.webm", "username": "username", "password": "password", "cache": "~/.ox/client.sqlite", "volumes": { diff --git a/pandora_client/__init__.py b/pandora_client/__init__.py index 507f47b..1e66406 100644 --- a/pandora_client/__init__.py +++ b/pandora_client/__init__.py @@ -50,15 +50,17 @@ def encode(filename, prefix, profile, info=None): } class Client(object): - def __init__(self, config): + def __init__(self, config, offline=False): if isinstance(config, basestring): with open(config) as f: self._config = json.load(f) else: self._config = config - self.api = API(self._config['url'], media_cache=self.media_cache()) - self.api.DEBUG = DEBUG - self.signin() + + self.profile = self._config.get('profile', '480p.webm') + + if not offline: + self.online() conn, c = self._conn() @@ -93,9 +95,6 @@ class Client(object): conn.text_factory = sqlite3.OptimizedUnicode return conn, conn.cursor() - def get_profile(self): - return "%sp.webm" % max(self.api._config['video']['resolutions']) - def media_cache(self): return os.path.expanduser(self._config.get('media-cache', default_media_cache)) @@ -126,6 +125,12 @@ class Client(object): paths.append(row[0]) return paths + def online(self): + self.api = API(self._config['url'], media_cache=self.media_cache()) + self.api.DEBUG = DEBUG + self.signin() + self.profile = "%sp.webm" % max(self.api._config['video']['resolutions']) + def signin(self): if 'username' in self._config: r = self.api.signin(username=self._config['username'], password=self._config['password']) @@ -213,7 +218,6 @@ class Client(object): else: volumes[name]['available'] = False - profile = self.get_profile() for name in volumes: if volumes[name]['available']: prefix = volumes[name]['path'] @@ -226,7 +230,7 @@ class Client(object): '/extras' not in filename.lower() and \ '/versions' not in filename.lower(): print filename.encode('utf-8') - i = encode(filename, self.media_cache(), profile, info) + i = encode(filename, self.media_cache(), self.profile, info) def sync(self): if not self.user: @@ -318,7 +322,6 @@ class Client(object): else: volumes[name]['available'] = False - profile = self.get_profile() for name in volumes: if volumes[name]['available']: prefix = volumes[name]['path'] @@ -346,7 +349,7 @@ class Client(object): filename = filenames[oshash] info = files['info'][oshash] if not self.api.uploadVideo(os.path.join(prefix, filename), - data, profile, info): + data, self.profile, info): if not self.signin(): print "failed to login again" return