ford442's picture
Create app.py
b827a2e verified
import requests
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
# Initialize the FastAPI app
app = FastAPI()
# Configure CORS middleware to allow requests from any origin
# This is the crucial step that solves the browser security issue
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
@app.get("/get_depth")
def get_depth_data(panoid: str):
"""
This endpoint fetches the depth data from Google's internal API.
It takes a panorama ID as a query parameter.
"""
if not panoid:
return {"error": "A 'panoid' query parameter is required."}
# The target URL for Google's undocumented API
url = f"https://maps.google.com/cbk?output=json&v=4&dm=1&panoid={panoid}"
print(f"Proxying request for panoid: {panoid}")
try:
# Make the server-to-server request to Google
response = requests.get(url)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
# Return the JSON data from Google's response
return response.json()
except requests.exceptions.RequestException as e:
# Handle potential network errors or bad responses from Google
return {"error": f"Failed to fetch data from Google: {e}"}
# A simple root endpoint to confirm the server is running
@app.get("/")
def read_root():
return {"message": "Street View Depth Data Proxy is running."}