|
5 | 5 | from __future__ import print_function |
6 | 6 |
|
7 | 7 | import shutil |
8 | | -import imghdr |
9 | 8 | import os |
10 | 9 | import concurrent.futures |
11 | 10 | import requests |
12 | 11 | import socket |
| 12 | +import filetype |
13 | 13 |
|
14 | 14 | headers = { |
15 | 15 | "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", |
@@ -39,16 +39,15 @@ def download_image(image_url, dst_dir, file_name, timeout=20, proxy_type=None, p |
39 | 39 | with open(file_path, 'wb') as f: |
40 | 40 | f.write(response.content) |
41 | 41 | response.close() |
42 | | - file_type = imghdr.what(file_path) |
43 | | - # if file_type is not None: |
44 | | - if file_type in ["jpg", "jpeg", "png", "bmp", "webp"]: |
45 | | - new_file_name = "{}.{}".format(file_name, file_type) |
| 42 | + kind = filetype.guess(file_path) |
| 43 | + if kind and kind.extension in ["jpg", "jpeg", "png", "bmp", "webp"]: |
| 44 | + new_file_name = "{}.{}".format(file_name, kind.extension) |
46 | 45 | new_file_path = os.path.join(dst_dir, new_file_name) |
47 | 46 | shutil.move(file_path, new_file_path) |
48 | 47 | print("## OK: {} {}".format(new_file_name, image_url)) |
49 | 48 | else: |
50 | 49 | os.remove(file_path) |
51 | | - print("## Err: TYPE({}) {}".format(file_type, image_url)) |
| 50 | + print("## Err: Invalid image type {}".format(image_url)) |
52 | 51 | break |
53 | 52 | except Exception as e: |
54 | 53 | if try_times < 3: |
|
0 commit comments