-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkita.py
89 lines (73 loc) · 2.85 KB
/
kita.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
import csv
import googlemaps
KITAS = []
def check_business_status(business_status):
return business_status == "OPERATIONAL"
def set_kita_data(kita_data):
kita = {"name": None, "address": None, "phone": None, "website": None}
# Set kita data
kita["name"] = kita_data["result"]["name"]
kita["address"] = kita_data["result"]["formatted_address"]
if "formatted_phone_number" in kita_data["result"]:
kita["phone"] = kita_data["result"]["formatted_phone_number"]
if "website" in kita_data["result"]:
kita["website"] = kita_data["result"]["website"]
KITAS.append(kita)
def get_kitas(address, radius, api_key):
try:
# Instanciates a Google API client
gmaps = googlemaps.Client(key=api_key)
# Get geocode of an address
geocode_result = gmaps.geocode(address)
set_geocode = geocode_result[0]["geometry"]["location"]
# Get places data
places_result = gmaps.places_nearby(
location=(set_geocode), radius=radius, keyword="kita"
)
for item in places_result["results"]:
business_status = item["business_status"]
# Check if tha place still functioning
if check_business_status(business_status):
# Get information about the kita
kita_place_id = item["place_id"]
kita_data = gmaps.place(kita_place_id)
set_kita_data(kita_data)
else:
print("This kita is not functioning anymore")
except Exception as e:
print(f"An error occurred: {e}")
return KITAS
def export_to_csv(filename):
try:
with open(filename, "w", newline="") as csvfile:
fieldnames = [
"name",
"address",
"phone",
"website",
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for kita in KITAS:
writer.writerow(
{
"name": kita["name"],
"address": kita["address"],
"phone": kita["phone"],
"website": kita["website"],
}
)
except Exception as e:
print(f"Error exporting data to CSV: {e}")
if __name__ == "__main__":
address = input("Address:")
if not address:
print("An address was not provided. The default value for the address is 'Dubliner Str. 59, 13349 Berlin'.")
address = "Avenue Charles de Gaulle 10 B, 13469"
radius = input("Radius in meters: ")
if not radius:
print("Radius was not provided. The default value for the radius is 1000m.")
radius = 1000
api_key = input("API KEY(required): ")
kitas = get_kitas(address, radius, api_key)
export_to_csv("kitas_near_address.csv")