antfraia's picture
Update app.py
bb3d082
raw
history blame
1.81 kB
import streamlit as st
import requests
SERPAPI_ENDPOINT = "https://serpapi.com/search"
API_KEY = "b18f4d37042ab8222028830ee1d3db47481ac48f545382eacd7f0469414f1b1a"
def fetch_google_maps_place_id(query):
params = {
"engine": "google_maps",
"q": query,
"api_key": API_KEY,
"output": "json"
}
response = requests.get(SERPAPI_ENDPOINT, params=params)
if response.status_code == 200:
data = response.json()
# Extract the place_id from the JSON. You might need to adjust based on the actual structure of the returned JSON.
place_id = data.get("place_id", None)
return place_id
else:
st.warning("Failed to fetch data from SerpAPI")
return None
def fetch_google_maps_reviews(place_id, hl="en"):
params = {
"engine": "google_maps_reviews",
"place_id": place_id,
"hl": hl,
"api_key": API_KEY,
"output": "json"
}
response = requests.get(SERPAPI_ENDPOINT, params=params)
if response.status_code == 200:
return response.json()
else:
st.warning("Failed to fetch reviews from SerpAPI")
return None
def main():
st.title("Google Maps Reviews Fetcher")
# User input
query = st.text_input("Enter a Google Maps place (e.g., 'Starbucks near Times Square'):")
if st.button("Start"):
# Fetch the place_id first
place_id = fetch_google_maps_place_id(query)
# If place_id was successfully fetched, then get the reviews
if place_id:
reviews = fetch_google_maps_reviews(place_id)
if reviews:
st.json(reviews)
else:
st.warning("Couldn't fetch the place_id. Please try again or refine your query.")
if __name__ == "__main__":
main()