antfraia commited on
Commit
ff80b22
·
1 Parent(s): 30b7d57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -24
app.py CHANGED
@@ -1,23 +1,27 @@
1
  import streamlit as st
2
- import requests
3
- from requests.exceptions import HTTPError
4
 
5
  def fetch_google_maps_results(query=None, search_type="search"):
6
  if not query:
 
 
 
 
 
 
 
 
7
  "q": query
8
  }
9
 
10
- response = requests.get("https://serpapi.com/search", params=params)
11
- response.raise_for_status() # Raise HTTPError for bad response
12
-
13
- jsonResponse = response.json() # Parse JSON content
14
-
15
- return jsonResponse
16
 
17
 
18
 
19
 
20
 
 
 
 
21
 
22
  st.title("Google Maps API Search via SerpAPI")
23
  st.write("Enter a search query to fetch results from Google Maps.")
@@ -32,22 +36,10 @@ query = st.text_input("Search Query (q): (Use for regular Google Maps search)")
32
  # Button to trigger the API call
33
  if st.button("Search"):
34
  if not query:
 
35
  else:
36
  try:
37
  results = fetch_google_maps_results(query=query)
38
- # Display user reviews
39
- user_reviews = results.get("user_reviews", [])
40
- if user_reviews:
41
- st.write("User Reviews:")
42
- for review in user_reviews:
43
- st.write("Username:", review["username"])
44
- st.write("Rating:", review["rating"])
45
- st.write("Description:", review["description"])
46
- st.write("Date:", review["date"])
47
- st.write("---")
48
- else:
49
- st.write("No user reviews found.")
50
- except HTTPError as http_err:
51
- st.error(f'HTTP error occurred: {http_err}')
52
- except Exception as err:
53
- st.error(f'Other error occurred: {err}')
 
1
  import streamlit as st
2
+ from serpapi import GoogleSearch
 
3
 
4
  def fetch_google_maps_results(query=None, search_type="search"):
5
  if not query:
6
+ raise ValueError("The 'query' must be provided.")
7
+
8
+ params = {
9
+ "api_key": "b18f4d37042ab8222028830ee1d3db47481ac48f545382eacd7f0469414f1b1a",
10
+ "type": search_type,
11
+ "google_domain": "google.it",
12
+ "hl": "it",
13
+ "gl": "it",
14
  "q": query
15
  }
16
 
 
 
 
 
 
 
17
 
18
 
19
 
20
 
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 to fetch results from Google Maps.")
 
36
  # Button to trigger the API call
37
  if st.button("Search"):
38
  if not query:
39
+ st.error("Please enter a search query.")
40
  else:
41
  try:
42
  results = fetch_google_maps_results(query=query)
43
+ st.write(results) # Display the results
44
+ except Exception as e:
45
+ st.error(f"Couldn't fetch the results. Error: {e}")