File size: 446 Bytes
31dc2ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from fastapi import FastAPI
import wikipedia

app = FastAPI()

@app.get("/")
async def get_wikipedia_url(topic: str):
    try:
        # Search for the exact page
        page = wikipedia.page(topic, auto_suggest=False, redirect=True)
        url = page.url
        return {"topic": topic, "url": url}
    except Exception:
        # Default fallback if not found
        return {"topic": topic, "url": "https://en.wikipedia.org/wiki/Main_Page"}