-
Notifications
You must be signed in to change notification settings - Fork 8
/
addphoto.py
30 lines (27 loc) · 908 Bytes
/
addphoto.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
import os
import glob
from google_images_search import GoogleImagesSearch
# setting dev key and cx
gis = GoogleImagesSearch(os.getenv("GCS_DEVELOPER_KEY"), os.getenv("GCS_CX"))
# function to getcall and download
def get_images(query, n):
_search_params = {
'q': query,
'num': n,
'fileType': 'jpg|gif|png',
}
try:
gis.search(search_params=_search_params, path_to_dir='./images/')
filenames = [f for f in os.listdir('./images/') if os.path.isfile(os.path.join('./images/', f))]
return filenames
except Exception as e:
print(e)
# function to empty the images folder
def empty_images():
folder_path = "./images/"
file_list = glob.glob(os.path.join(folder_path, "*"))
for file_path in file_list:
try:
os.remove(file_path)
except Exception as e:
print(f"Error deleting {file_path}: {e}")