-
Notifications
You must be signed in to change notification settings - Fork 1
/
googleapi.py
107 lines (91 loc) · 3.13 KB
/
googleapi.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
import json
import os
import random
from typing import List
from uuid import uuid4, uuid5
import dotenv
import requests
from tags import EXCLUDED_TAGS, INCLUDED_TAGS
dotenv.load_dotenv()
API_URL = "https://places.googleapis.com/v1/places:searchNearby"
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
if GOOGLE_API_KEY is None:
print("Google api key is none")
fields = [
"id",
"types",
"formattedAddress",
"location",
"rating",
"userRatingCount",
"displayName",
"primaryTypeDisplayName",
"editorialSummary",
"photos",
]
def get_nearby(location: List[float], radius: float):
request_data = {
"maxResultCount": 20,
"languageCode": "pl",
"locationRestriction": {
"circle": {
"center": {"latitude": location[0], "longitude": location[1]},
"radius": radius,
}
},
"includedTypes": INCLUDED_TAGS,
"excludedTypes": EXCLUDED_TAGS,
}
headers = {
"Content-Type": "application/json",
"X-Goog-Api-Key": GOOGLE_API_KEY,
"X-Goog-FieldMask": ",".join([f"places.{field}" for field in fields]),
}
response = requests.post(API_URL, json=request_data, headers=headers)
response = response.json()
with open("response_google.json", "w") as f:
json.dump(response, f, indent=4)
places = []
if not "places" in response:
return []
for place in response["places"]:
has_rating = "rating" in place and "rating_count" in place
places.append(
{
"id": place["id"],
"tags": place["types"],
"primary_tag": (
place["primaryTypeDisplayName"]["text"]
if "primaryTypeDisplayName" in place
else None
),
"name": place["displayName"]["text"],
"address": place["formattedAddress"],
"location": [
place["location"]["latitude"],
place["location"]["longitude"],
],
"rating": place["rating"] if has_rating else None,
"rating_count": place["userRatingCount"] if has_rating else None,
"summary": place.get("editorialSummary", {}).get("text", None),
"photos_ids": [photo["name"] for photo in place.get("photos", [])],
}
)
return places
def get_photos(place):
photos_urls = []
# filter only 3 random images
place["photos_ids"] = random.sample(
place["photos_ids"], min(3, len(place["photos_ids"]))
)
for photo_id in place["photos_ids"]:
# maxHeightPx=400&maxWidthPx=400&
url = f"https://places.googleapis.com/v1/{photo_id}/media?maxWidthPx=1000&key={GOOGLE_API_KEY}"
# get response and save it to file
response = requests.get(url)
image_hash = uuid5(namespace=uuid4(), name=f"{place['id']}_{photo_id}")
path = f"outputs/{image_hash}.jpg"
with open(path, "wb") as f:
f.write(response.content)
photos_urls.append(f"images/{image_hash}.jpg")
return photos_urls