def get_download_filename(url, cookie_file=None):
 from calibre import browser
 from contextlib import closing
 filename = ''
 br = browser()
 if cookie_file:
  from mechanize import MozillaCookieJar
  cj = MozillaCookieJar()
  cj.load(cookie_file)
  br.set_cookiejar(cj)
 try:
  with closing(br.open(url)) as r:
   filename = get_download_filename_from_response(r)
 except:
  import traceback
  traceback.print_exc()
 return filename

def get_download_filename ( url , cookie_file = None ) : from calibre import browser from contextlib import closing filename = '' br = browser ( ) if cookie_file : from mechanize import MozillaCookieJar cj = MozillaCookieJar ( ) cj . load ( cookie_file ) br . set_cookiejar ( cj ) try : with closing ( br . open ( url ) ) as r : filename = get_download_filename_from_response ( r ) except : import traceback traceback . print_exc ( ) return filename
get a local filename for a url using the content disposition header returns empty string if an error occurs .