File size: 1,811 Bytes
d5ff050
2f1d1ae
d5ff050
2f1d1ae
bb3d082
d5ff050
b2f9c9a
d5ff050
 
2f1d1ae
 
d5ff050
 
 
2f1d1ae
b2f9c9a
2f1d1ae
b2f9c9a
 
 
 
2f1d1ae
 
 
 
b2f9c9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f1d1ae
b2f9c9a
 
 
 
 
 
 
d5ff050
b2f9c9a
 
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
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()