antfraia commited on
Commit
b2f9c9a
·
1 Parent(s): e26a8b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -10
app.py CHANGED
@@ -2,9 +2,9 @@ import streamlit as st
2
  import requests
3
 
4
  SERPAPI_ENDPOINT = "https://serpapi.com/search"
5
- API_KEY = "b18f4d37042ab8222028830ee1d3db47481ac48f545382eacd7f0469414f1b1a"
6
 
7
- def fetch_google_maps_data(query):
8
  params = {
9
  "engine": "google_maps",
10
  "q": query,
@@ -13,18 +13,50 @@ def fetch_google_maps_data(query):
13
  }
14
 
15
  response = requests.get(SERPAPI_ENDPOINT, params=params)
16
-
17
  if response.status_code == 200:
18
- return response.json()
 
 
 
19
  else:
20
  st.warning("Failed to fetch data from SerpAPI")
21
  return None
22
 
23
- st.title("Google Maps Data Fetcher")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- query = st.text_input("Enter your query:")
 
 
 
 
 
 
26
 
27
- if st.button("Search"):
28
- data = fetch_google_maps_data(query)
29
- if data:
30
- st.write(data)
 
2
  import requests
3
 
4
  SERPAPI_ENDPOINT = "https://serpapi.com/search"
5
+ API_KEY = "YOUR_SERPAPI_KEY"
6
 
7
+ def fetch_google_maps_place_id(query):
8
  params = {
9
  "engine": "google_maps",
10
  "q": query,
 
13
  }
14
 
15
  response = requests.get(SERPAPI_ENDPOINT, params=params)
16
+
17
  if response.status_code == 200:
18
+ data = response.json()
19
+ # Extract the place_id from the JSON. You might need to adjust based on the actual structure of the returned JSON.
20
+ place_id = data.get("place_id", None)
21
+ return place_id
22
  else:
23
  st.warning("Failed to fetch data from SerpAPI")
24
  return None
25
 
26
+ def fetch_google_maps_reviews(place_id, hl="en"):
27
+ params = {
28
+ "engine": "google_maps_reviews",
29
+ "place_id": place_id,
30
+ "hl": hl,
31
+ "api_key": API_KEY,
32
+ "output": "json"
33
+ }
34
+
35
+ response = requests.get(SERPAPI_ENDPOINT, params=params)
36
+
37
+ if response.status_code == 200:
38
+ return response.json()
39
+ else:
40
+ st.warning("Failed to fetch reviews from SerpAPI")
41
+ return None
42
+
43
+ def main():
44
+ st.title("Google Maps Reviews Fetcher")
45
+
46
+ # User input
47
+ query = st.text_input("Enter a Google Maps place (e.g., 'Starbucks near Times Square'):")
48
+
49
+ if st.button("Start"):
50
+ # Fetch the place_id first
51
+ place_id = fetch_google_maps_place_id(query)
52
 
53
+ # If place_id was successfully fetched, then get the reviews
54
+ if place_id:
55
+ reviews = fetch_google_maps_reviews(place_id)
56
+ if reviews:
57
+ st.json(reviews)
58
+ else:
59
+ st.warning("Couldn't fetch the place_id. Please try again or refine your query.")
60
 
61
+ if __name__ == "__main__":
62
+ main()