Spaces:
Running
Running
| from fastapi import FastAPI, HTTPException, Query | |
| import duckdb | |
| from contextlib import asynccontextmanager | |
| from datetime import datetime, timedelta | |
| # === VALIDITY CHECK === | |
| DEPLOYMENT_DATE = datetime(2026, 6, 27) # Tune aaj deploy kiya hai | |
| EXPIRY_DAYS = 21 # 3 weeks | |
| def is_api_valid(): | |
| return datetime.now() < DEPLOYMENT_DATE + timedelta(days=EXPIRY_DAYS) | |
| # Global Connection | |
| con = None | |
| 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!") | |
| yield | |
| con.close() | |
| app = FastAPI(lifespan=lifespan) | |
| VALID_KEY = "@kzr0x" | |
| def home(key: str = Query(..., description="API Key required")): | |
| if not is_api_valid(): | |
| return {"status": "error", "message": "API Expired! Valid only for 3 weeks from 27-June-2026."} | |
| if key != VALID_KEY: | |
| return {"status": "error", "message": "Invalid API key"} | |
| return {"status": "success", "message": "API Live! Use /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") | |
| ): | |
| # === EXPIRY CHECK (SAB SE PEHLE) === | |
| if not is_api_valid(): | |
| return {"status": "error", "message": "This API has expired. Valid only for 3 weeks (till 18-July-2026)."} | |
| 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}' not found.", | |
| "developer": {"name": "@kzr0x", "team": "TEAM DARK KNIGHT APIs"} | |
| } | |
| 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"} | |
| } | |
| except Exception as e: | |
| return {"status": "error", "message": str(e)} |