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 torrent import TorrentClient
from utils import get_sha1, makedirs, get_user_folder from utils import get_sha1, makedirs, get_user_folder
from version import __version__, __appname__
class API(ox.API):
__version__ = __version__
__name__ = __appname__
class Backend: class Backend:
sites = {} sites = {}
@ -52,14 +57,14 @@ class Site:
def add_requests(self, ids): def add_requests(self, ids):
r = self.api.addRequests(ids=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']: for item in r['data']['metadata']:
self.download(item['sha1'], item['info_hash']) self.download(item['sha1'], item['info_hash'])
def connect(self): def connect(self):
self.connected = False self.connected = False
if self.config.get('url'): if self.config.get('url'):
self.api = ox.API(self.config['url']) self.api = API(self.config['url'])
if 'username' in self.config: if 'username' in self.config:
r = self.api.signin(username=self.config['username'], password=self.config['password']) r = self.api.signin(username=self.config['username'], password=self.config['password'])
self.connected = r.get('status', {}).get('code') == 200 self.connected = r.get('status', {}).get('code') == 200
@ -91,13 +96,15 @@ class Site:
json.dump(self.config, fd, indent=1) json.dump(self.config, fd, indent=1)
def seed_requests(self): def seed_requests(self):
requested = self.api.getRequests()['data']['ids'] r = self.api.getRequests()
for sha1 in requested: if 'data' in r and 'ids' in r['data']:
if sha1 not in self.t.handles: requested = r['data']['ids']
self.t.add_torrent(self.t.torrent_path(sha1)) for sha1 in requested:
for sha1 in self.t.handles.keys(): if sha1 not in self.t.handles:
if sha1 not in self.t.downloads and sha1 not in requested: self.t.add_torrent(self.t.torrent_path(sha1))
self.t.remove(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): def sync(self):
# dont scan if path does not exist at all, # dont scan if path does not exist at all,
@ -119,23 +126,26 @@ class Site:
tinfo = self.t.make_torrent(f['path'], f['sha1']) tinfo = self.t.make_torrent(f['path'], f['sha1'])
f['info_hash'] = tinfo.info_hash().to_string().encode('hex') f['info_hash'] = tinfo.info_hash().to_string().encode('hex')
known = set(self.api.sync()['data']['ids']) r = self.api.sync()
files = list(set([f['sha1'] for f in current.values()])) if 'data' in r and 'ids' in r['data']:
removed = list(known - set(files)) known = set(r['data']['ids'])
added = list(set([f['sha1'] for f in current.values() if f['sha1'] not in known])) files = list(set([f['sha1'] for f in current.values()]))
r = self.api.sync({'add': added, 'remove': removed}) removed = list(known - set(files))
update = [] added = list(set([f['sha1'] for f in current.values() if f['sha1'] not in known]))
by_sha1 = {f['sha1']: f for f in current.values()} r = self.api.sync({'add': added, 'remove': removed})
for sha1 in r['data']['update']: if 'data' in r and 'update' in r['data']:
f = by_sha1[sha1] update = []
i = {} by_sha1 = {f['sha1']: f for f in current.values()}
for key in ('sha1', 'extension', 'info_hash', 'size'): for sha1 in r['data']['update']:
i[key] = f[key] f = by_sha1[sha1]
i['title'] = os.path.basename(f['path']) i = {}
update.append(i) for key in ('sha1', 'extension', 'info_hash', 'size'):
if update: i[key] = f[key]
r = self.api.sync({'update': update}) i['title'] = os.path.basename(f['path'])
self.save_config() update.append(i)
if update:
r = self.api.sync({'update': update})
self.save_config()
def info(path): def info(path):

View file

@ -53,6 +53,8 @@ class TorrentClient:
save_path = os.path.dirname(torrent_path) save_path = os.path.dirname(torrent_path)
atp = {} atp = {}
atp['url'] = 'magnet:?xt=urn:btih:%s&dn=%s&tr=%s' % (info_hash, sha1, quote(TRACKER_URL)) 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["save_path"] = save_path
atp["storage_mode"] = lt.storage_mode_t.storage_mode_sparse atp["storage_mode"] = lt.storage_mode_t.storage_mode_sparse
atp["paused"] = False atp["paused"] = False