Support magnet URLs in weboorents

This commit is contained in:
Laurent Bachelier 2012-03-09 02:48:34 +01:00
commit ee04a92f04
2 changed files with 30 additions and 13 deletions

View file

@ -21,16 +21,17 @@ from __future__ import with_statement
import sys import sys
from weboob.capabilities.torrent import ICapTorrent from weboob.capabilities.torrent import ICapTorrent, MagnetOnly
from weboob.tools.application.repl import ReplApplication from weboob.tools.application.repl import ReplApplication
from weboob.tools.application.formatters.iformatter import IFormatter from weboob.tools.application.formatters.iformatter import IFormatter
from weboob.core import CallErrors
__all__ = ['Weboorrents'] __all__ = ['Weboorrents']
def sizeof_fmt(num): def sizeof_fmt(num):
for x in ['bytes','KB','MB','GB','TB']: for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
if num < 1024.0: if num < 1024.0:
return "%-4.1f%s" % (num, x) return "%-4.1f%s" % (num, x)
num /= 1024.0 num /= 1024.0
@ -142,18 +143,28 @@ class Weboorrents(ReplApplication):
if dest is None: if dest is None:
dest = '%s.torrent' % _id dest = '%s.torrent' % _id
for backend, buf in self.do('get_torrent_file', _id, backends=backend_name): try:
if buf: for backend, buf in self.do('get_torrent_file', _id, backends=backend_name):
if dest == '-': if buf:
print buf if dest == '-':
print buf
else:
try:
with open(dest, 'w') as f:
f.write(buf)
except IOError, e:
print >>sys.stderr, 'Unable to write .torrent in "%s": %s' % (dest, e)
return 1
return
except CallErrors, errors:
for backend, error, backtrace in errors:
if isinstance(error, MagnetOnly):
print >>sys.stderr, u'Error(%s): No direct URL available, ' \
u'please provide this magnet URL ' \
u'to your client:\n%s' % (backend, error.magnet)
return 4
else: else:
try: self.bcall_error_handler(backend, error, backtrace)
with open(dest, 'w') as f:
f.write(buf)
except IOError, e:
print >>sys.stderr, 'Unable to write .torrent in "%s": %s' % (dest, e)
return 1
return
print >>sys.stderr, 'Torrent "%s" not found' % id print >>sys.stderr, 'Torrent "%s" not found' % id
return 3 return 3

View file

@ -25,6 +25,12 @@ from .base import IBaseCap, CapBaseObject
__all__ = ['ICapTorrent', 'Torrent'] __all__ = ['ICapTorrent', 'Torrent']
class MagnetOnly(Exception):
def __init__(self, magnet):
self.magnet = magnet
Exception.__init__(self, 'Only magnet URL is available')
class Torrent(CapBaseObject): class Torrent(CapBaseObject):
def __init__(self, id, name): def __init__(self, id, name):
CapBaseObject.__init__(self, id) CapBaseObject.__init__(self, id)