Skip to content

Commit

Permalink
Fix decoding response data
Browse files Browse the repository at this point in the history
  • Loading branch information
sugyan committed Nov 27, 2019
1 parent 0d2bf8f commit ccd8b67
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
5 changes: 2 additions & 3 deletions google_images_download/google_images_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def download_page(self,url):
headers['User-Agent'] = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
req = urllib.request.Request(url, headers=headers)
resp = urllib.request.urlopen(req)
respData = str(resp.read())
respData = resp.read().decode()
return respData
except Exception as e:
print("Could not open URL. Please check your internet connection and/or ssl settings \n"
Expand Down Expand Up @@ -727,8 +727,7 @@ def _get_next_item(self,s):
cur_version = sys.version_info
if cur_version >= version: #python3
try:
object_decode = bytes(object_raw, "utf-8").decode("unicode_escape")
final_object = json.loads(object_decode)
final_object = json.loads(object_raw)
except:
final_object = ""
else: #python2
Expand Down
33 changes: 33 additions & 0 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from google_images_download import google_images_download
import os
import json
import time

from .test_google_images_download import silent_remove_of_file


def test_metadata():
start_time = time.time()
output_folder_path = os.path.join(os.path.realpath('.'), 'logs')

keywords = ["Polar bears", "ほげ"]
for keyword in keywords:
argumnets = {
"keywords": keyword,
"limit": 5,
"no_download": True,
"extract_metadata": True
}
response = google_images_download.googleimagesdownload()
response.download(argumnets)
with open(os.path.join(output_folder_path, '{}.json'.format(keyword)), 'r') as fp:
for item in json.load(fp):
assert keyword.lower() in item['image_description'].lower()

files_modified_after_test_started = [name for name in os.listdir(output_folder_path) if os.path.isfile(os.path.join(output_folder_path, name)) and os.path.getmtime(os.path.join(output_folder_path, name)) > start_time]
print(f"Cleaning up all files downloaded by test {__name__}...")
for file in files_modified_after_test_started:
if silent_remove_of_file(os.path.join(output_folder_path, file)):
print(f"Deleted {os.path.join(output_folder_path, file)}")
else:
print(f"Failed to delete {os.path.join(output_folder_path, file)}")

0 comments on commit ccd8b67

Please sign in to comment.