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 subprocess
import sys import sys
import os import os
import re
from weboob.capabilities.video import ICapVideo, BaseVideo from weboob.capabilities.video import ICapVideo, BaseVideo
from weboob.capabilities.base import empty from weboob.capabilities.base import empty
@ -75,22 +74,6 @@ class Videoob(ReplApplication):
self.load_config() self.load_config()
return ReplApplication.main(self, argv) 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): def download(self, video, dest, default=None):
if not video.url: if not video.url:
print >>sys.stderr, 'Error: the direct URL is not available.' 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) os.spawnlp(os.P_WAIT, args[0], *args)
def complete_download(self, text, line, *ignored): def complete_download(self, text, line, *ignored):
args = line.split(' ') args = line.split(' ')
if len(args) == 2: if len(args) == 2:

View file

@ -140,13 +140,15 @@ class Weboorrents(ReplApplication):
""" """
id, dest = self.parse_command_args(line, 2, 1) 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 = self.obj_to_filename(torrent, dest, '{id}-{name}.torrent')
dest = '%s.torrent' % _id
try: 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 buf:
if dest == '-': if dest == '-':
print buf print buf

View file

@ -22,6 +22,7 @@ import atexit
from cmd import Cmd from cmd import Cmd
import logging import logging
import locale import locale
import re
from optparse import OptionGroup, OptionParser, IndentedHelpFormatter from optparse import OptionGroup, OptionParser, IndentedHelpFormatter
import os import os
import sys import sys
@ -1097,6 +1098,37 @@ class ReplApplication(Cmd, ConsoleApplication):
obj_collections = [obj for obj in self.objects if isinstance(obj, BaseCollection)] obj_collections = [obj for obj in self.objects if isinstance(obj, BaseCollection)]
return obj_collections + self.collections return obj_collections + self.collections
def obj_to_filename(self, obj, dest=None, default=None):
"""
This method can be used to get a filename from an object, using a mask
filled by information of this object.
All patterns are braces-enclosed, and are name of available fields in
the object.
:param obj: object
:type obj: CapBaseObject
:param dest: dest given by user (default None)
:type dest: str
:param default: default file mask (if not given, this is '{id}-{title}.{ext}')
:type default: str
:rtype: str
"""
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)
# for cd & ls # for cd & ls
def complete_path(self, text, line, begidx, endidx): def complete_path(self, text, line, begidx, endidx):
directories = set() directories = set()