Skip to content

Commit b95b0ef

Browse files
committed
Fix decoding response data
1 parent 0d2bf8f commit b95b0ef

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

google_images_download/google_images_download.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def download_page(self,url):
137137
headers['User-Agent'] = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
138138
req = urllib.request.Request(url, headers=headers)
139139
resp = urllib.request.urlopen(req)
140-
respData = str(resp.read())
140+
respData = resp.read().decode()
141141
return respData
142142
except Exception as e:
143143
print("Could not open URL. Please check your internet connection and/or ssl settings \n"
@@ -727,8 +727,7 @@ def _get_next_item(self,s):
727727
cur_version = sys.version_info
728728
if cur_version >= version: #python3
729729
try:
730-
object_decode = bytes(object_raw, "utf-8").decode("unicode_escape")
731-
final_object = json.loads(object_decode)
730+
final_object = json.loads(object_raw)
732731
except:
733732
final_object = ""
734733
else: #python2

tests/test_metadata.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from google_images_download import google_images_download
2+
import os
3+
import json
4+
import time
5+
6+
from .test_google_images_download import silent_remove_of_file
7+
8+
9+
def test_metadata():
10+
start_time = time.time()
11+
output_folder_path = os.path.join(os.path.realpath('.'), 'logs')
12+
13+
keywords = ["Polar bears", "ほげ"]
14+
for keyword in keywords:
15+
argumnets = {
16+
"keywords": keyword,
17+
"limit": 5,
18+
"no_download": True,
19+
"extract_metadata": True
20+
}
21+
response = google_images_download.googleimagesdownload()
22+
response.download(argumnets)
23+
with open(os.path.join(output_folder_path, '{}.json'.format(keyword)), 'r') as fp:
24+
for item in json.load(fp):
25+
assert keyword.lower() in item['image_description'].lower()
26+
27+
argumnets = {
28+
"keywords": "ほげ",
29+
"limit": 5,
30+
"no_download": True,
31+
"extract_metadata": True
32+
}
33+
response = google_images_download.googleimagesdownload()
34+
response.download(argumnets)
35+
36+
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]
37+
print(f"Cleaning up all files downloaded by test {__name__}...")
38+
for file in files_modified_after_test_started:
39+
if silent_remove_of_file(os.path.join(output_folder_path, file)):
40+
print(f"Deleted {os.path.join(output_folder_path, file)}")
41+
else:
42+
print(f"Failed to delete {os.path.join(output_folder_path, file)}")

0 commit comments

Comments
 (0)