Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,45 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
def fetch_google_maps_place_id(query):
|
| 8 |
params = {
|
|
|
|
| 9 |
"engine": "google_maps",
|
| 10 |
-
"
|
| 11 |
-
"
|
| 12 |
-
"
|
|
|
|
| 13 |
}
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 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 |
-
|
| 27 |
-
|
| 28 |
-
"engine": "google_maps_reviews",
|
| 29 |
-
"place_id": place_id,
|
| 30 |
-
"hl": hl,
|
| 31 |
-
"api_key": API_KEY,
|
| 32 |
-
"output": "json"
|
| 33 |
-
}
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
return response.json()
|
| 39 |
-
else:
|
| 40 |
-
st.warning("Failed to fetch reviews from SerpAPI")
|
| 41 |
-
return None
|
| 42 |
|
| 43 |
-
|
| 44 |
-
st.title("Google Maps Reviews Fetcher")
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 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()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from serpapi import GoogleSearch
|
| 3 |
|
| 4 |
+
def fetch_google_maps_results(query=None, location=None, search_type="search"):
|
| 5 |
+
if not query and not location:
|
| 6 |
+
raise ValueError("Either 'query' or 'location' must be provided.")
|
| 7 |
|
|
|
|
| 8 |
params = {
|
| 9 |
+
"api_key": "YOUR_API_KEY",
|
| 10 |
"engine": "google_maps",
|
| 11 |
+
"type": search_type,
|
| 12 |
+
"google_domain": "google.it",
|
| 13 |
+
"hl": "it",
|
| 14 |
+
"gl": "it"
|
| 15 |
}
|
| 16 |
|
| 17 |
+
if query:
|
| 18 |
+
params["q"] = query
|
| 19 |
+
if location:
|
| 20 |
+
params["ll"] = location
|
| 21 |
|
| 22 |
+
search = GoogleSearch(params)
|
| 23 |
+
results = search.get_dict()
|
| 24 |
+
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
st.title("Google Maps API Search via SerpAPI")
|
| 27 |
+
st.write("Enter a search query or a geographic location to fetch results from Google Maps.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Input fields
|
| 30 |
+
search_type = st.selectbox("Select Search Type:", ["search", "place"])
|
| 31 |
+
query = st.text_input("Search Query (q): (Use for regular Google Maps search)")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
location = st.text_input("Geographic Location (ll): (Format: '@latitude,longitude,zoom')")
|
|
|
|
| 34 |
|
| 35 |
+
# Button to trigger the API call
|
| 36 |
+
if st.button("Search"):
|
| 37 |
+
if not query and not location:
|
| 38 |
+
st.error("Please enter either a search query or a geographic location.")
|
| 39 |
+
else:
|
| 40 |
+
try:
|
| 41 |
+
results = fetch_google_maps_results(query=query, location=location, search_type=search_type)
|
| 42 |
+
# Display the results (you might want to format or filter this)
|
| 43 |
+
st.write(results)
|
| 44 |
+
except Exception as e:
|
| 45 |
+
st.error(f"Couldn't fetch the results. Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|