Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import hashlib
|
| 3 |
+
import hmac
|
| 4 |
+
import urllib.parse
|
| 5 |
+
import requests
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import pandas as pd
|
| 8 |
+
|
| 9 |
+
# Retrieve API key and URL signing secret from environment variables
|
| 10 |
+
api_key = os.getenv("GOOGLE_API_KEY")
|
| 11 |
+
url_signing_secret = os.getenv("GOOGLE_URL_SIGNING_SECRET")
|
| 12 |
+
|
| 13 |
+
# Google Places API endpoints
|
| 14 |
+
url = "https://maps.googleapis.com/maps/api/place/textsearch/json"
|
| 15 |
+
places_details_url = "https://maps.googleapis.com/maps/api/place/details/json"
|
| 16 |
+
|
| 17 |
+
# Function to generate signature for a URL
|
| 18 |
+
def generate_signature(base_url, secret):
|
| 19 |
+
secret_bytes = bytes(urllib.parse.unquote(secret), 'utf-8')
|
| 20 |
+
signature = hmac.new(secret_bytes, msg=base_url.encode('utf-8'), digestmod=hashlib.sha1).hexdigest()
|
| 21 |
+
return signature
|
| 22 |
+
|
| 23 |
+
# Function to send a request to Google Places API and fetch places data
|
| 24 |
+
def get_places_data(query, location, radius, next_page_token=None):
|
| 25 |
+
params = {
|
| 26 |
+
"query": query,
|
| 27 |
+
"location": location,
|
| 28 |
+
"radius": radius,
|
| 29 |
+
"key": api_key
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
if next_page_token:
|
| 33 |
+
params["pagetoken"] = next_page_token
|
| 34 |
+
|
| 35 |
+
base_url = f"{url}?{'&'.join([f'{k}={v}' for k, v in params.items()])}"
|
| 36 |
+
signature = generate_signature(base_url, url_signing_secret)
|
| 37 |
+
full_url = f"{base_url}&signature={signature}"
|
| 38 |
+
|
| 39 |
+
response = requests.get(full_url)
|
| 40 |
+
|
| 41 |
+
if response.status_code == 200:
|
| 42 |
+
return response.json()
|
| 43 |
+
else:
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
# Function to fetch detailed information for a specific place using its place_id
|
| 47 |
+
def get_place_details(place_id):
|
| 48 |
+
params = {
|
| 49 |
+
"place_id": place_id,
|
| 50 |
+
"key": api_key
|
| 51 |
+
}
|
| 52 |
+
base_url = f"{places_details_url}?{'&'.join([f'{k}={v}' for k, v in params.items()])}"
|
| 53 |
+
signature = generate_signature(base_url, url_signing_secret)
|
| 54 |
+
full_url = f"{base_url}&signature={signature}"
|
| 55 |
+
|
| 56 |
+
response = requests.get(full_url)
|
| 57 |
+
|
| 58 |
+
if response.status_code == 200:
|
| 59 |
+
details_data = response.json().get("result", {})
|
| 60 |
+
return {
|
| 61 |
+
"name": details_data.get("name", ""),
|
| 62 |
+
"address": details_data.get("formatted_address", ""),
|
| 63 |
+
"phone_number": details_data.get("formatted_phone_number", ""),
|
| 64 |
+
"website": details_data.get("website", "")
|
| 65 |
+
}
|
| 66 |
+
else:
|
| 67 |
+
return {}
|
| 68 |
+
|
| 69 |
+
# Function to fetch all places data including pagination
|
| 70 |
+
def get_all_places(query, location, radius):
|
| 71 |
+
all_results = []
|
| 72 |
+
next_page_token = None
|
| 73 |
+
while True:
|
| 74 |
+
data = get_places_data(query, location, radius, next_page_token)
|
| 75 |
+
if data:
|
| 76 |
+
results = data.get('results', [])
|
| 77 |
+
for place in results:
|
| 78 |
+
place_id = place.get("place_id")
|
| 79 |
+
details = get_place_details(place_id)
|
| 80 |
+
all_results.append(details)
|
| 81 |
+
|
| 82 |
+
next_page_token = data.get('next_page_token')
|
| 83 |
+
if not next_page_token:
|
| 84 |
+
break
|
| 85 |
+
|
| 86 |
+
time.sleep(2)
|
| 87 |
+
else:
|
| 88 |
+
break
|
| 89 |
+
|
| 90 |
+
return all_results
|
| 91 |
+
|
| 92 |
+
# Function to display the fetched data using Gradio
|
| 93 |
+
def display_data(query, location, radius):
|
| 94 |
+
google_places_data = get_all_places(query, location, radius)
|
| 95 |
+
return pd.DataFrame(google_places_data)
|
| 96 |
+
|
| 97 |
+
# Gradio interface
|
| 98 |
+
iface = gr.Interface(
|
| 99 |
+
fn=display_data,
|
| 100 |
+
inputs=["text", "text", "number"],
|
| 101 |
+
outputs="dataframe",
|
| 102 |
+
title="Wellness Professionals in Hawaii",
|
| 103 |
+
description="Enter the search query, location coordinates, and radius to fetch wellness professionals from Google Places API."
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Run the Gradio interface
|
| 107 |
+
iface.launch()
|