set user agent, handle api failures

This commit is contained in:
j 2013-11-03 16:50:34 +01:00
parent e191313e4f
commit 45293a128c
2 changed files with 38 additions and 26 deletions

View file

@ -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,7 +96,9 @@ class Site:
json.dump(self.config, fd, indent=1)
def seed_requests(self):
requested = self.api.getRequests()['data']['ids']
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))
@ -119,11 +126,14 @@ 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'])
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']:

View file

@ -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