Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,8 @@ This project is licensed under the terms of the MIT license.
### Contact

If you have any questions or feedback, please contact us at [email](mailto:shentharkrishnatejaswi@gmail.com).

### Changelog

- 1.1.3:
- Fixed issue with invalid image types. Deleted imghdr and used filetype to check image types.
6 changes: 3 additions & 3 deletions better_bing_image_downloader/bing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from pathlib import Path
import urllib.request
import urllib
import imghdr
import posixpath
import re
import logging
from tqdm import tqdm
import filetype

'''

Expand Down Expand Up @@ -123,8 +123,8 @@ def save_image(self, link, file_path) -> None:
try:
request = urllib.request.Request(link, None, self.headers)
image = urllib.request.urlopen(request, timeout=self.timeout).read()
if not imghdr.what(None, image):
kind = filetype.guess(image)
if kind is None or not kind.mime.startswith('image/'):
logging.error('Invalid image, not saving %s', link)
raise ValueError('Invalid image, not saving %s' % link)
with open(str(file_path), 'wb') as f:
Expand Down
11 changes: 5 additions & 6 deletions better_bing_image_downloader/helperdownload.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from __future__ import print_function

import shutil
import imghdr
import os
import concurrent.futures
import requests
import socket
import filetype

headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
Expand Down Expand Up @@ -39,16 +39,15 @@ def download_image(image_url, dst_dir, file_name, timeout=20, proxy_type=None, p
with open(file_path, 'wb') as f:
f.write(response.content)
response.close()
file_type = imghdr.what(file_path)
# if file_type is not None:
if file_type in ["jpg", "jpeg", "png", "bmp", "webp"]:
new_file_name = "{}.{}".format(file_name, file_type)
kind = filetype.guess(file_path)
if kind and kind.extension in ["jpg", "jpeg", "png", "bmp", "webp"]:
new_file_name = "{}.{}".format(file_name, kind.extension)
new_file_path = os.path.join(dst_dir, new_file_name)
shutil.move(file_path, new_file_path)
print("## OK: {} {}".format(new_file_name, image_url))
else:
os.remove(file_path)
print("## Err: TYPE({}) {}".format(file_type, image_url))
print("## Err: Invalid image type {}".format(image_url))
break
except Exception as e:
if try_times < 3:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ charset-normalizer==3.3.2
chromedriver-autoinstaller==0.6.4
cryptography==42.0.2
docutils==0.20.1
filetype==1.2.0
h11==0.14.0
idna==3.6
importlib-metadata==6.0.0
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="better_bing_image_downloader",
version="1.1.2",
version="1.1.3",
author="Krishnatejaswi S",
author_email="shentharkrishnatejaswi@gmail.com",
description="This package is built on top of bing-image-downloader by gaurav singh",
Expand Down