From caba2c81d03b370cb1781a71f6b3e74b91c112ad Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Sat, 14 Jul 2012 20:08:40 +0200 Subject: [PATCH] Move unzipping to a separate function --- subdivxget | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/subdivxget b/subdivxget index e938ac8..1badd17 100755 --- a/subdivxget +++ b/subdivxget @@ -218,8 +218,21 @@ def subdivx_get_subs(query_str): return subs -def get_subs(query_str, filters): +def unzip_subs(fname): 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): zip_exts = ('application/zip',) rar_exts = ('application/rar', 'application/x-rar-compressed') @@ -236,15 +249,7 @@ def get_subs(query_str, filters): 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) -- 2.43.0