Mercurial > public > sg101
comparison core/download.py @ 981:ef1558941bc9
Additional tweaks to ssl_images.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 24 Oct 2015 21:29:07 -0500 |
parents | 3ebde23a59d0 |
children |
comparison
equal
deleted
inserted
replaced
980:3ebde23a59d0 | 981:ef1558941bc9 |
---|---|
3 import logging | 3 import logging |
4 import mimetypes | 4 import mimetypes |
5 import os | 5 import os |
6 import shutil | 6 import shutil |
7 import tempfile | 7 import tempfile |
8 from urlparse import urlparse | |
8 | 9 |
9 import requests | 10 import requests |
10 | 11 |
11 | 12 |
12 logger = logging.getLogger(__name__) | 13 logger = logging.getLogger(__name__) |
36 # Save file data | 37 # Save file data |
37 | 38 |
38 if not path: | 39 if not path: |
39 content_type = r.headers.get('content-type') | 40 content_type = r.headers.get('content-type') |
40 suffix = mimetypes.guess_extension(content_type) if content_type else '' | 41 suffix = mimetypes.guess_extension(content_type) if content_type else '' |
42 | |
43 # mimetypes currently returns '.jpe' for jpeg; so fix that up here... | |
41 if suffix == '.jpe': | 44 if suffix == '.jpe': |
42 suffix = '.jpg' | 45 suffix = '.jpg' |
43 elif suffix is None: | 46 elif not suffix: |
44 suffix = '' | 47 # No content-type so guess based on extension if we can |
48 p = urlparse(url) | |
49 suffix = os.path.splitext(p.path)[1] | |
50 | |
45 fd, path = tempfile.mkstemp(suffix=suffix) | 51 fd, path = tempfile.mkstemp(suffix=suffix) |
46 os.close(fd) | 52 os.close(fd) |
47 | 53 |
48 try: | 54 try: |
49 with open(path, 'wb') as fp: | 55 with open(path, 'wb') as fp: |