save subtitles in a better file name (closes #1278)

This commit is contained in:
Romain Bignon 2013-07-27 13:33:34 +02:00
commit ca992fcafa

View file

@ -25,7 +25,6 @@ from weboob.capabilities.subtitle import ICapSubtitle
from weboob.capabilities.base import empty from weboob.capabilities.base import empty
from weboob.tools.application.repl import ReplApplication from weboob.tools.application.repl import ReplApplication
from weboob.tools.application.formatters.iformatter import IFormatter, PrettyFormatter from weboob.tools.application.formatters.iformatter import IFormatter, PrettyFormatter
from weboob.core import CallErrors
__all__ = ['Suboob'] __all__ = ['Suboob']
@ -139,30 +138,29 @@ class Suboob(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) subtitle = self.get_object(id, 'get_subtitle')
if not subtitle:
print >>sys.stderr, 'Subtitle not found: %s' % id
return 3
if dest is None: if dest is None:
dest = '%s' % _id ext = subtitle.ext
if empty(ext):
ext = 'zip'
dest = '%s.%s' % (subtitle.name, ext)
try: for backend, buf in self.do('get_subtitle_file', subtitle.id, backends=subtitle.backend):
for backend, buf in self.do('get_subtitle_file', _id, backends=backend_name): if buf:
if buf: if dest == '-':
if dest == '-': sys.stdout.write(buf)
print buf else:
else: try:
try: with open(dest, 'w') as f:
with open(dest, 'w') as f: f.write(buf)
f.write(buf) except IOError, e:
except IOError, e: print >>sys.stderr, 'Unable to write file in "%s": %s' % (dest, e)
print >>sys.stderr, 'Unable to write file in "%s": %s' % (dest, e) return 1
return 1 return
return
except CallErrors, errors:
for backend, error, backtrace in errors:
self.bcall_error_handler(backend, error, backtrace)
print >>sys.stderr, 'Subtitle "%s" not found' % id
return 3
def do_search(self, line): def do_search(self, line):
""" """