-
Notifications
You must be signed in to change notification settings - Fork 0
/
images.py
112 lines (95 loc) · 3.33 KB
/
images.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import os
import time
import shutil
import random
import requests
from config import *
from geopy.geocoders import Nominatim
from googleapiclient.discovery import build
class Geo():
"""Generate random coordinates and use geopy to get the address."""
def __init__(self):
self.geolocator = Nominatim()
def coordinates(self):
print('Generating coordinates...')
lat = random.uniform(0, 90)
lon = random.uniform(0, 180)
coord = '{lat}, {lon}'.format(lat=lat, lon=lon)
return coord
def get_location(self):
try:
coordinates = self.coordinates()
print('Deciphering coordinates...')
geo = self.geolocator.reverse(coordinates, language='en')
# activate recursion if coordinates land on unknown territory
if geo.address is None:
return self.get_location()
address = geo.raw['address']
country = address['country']
if 'city' in address:
city = address['city']
elif 'city_district' in address:
city = address['city_district']
elif 'county' in address:
city = address['county']
elif 'state_district' in address:
city = address['state_district']
else:
city = None
if city is None:
return country
else:
location = '{city}, {country}'.format(city=city, country=country)
return location
except Exception as e:
print(e)
class Google():
"""Using a Google Custom Search Engine (CSE) that returns four (4) images."""
def __init__(self):
self.service = build("customsearch", "v1", developerKey=devKey)
def search(self, place):
try:
print('Searching images...')
results = self.service.cse().list(
q=place,
num=num,
searchType=searchType,
imgType=imgType,
cx=cx
).execute()
# return list of urls (4)
urls = [url['link'] for url in results['items']]
return urls
except Exception as e:
return e
class ImageManager():
"""Using requests to download the four images obtained from the CSE
and save them to ./images; and delete them with self.delete().
"""
def __init__(self):
self.path = 'images/'
self.default_image_name = 'images/{}.jpg'
def save(self, urls):
image_names = []
print('Saving images...')
for url in urls:
try:
name = self.rename()
image_names.append(name)
pic = requests.get(url, stream=True)
with open(name, 'wb') as out_file:
shutil.copyfileobj(pic.raw, out_file)
except Exception as e:
print(e)
return image_names
def delete(self):
try:
print('Cleaning image directory...')
for image in os.listdir(self.path):
image_path = os.path.join(self.path, image)
os.unlink(image_path)
except Exception as e:
print(e)
def rename(self):
time.sleep(1)
return self.default_image_name.format(int(time.time()))