| from fastapi import FastAPI, HTTPException, Query |
| import duckdb |
| from contextlib import asynccontextmanager |
|
|
| |
| con = None |
|
|
| @asynccontextmanager |
| async def lifespan(app: FastAPI): |
| global con |
| print("🟢 DuckDB initialize ho raha hai...") |
| |
| con = duckdb.connect() |
| con.execute("PRAGMA memory_limit='800MB'") |
| con.execute("PRAGMA threads=2") |
| con.execute("SET enable_object_cache=true") |
| con.execute("SET preserve_insertion_order=false") |
| con.execute("INSTALL httpfs; LOAD httpfs;") |
| |
| URL_ICMR = "https://huggingface.co/datasets/kzropx/icmr-parquet/resolve/main/icmr.parquet" |
| con.execute(f"SELECT 1 FROM read_parquet('{URL_ICMR}') LIMIT 1") |
| print("🟢 DuckDB ready! API secure hai.") |
| yield |
| con.close() |
|
|
| app = FastAPI(lifespan=lifespan) |
|
|
| |
| VALID_KEY = "@kzr0x" |
|
|
| @app.get("/") |
| def home(key: str = Query(..., description="API Key required")): |
| if key != VALID_KEY: |
| return {"status": "error", "message": "Invalid or missing API key"} |
| return { |
| "status": "success", |
| "message": "API Live! Use /search?key=@kzr0x&number=xxx", |
| "developer": { |
| "name": "@kzr0x", |
| "team": "TEAM DARK KNIGHT APIs", |
| "Channel": "@API_WALLAH" |
| } |
| } |
|
|
| @app.get("/search") |
| async def search_data( |
| key: str = Query(..., description="API Key required"), |
| number: str = Query(None, description="Search by Phone Number"), |
| aadhar: str = Query(None, description="Search by Aadhar Number"), |
| q: str = Query(None, description="Generic search") |
| ): |
| |
| if key != VALID_KEY: |
| return {"status": "error", "message": "Invalid or missing API key"} |
| |
| if not number and not aadhar and not q: |
| raise HTTPException(status_code=400, detail="Provide number, aadhar, or q") |
| |
| search_term = q or number or aadhar |
| URL_ICMR = "https://huggingface.co/datasets/kzropx/icmr-parquet/resolve/main/icmr.parquet" |
| |
| try: |
| query = f""" |
| SELECT name, fathersName, phoneNumber, aadharNumber, address, district, pincode, state, town |
| FROM read_parquet('{URL_ICMR}') |
| WHERE phoneNumber = ? OR aadharNumber = ? |
| LIMIT 1 |
| """ |
| result = con.execute(query, [search_term, search_term]).fetchall() |
| |
| if not result: |
| return { |
| "status": "error", |
| "message": f"'{search_term}' se koi data nahi mila.", |
| "developer": { |
| "name": "@kzr0x", |
| "team": "TEAM DARK KNIGHT APIs", |
| "Channel": "@API_WALLAH" |
| } |
| } |
| |
| columns = [desc[0] for desc in con.description] |
| return { |
| "status": "success", |
| "data": dict(zip(columns, result[0])), |
| "developer": { |
| "name": "@kzr0x", |
| "team": "TEAM DARK KNIGHT APIs", |
| "Channel": "@API_WALLAH" |
| } |
| } |
| |
| except Exception as e: |
| return { |
| "status": "error", |
| "message": f"Query error: {str(e)}", |
| "developer": { |
| "name": "@kzr0x", |
| "team": "TEAM DARK KNIGHT APIs", |
| "Channel": "@API_WALLAH" |
| } |
| } |