Spaces:
Sleeping
Sleeping
File size: 1,786 Bytes
13a1f47 | 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 48 49 50 51 52 53 54 55 56 57 58 59 60 | from fastapi import FastAPI, Query, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import pandas as pd
import requests
from io import StringIO
app = FastAPI(title="IAPI", version="1.0.0")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
DATA_URL = "https://raw.githubusercontent.com/live-by-unix/iapi-csv/refs/heads/main/iapi_data.csv"
def load_database():
try:
response = requests.get(DATA_URL)
response.raise_for_status()
return pd.read_csv(StringIO(response.text))
except Exception:
return pd.DataFrame(columns=["category", "title", "content"])
df = load_database()
@app.get("/")
async def root():
return {
"name": "IAPI",
"records": len(df),
"license": "CC BY-SA 4.0",
"attribution": "Data sourced from Wikipedia editors",
"status": "Online"
}
@app.get("/category/{category_name}")
async def get_category(category_name: str):
subset = df[df['category'].str.lower() == category_name.lower()]
if subset.empty:
raise HTTPException(status_code=404, detail="Category not found")
return subset.to_dict(orient="records")
@app.get("/search")
async def search(q: str = Query(..., min_length=2)):
mask = df['title'].str.contains(q, case=False, na=False) | \
df['content'].str.contains(q, case=False, na=False)
results = df[mask]
return results.to_dict(orient="records")
@app.get("/article/random")
async def get_random():
if df.empty:
raise HTTPException(status_code=503, detail="Database unavailable")
return df.sample(1).to_dict(orient="records")[0]
@app.get("/meta/stats")
async def get_stats():
return df['category'].value_counts().to_dict() |