forked from hardikvasa/google-images-download
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_google_images_download.py
53 lines (44 loc) · 2.16 KB
/
test_google_images_download.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from google_images_download import google_images_download
import os, errno
import time
def silent_remove_of_file(file):
try:
os.remove(file)
except OSError as e:
if e.errno != errno.ENOENT:
raise e
return False
return True
def test_download_images_to_default_location():
start_time = time.time()
argumnets = {
"keywords": "Polar bears",
"limit": 5,
"print_urls": False
}
try:
temp = argumnets['output_folder']
except KeyError:
pass
else:
assert False, "This test checks download to default location yet an output folder was provided"
output_folder_path = os.path.join(os.path.realpath('.'), 'downloads', '{}'.format(argumnets['keywords']))
if os.path.exists(output_folder_path):
start_amount_of_files_in_output_folder = len([name for name in os.listdir(output_folder_path) if os.path.isfile(os.path.join(output_folder_path, name)) and os.path.getctime(os.path.join(output_folder_path, name)) < start_time])
else:
start_amount_of_files_in_output_folder = 0
response = google_images_download.googleimagesdownload()
response.download(argumnets)
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]
end_amount_of_files_in_output_folder = len(files_modified_after_test_started)
print(f"Files downloaded by test {__name__}:")
for file in files_modified_after_test_started:
print(os.path.join(output_folder_path, file))
# assert end_amount_of_files_in_output_folder - start_amount_of_files_in_output_folder == argumnets['limit']
assert end_amount_of_files_in_output_folder == argumnets['limit']
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)}")