move obj_to_filename() into ReplApplication and use it in weboorrents

This commit is contained in:
Romain Bignon 2014-01-16 23:56:42 +01:00
commit d2b9a0a2d2
3 changed files with 38 additions and 22 deletions

View file

@ -22,7 +22,6 @@
import subprocess
import sys
import os
import re
from weboob.capabilities.video import ICapVideo, BaseVideo
from weboob.capabilities.base import empty
@ -75,22 +74,6 @@ class Videoob(ReplApplication):
self.load_config()
return ReplApplication.main(self, argv)
def obj_to_filename(self, obj, dest, default=None):
if default is None:
default = '{id}-{title}.{ext}'
if dest is None:
dest = '.'
if os.path.isdir(dest):
dest = os.path.join(dest, default)
def repl(m):
field = m.group(1)
if hasattr(obj, field):
return re.sub('[?:/]', '-', '%s' % getattr(obj, field))
else:
return m.group(0)
return re.sub(r'\{(.+?)\}', repl, dest)
def download(self, video, dest, default=None):
if not video.url:
print >>sys.stderr, 'Error: the direct URL is not available.'
@ -124,7 +107,6 @@ class Videoob(ReplApplication):
os.spawnlp(os.P_WAIT, args[0], *args)
def complete_download(self, text, line, *ignored):
args = line.split(' ')
if len(args) == 2:

View file

@ -140,13 +140,15 @@ class Weboorrents(ReplApplication):
"""
id, dest = self.parse_command_args(line, 2, 1)
_id, backend_name = self.parse_id(id)
torrent = self.get_object(id, 'get_torrent', ('description', 'files'))
if not torrent:
print >>sys.stderr, 'Torrent not found: %s' % id
return 3
if dest is None:
dest = '%s.torrent' % _id
dest = self.obj_to_filename(torrent, dest, '{id}-{name}.torrent')
try:
for backend, buf in self.do('get_torrent_file', _id, backends=backend_name):
for backend, buf in self.do('get_torrent_file', torrent.id, backends=torrent.backend):
if buf:
if dest == '-':
print buf