Use filename from redirected URL (bug 1423)

In some cases, proper filenames hide in the final URL
of a redirect (or redirect chain) of a web server. An
example of this is the "Science Talk" podcast.
This commit is contained in:
Thomas Perl 2011-08-31 21:27:41 +02:00
parent 820b9a9cbe
commit 580056a59c
1 changed files with 10 additions and 3 deletions

View File

@ -754,8 +754,14 @@ class DownloadTask(object):
if old_extension != new_extension or util.wrong_extension(ext):
self.filename = self.__episode.local_filename(create=True, force_update=True)
# TODO: Check if "real_url" is different from "url" and if it is,
# see if we can get a better episode filename out of it
# In some cases, the redirect of a URL causes the real filename to
# be revealed in the final URL (e.g. http://gpodder.org/bug/1423)
if real_url != url:
real_filename = os.path.basename(real_url)
self.filename = self.__episode.local_filename(create=True,
force_update=True, template=real_filename)
logger.info('Download was redirected (%s). New filename: %s',
real_url, os.path.basename(self.filename))
# Look at the Content-disposition header; use if if available
disposition_filename = get_header_param(headers, \
@ -775,7 +781,8 @@ class DownloadTask(object):
# Re-evaluate filename and tempname to take care of podcast renames
# while downloads are running (which will change both file names)
self.filename = self.__episode.local_filename(create=False)
self.tempname = self.filename + '.partial'
self.tempname = os.path.join(os.path.dirname(self.filename),
os.path.basename(self.tempname))
shutil.move(self.tempname, self.filename)
# Model- and database-related updates after a download has finished