rewritten weboorrents

This commit is contained in:
Romain Bignon 2010-06-30 23:42:17 +02:00
commit e908d862c8

View file

@ -17,7 +17,7 @@
from __future__ import with_statement from __future__ import with_statement
import logging import sys
from weboob.capabilities.torrent import ICapTorrent from weboob.capabilities.torrent import ICapTorrent
from weboob.tools.application import ConsoleApplication from weboob.tools.application import ConsoleApplication
@ -36,48 +36,33 @@ class Weboorrents(ConsoleApplication):
self.load_backends(ICapTorrent) self.load_backends(ICapTorrent)
return self.process_command(*argv[1:]) return self.process_command(*argv[1:])
def split_id(self, id):
if not '.' in id:
logging.error('ID must be in form <backend>.<ID>')
return None, None
backend_name, id = id.split('.', 1)
backend = self.weboob.backends.get(backend_name, None)
if not backend:
logging.error('Backends "%s" not found' % backend_name)
return None, None
return backend, id
@ConsoleApplication.command('Get information about a torrent') @ConsoleApplication.command('Get information about a torrent')
def command_info(self, id): def command_info(self, id):
backend, id = self.split_id(id) _id, backend_name = self.parse_id(id)
if not backend:
return 1 found = 0
with backend: for backend, torrent in self.weboob.do_backends(backend_name, 'get_torrent', _id):
torrent = backend.get_torrent(id) if torrent:
if not torrent: self.format(torrent, backend.name)
logging.error('Torrent "%s" not found' % id) found = 1
return 1
self.format(torrent, backend.name) if not found:
print >>sys.stderr, 'Torrent "%s" not found' % id
@ConsoleApplication.command('Get the torrent file') @ConsoleApplication.command('Get the torrent file')
def command_getfile(self, id, dest): def command_getfile(self, id, dest):
backend, id = self.split_id(id) _id, backend_name = self.parse_id(id)
if not backend:
return 1
with backend: for backend, buf in self.weboob.do_backends(backend_name, 'get_torrent_file', _id):
s = backend.get_torrent_file(id) if buf:
if not s: if dest == '-':
logging.error('Torrent "%s" not found' % id) print buf
return 1 else:
with open(dest, 'w') as f:
f.write(buf)
return
if dest == '-': print >>sys.stderr, 'Torrent "%s" not found' % id
print s
else:
with open(dest, 'w') as f:
f.write(s)
@ConsoleApplication.command('Search torrents') @ConsoleApplication.command('Search torrents')
def command_search(self, pattern=None): def command_search(self, pattern=None):