From 45293a128c62cca4b275fe92467343e0411e95e4 Mon Sep 17 00:00:00 2001 From: j Date: Sun, 3 Nov 2013 16:50:34 +0100 Subject: [PATCH] set user agent, handle api failures --- lsd/backend.py | 62 +++++++++++++++++++++++++++++--------------------- lsd/torrent.py | 2 ++ 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/lsd/backend.py b/lsd/backend.py index 35b223b..231b080 100644 --- a/lsd/backend.py +++ b/lsd/backend.py @@ -12,6 +12,11 @@ import ox from torrent import TorrentClient from utils import get_sha1, makedirs, get_user_folder +from version import __version__, __appname__ + +class API(ox.API): + __version__ = __version__ + __name__ = __appname__ class Backend: sites = {} @@ -52,14 +57,14 @@ class Site: def add_requests(self, ids): r = self.api.addRequests(ids=ids) - if r['data'] and r['data']['metadata']: + if 'data' in r and 'metadata' in r['data']: for item in r['data']['metadata']: self.download(item['sha1'], item['info_hash']) def connect(self): self.connected = False if self.config.get('url'): - self.api = ox.API(self.config['url']) + self.api = API(self.config['url']) if 'username' in self.config: r = self.api.signin(username=self.config['username'], password=self.config['password']) self.connected = r.get('status', {}).get('code') == 200 @@ -91,13 +96,15 @@ class Site: json.dump(self.config, fd, indent=1) def seed_requests(self): - requested = self.api.getRequests()['data']['ids'] - for sha1 in requested: - if sha1 not in self.t.handles: - self.t.add_torrent(self.t.torrent_path(sha1)) - for sha1 in self.t.handles.keys(): - if sha1 not in self.t.downloads and sha1 not in requested: - self.t.remove(sha1) + r = self.api.getRequests() + if 'data' in r and 'ids' in r['data']: + requested = r['data']['ids'] + for sha1 in requested: + if sha1 not in self.t.handles: + self.t.add_torrent(self.t.torrent_path(sha1)) + for sha1 in self.t.handles.keys(): + if sha1 not in self.t.downloads and sha1 not in requested: + self.t.remove(sha1) def sync(self): # dont scan if path does not exist at all, @@ -119,23 +126,26 @@ class Site: tinfo = self.t.make_torrent(f['path'], f['sha1']) f['info_hash'] = tinfo.info_hash().to_string().encode('hex') - known = set(self.api.sync()['data']['ids']) - files = list(set([f['sha1'] for f in current.values()])) - removed = list(known - set(files)) - added = list(set([f['sha1'] for f in current.values() if f['sha1'] not in known])) - r = self.api.sync({'add': added, 'remove': removed}) - update = [] - by_sha1 = {f['sha1']: f for f in current.values()} - for sha1 in r['data']['update']: - f = by_sha1[sha1] - i = {} - for key in ('sha1', 'extension', 'info_hash', 'size'): - i[key] = f[key] - i['title'] = os.path.basename(f['path']) - update.append(i) - if update: - r = self.api.sync({'update': update}) - self.save_config() + r = self.api.sync() + if 'data' in r and 'ids' in r['data']: + known = set(r['data']['ids']) + files = list(set([f['sha1'] for f in current.values()])) + removed = list(known - set(files)) + added = list(set([f['sha1'] for f in current.values() if f['sha1'] not in known])) + r = self.api.sync({'add': added, 'remove': removed}) + if 'data' in r and 'update' in r['data']: + update = [] + by_sha1 = {f['sha1']: f for f in current.values()} + for sha1 in r['data']['update']: + f = by_sha1[sha1] + i = {} + for key in ('sha1', 'extension', 'info_hash', 'size'): + i[key] = f[key] + i['title'] = os.path.basename(f['path']) + update.append(i) + if update: + r = self.api.sync({'update': update}) + self.save_config() def info(path): diff --git a/lsd/torrent.py b/lsd/torrent.py index b7e6991..8bbd4d9 100644 --- a/lsd/torrent.py +++ b/lsd/torrent.py @@ -53,6 +53,8 @@ class TorrentClient: save_path = os.path.dirname(torrent_path) atp = {} atp['url'] = 'magnet:?xt=urn:btih:%s&dn=%s&tr=%s' % (info_hash, sha1, quote(TRACKER_URL)) + if isinstance(atp['url'], unicode): + atp['url'] = atp['url'].encode('utf-8') atp["save_path"] = save_path atp["storage_mode"] = lt.storage_mode_t.storage_mode_sparse atp["paused"] = False