File size: 1,570 Bytes
b827a2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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."}