import os import hashlib import hmac import urllib.parse import requests import gradio as gr import pandas as pd # Retrieve API key and URL signing secret from environment variables api_key = os.getenv("GOOGLE_API_KEY") url_signing_secret = os.getenv("GOOGLE_URL_SIGNING_SECRET") # Google Places API endpoints url = "https://maps.googleapis.com/maps/api/place/textsearch/json" places_details_url = "https://maps.googleapis.com/maps/api/place/details/json" # Function to generate signature for a URL def generate_signature(base_url, secret): secret_bytes = bytes(urllib.parse.unquote(secret), 'utf-8') signature = hmac.new(secret_bytes, msg=base_url.encode('utf-8'), digestmod=hashlib.sha1).hexdigest() return signature # Function to send a request to Google Places API and fetch places data def get_places_data(query, location, radius, next_page_token=None): params = { "query": query, "location": location, "radius": radius, "key": api_key } if next_page_token: params["pagetoken"] = next_page_token base_url = f"{url}?{'&'.join([f'{k}={v}' for k, v in params.items()])}" signature = generate_signature(base_url, url_signing_secret) full_url = f"{base_url}&signature={signature}" response = requests.get(full_url) if response.status_code == 200: return response.json() else: return None # Function to fetch detailed information for a specific place using its place_id def get_place_details(place_id): params = { "place_id": place_id, "key": api_key } base_url = f"{places_details_url}?{'&'.join([f'{k}={v}' for k, v in params.items()])}" signature = generate_signature(base_url, url_signing_secret) full_url = f"{base_url}&signature={signature}" response = requests.get(full_url) if response.status_code == 200: details_data = response.json().get("result", {}) return { "name": details_data.get("name", ""), "address": details_data.get("formatted_address", ""), "phone_number": details_data.get("formatted_phone_number", ""), "website": details_data.get("website", "") } else: return {} # Function to fetch all places data including pagination def get_all_places(query, location, radius): all_results = [] next_page_token = None while True: data = get_places_data(query, location, radius, next_page_token) if data: results = data.get('results', []) for place in results: place_id = place.get("place_id") details = get_place_details(place_id) all_results.append(details) next_page_token = data.get('next_page_token') if not next_page_token: break time.sleep(2) else: break return all_results # Function to display the fetched data using Gradio def display_data(query, location, radius): google_places_data = get_all_places(query, location, radius) return pd.DataFrame(google_places_data) # Gradio interface iface = gr.Interface( fn=display_data, inputs=["text", "text", "number"], outputs="dataframe", title="Wellness Professionals in Hawaii", description="Enter the search query, location coordinates, and radius to fetch wellness professionals from Google Places API." ) # Run the Gradio interface iface.launch()