mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Ensure filename is always set and report any errors while trying to get the filename from a url.
This commit is contained in:
parent
77a9964fac
commit
5de6672807
@ -519,23 +519,28 @@ def get_download_filename(url, cookie_file=None):
|
|||||||
cj.load(cookie_file)
|
cj.load(cookie_file)
|
||||||
br.set_cookiejar(cj)
|
br.set_cookiejar(cj)
|
||||||
|
|
||||||
with closing(br.open(url)) as r:
|
try:
|
||||||
disposition = r.info().get('Content-disposition', '')
|
with closing(br.open(url)) as r:
|
||||||
for p in disposition.split(';'):
|
disposition = r.info().get('Content-disposition', '')
|
||||||
if 'filename' in p:
|
for p in disposition.split(';'):
|
||||||
if '*=' in disposition:
|
if 'filename' in p:
|
||||||
parts = disposition.split('*=')[-1]
|
if '*=' in disposition:
|
||||||
filename = parts.split('\'')[-1]
|
parts = disposition.split('*=')[-1]
|
||||||
else:
|
filename = parts.split('\'')[-1]
|
||||||
filename = disposition.split('=')[-1]
|
else:
|
||||||
if filename[0] in ('\'', '"'):
|
filename = disposition.split('=')[-1]
|
||||||
filename = filename[1:]
|
if filename[0] in ('\'', '"'):
|
||||||
if filename[-1] in ('\'', '"'):
|
filename = filename[1:]
|
||||||
filename = filename[:-1]
|
if filename[-1] in ('\'', '"'):
|
||||||
filename = urllib2_unquote(filename)
|
filename = filename[:-1]
|
||||||
break
|
filename = urllib2_unquote(filename)
|
||||||
if not filename:
|
break
|
||||||
filename = r.geturl().split('/')[-1]
|
except:
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
if not filename:
|
||||||
|
filename = r.geturl().split('/')[-1]
|
||||||
|
|
||||||
return filename
|
return filename
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user