Commits

anatoly techtonik committed 5196895

it detects filename from HTTP headers

  • Participants
  • Parent commits 67b4d0d
  • Tags 0.8

Comments (0)

Files changed (3)

 ChangeLog
 =========
+0.8 (2011-05-03)
+ * it detects filename from HTTP headers
+
 0.7 (2011-03-01)
  * compatibility fix for Python 2.5
  * limit width of progress bar to 100 chars
 
 setup(
     name='wget',
-    version='0.7',
+    version='0.8',
     author='anatoly techtonik <techtonik@gmail.com>',
     url='http://bitbucket.org/techtonik/python-wget/',
 
     long_description= """
 ChangeLog
 =========
+0.8 (2011-05-03)
+ * it detects filename from HTTP headers
+
 0.7 (2011-03-01)
  * compatibility fix for Python 2.5
  * limit width of progress bar to 100 chars
 import math
 
 
-__version__ = "0.8dev"
+__version__ = "0.8"
 
 
 def filename_from_url(url):
         return None
     return fname
 
+def filename_from_headers(headers):
+    """Detect filename from Content-Disposition headers if present.
+   http://greenbytes.de/tech/tc2231/
+
+    :param: headers as dict, list or string
+    :return: filename from content-disposition header or None
+    """
+    if type(headers) in (str, unicode):
+        headers = headers.splitlines()
+    if type(headers) == list:
+        headers = dict([x.split(':', 1) for x in headers])
+    cdisp = headers.get("Content-Disposition")
+    if not cdisp:
+        return None
+    cdtype = cdisp.split(';')
+    if len(cdtype) == 1:
+        return None
+    if cdtype[0].strip().lower() not in ('inline', 'attachment'):
+        return None
+    # several filename params is illegal, but just in case
+    fnames = [x for x in cdtype[1:] if x.strip().startswith('filename=')]
+    if len(fnames) > 1:
+        return None
+    name = fnames[0].split('=')[1].strip(' \t"')
+    name = os.path.basename(name)
+    if not name:
+        return None
+    return name
 
 def get_console_width():
     """Return width of available window area. Autodetection works for
     os.unlink(tmpfile)
 
     (tmpfile, headers) = urllib.urlretrieve(url, tmpfile, progress_callback)
+    filenamealt = filename_from_headers(headers)
+    if filenamealt:
+        filename = filenamealt
     shutil.move(tmpfile, filename)
 
     print
 http://www.python.org/doc/2.6/library/urllib.html#urllib.urlretrieve
 
 [x] autodetect filename from URL
-[ ] autodetect filename from headers - Content-Disposition
+[x] autodetect filename from headers - Content-Disposition
     http://greenbytes.de/tech/tc2231/
+[ ] make HEAD request to detect temp filename from Content-Disposition
 [ ] catch KeyboardInterrupt
 [ ] optionally preserve incomplete file
 [x] create temp file in current directory