Léo Bourrel commited on
Commit
2b811d8
·
1 Parent(s): 1f03301

feat: add code to handle fetch photos

Browse files
Files changed (1) hide show
  1. src/fetch_places.py +98 -0
src/fetch_places.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import json
4
+
5
+
6
+ def nearby_search(coordinates: tuple) -> str:
7
+ """Search nearby places
8
+
9
+ Args:
10
+ coordinates (tuple): Coordinates
11
+
12
+ Raises:
13
+ Exception: Failed to fetch places
14
+
15
+ Returns:
16
+ str: Place ID
17
+ """
18
+ url = "https://places.googleapis.com/v1/places:searchNearby"
19
+ payload = json.dumps(
20
+ {
21
+ "includedTypes": ["park"],
22
+ "maxResultCount": 1,
23
+ "locationRestriction": {
24
+ "circle": {
25
+ "center": {"longitude": coordinates[0], "latitude": coordinates[1]},
26
+ "radius": 500,
27
+ }
28
+ },
29
+ }
30
+ )
31
+ headers = {
32
+ "Content-Type": "application/json",
33
+ "X-Goog-Api-Key": "AIzaSyAPmP_sVOZMmJvyXFNPrWJY11BzhKRRBh4",
34
+ "X-Goog-FieldMask": "places.id,places.name,places.location,places.photos",
35
+ }
36
+
37
+ response = requests.request("POST", url, headers=headers, data=payload)
38
+ if response.status_code != 200:
39
+ raise Exception(f"Failed to fetch places: {response.text}")
40
+
41
+ return response.json()
42
+
43
+
44
+ def fetch_photo(place_id: str, photo_id: str) -> str:
45
+ """Fetch photo from place
46
+
47
+ Args:
48
+ place_id (str): Place ID
49
+ photo_id (str): Photo ID
50
+
51
+ Raises:
52
+ Exception: Failed to fetch photo
53
+
54
+ Returns:
55
+ str: Photo binary
56
+ """
57
+ url = f"https://places.googleapis.com/v1/places/{place_id}/photos/{photo_id}/media?key=AIzaSyAPmP_sVOZMmJvyXFNPrWJY11BzhKRRBh4&maxHeightPx=400&maxWidthPx=400"
58
+
59
+ payload = {}
60
+ headers = {
61
+ "Content-Type": "application/json",
62
+ "X-Goog-Api-Key": "AIzaSyAPmP_sVOZMmJvyXFNPrWJY11BzhKRRBh4",
63
+ }
64
+
65
+ response = requests.request("GET", url, headers=headers, data=payload)
66
+
67
+ if response.status_code != 200:
68
+ raise Exception(f"Failed to fetch photo: {response.text}")
69
+
70
+ return response.content
71
+
72
+
73
+ def fetch_place_photos(place_datas: dict, folder: str = "images") -> str:
74
+ """Fetch all photos from place datas
75
+
76
+ Args:
77
+ place_datas (dict): Place datas
78
+ folder (str): Folder to save photos
79
+
80
+ Returns:
81
+ str: Place ID
82
+ """
83
+ place_id = place_datas["id"]
84
+
85
+ folder_path = f"{folder}/{place_id}/"
86
+ os.makedirs(folder_path, exist_ok=True)
87
+
88
+ for photo in place_datas["photos"]:
89
+ photo_id = photo["name"].replace(f"places/{place_id}/photos/", "")
90
+
91
+ photo_binary = fetch_photo(place_id, photo_id)
92
+
93
+ with open(f"{folder_path}/{photo_id}.jpg", "wb") as f:
94
+ f.write(photo_binary)
95
+
96
+ print(f"{len(place_datas["photos"])} photos fetched for place: {place_id}")
97
+
98
+ return place_id