]> git.llucax.com Git - software/subdivxget.git/blobdiff - subdivxget
Use lowercase for help messages to match --help
[software/subdivxget.git] / subdivxget
index e938ac8e3b01c1d2254134922889f98573619c3b..59971140ba6957dd771a575455f6b51d50bc3d00 100755 (executable)
@@ -23,6 +23,9 @@ def output(fo, fmt, *args, **kargs):
        fo.write((fmt % args) + '\n')
 
 def echo(fmt, *args, **kargs):
        fo.write((fmt % args) + '\n')
 
 def echo(fmt, *args, **kargs):
+       global opts
+       if opts.quiet:
+               return
        output(sys.stdout, fmt, *args, **kargs)
 
 def error(fmt, *args, **kargs):
        output(sys.stdout, fmt, *args, **kargs)
 
 def error(fmt, *args, **kargs):
@@ -218,8 +221,22 @@ def subdivx_get_subs(query_str):
        return subs
 
 
        return subs
 
 
-def get_subs(query_str, filters):
+def unzip_subs(fname):
        sub_exts = ('.srt', '.sub')
        sub_exts = ('.srt', '.sub')
+       z = zipfile.ZipFile(fname, 'r')
+       z.printdir()
+       for fn in z.namelist():
+               if fn.endswith(sub_exts):
+                       if '..' in fn or fn.startswith('/'):
+                               error('Ignoring file with dangerous name: %s',
+                                               fn)
+                               continue
+                       echo('Extracting %s...', fn)
+                       z.extract(fn)
+
+
+def get_subs(query_str, filters):
+       global opts
        zip_exts = ('application/zip',)
        rar_exts = ('application/rar', 'application/x-rar-compressed')
 
        zip_exts = ('application/zip',)
        rar_exts = ('application/rar', 'application/x-rar-compressed')
 
@@ -233,18 +250,12 @@ def get_subs(query_str, filters):
   %(desc)s
        DOWNLOADING ...
 ''', **sub)
   %(desc)s
        DOWNLOADING ...
 ''', **sub)
+               if opts.list_only:
+                       continue
                fname, headers = urlretrieve(sub['url'])
                if 'Content-Type' in headers:
                        if headers['Content-Type'] in zip_exts:
                fname, headers = urlretrieve(sub['url'])
                if 'Content-Type' in headers:
                        if headers['Content-Type'] in zip_exts:
-                               z = zipfile.ZipFile(fname, 'r')
-                               z.printdir()
-                               for fn in z.namelist():
-                                       if fn.endswith(sub_exts):
-                                               if '..' in fn or fn.startswith('/'):
-                                                       error('Dangerous file name: %s', fn)
-                                                       continue
-                                               echo('Extracting %s...', fn)
-                                               z.extract(fn)
+                               unzip_subs(fname)
                        elif headers['Content-Type'] in rar_exts:
                                if subprocess.call(['rar', 'x', fname]) != 0:
                                        error('Error unraring file %s', fname)
                        elif headers['Content-Type'] in rar_exts:
                                if subprocess.call(['rar', 'x', fname]) != 0:
                                        error('Error unraring file %s', fname)
@@ -255,5 +266,37 @@ def get_subs(query_str, filters):
                        error('No Content-Type!')
 
 
                        error('No Content-Type!')
 
 
-get_subs(sys.argv[1], sys.argv[2:])
+def parse_args(argv):
+       from optparse import OptionParser
+       parser = OptionParser(usage="%prog [OPTIONS] QUERY [FILTER ...]",
+                       description="""
+Download subtitles from subdivx.com searching the string QUERY. If FILTERs are
+specified, only subtitles that matches all those filters are downloaded.
+Filters have the format "X:fitler", where X is a field specification: t=titulo,
+d=desc, a=autor, f=formato, c=comentarios, C=cds, F=fecha and D=downloads.
+filter is a string that should be found on that field (case insensitive). If
+the format specifier is not known (or there isn't one) the filter string is
+looked in all the fields.
+                       """.strip())
+       parser.add_option("-l", "--list-only",
+                       default=False, action='store_true',
+                       help="don't download the subtitles, just list them")
+       parser.add_option("-q", "--quiet",
+                       default=False, action='store_true',
+                       help="don't print progress messages")
+
+       (opts, args) = parser.parse_args()
+       if not args:
+              parser.error("Missing query string")
+
+       if opts.quiet and opts.list_only:
+               parser.error("Using --quiet and --list-only together doesn't "
+                               "make any sense")
+
+       return (args[0], args[1:], opts)
+
+(query_str, filters, opts) = parse_args(sys.argv)
+
+get_subs(query_str, filters)
+