Spaces:
Running
Running
Update app.py
#2
by Kzzr0x - opened
app.py
CHANGED
|
@@ -1,43 +1,89 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException, Query
|
| 2 |
import duckdb
|
| 3 |
from contextlib import asynccontextmanager
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
@asynccontextmanager
|
| 8 |
async def lifespan(app: FastAPI):
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
yield
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
@app.get("/")
|
| 13 |
-
def home():
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
@app.get("/search")
|
| 17 |
-
async def
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
-
con.execute("PRAGMA memory_limit='800MB'")
|
| 21 |
-
con.execute("PRAGMA threads=2")
|
| 22 |
-
con.execute("INSTALL httpfs; LOAD httpfs;")
|
| 23 |
-
|
| 24 |
-
PARQUET_URL = "https://huggingface.co/datasets/kzropx/icmr-parquet/resolve/main/icmr.parquet"
|
| 25 |
-
|
| 26 |
query = f"""
|
| 27 |
-
SELECT name, fathersName, phoneNumber, address, district, pincode, state, town
|
| 28 |
-
FROM read_parquet('{
|
| 29 |
-
WHERE phoneNumber = ?
|
| 30 |
LIMIT 1
|
| 31 |
"""
|
| 32 |
-
result = con.execute(query, [
|
| 33 |
|
| 34 |
if not result:
|
| 35 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
columns = [desc[0] for desc in con.description]
|
| 38 |
-
return {
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
-
|
| 42 |
-
finally:
|
| 43 |
-
con.close()
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException, Query
|
| 2 |
import duckdb
|
| 3 |
from contextlib import asynccontextmanager
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
|
| 6 |
+
# === VALIDITY CHECK ===
|
| 7 |
+
DEPLOYMENT_DATE = datetime(2026, 6, 27) # Tune aaj deploy kiya hai
|
| 8 |
+
EXPIRY_DAYS = 21 # 3 weeks
|
| 9 |
+
|
| 10 |
+
def is_api_valid():
|
| 11 |
+
return datetime.now() < DEPLOYMENT_DATE + timedelta(days=EXPIRY_DAYS)
|
| 12 |
+
|
| 13 |
+
# Global Connection
|
| 14 |
+
con = None
|
| 15 |
|
| 16 |
@asynccontextmanager
|
| 17 |
async def lifespan(app: FastAPI):
|
| 18 |
+
global con
|
| 19 |
+
print("🟢 DuckDB initialize ho raha hai...")
|
| 20 |
+
|
| 21 |
+
con = duckdb.connect()
|
| 22 |
+
con.execute("PRAGMA memory_limit='800MB'")
|
| 23 |
+
con.execute("PRAGMA threads=2")
|
| 24 |
+
con.execute("SET enable_object_cache=true")
|
| 25 |
+
con.execute("SET preserve_insertion_order=false")
|
| 26 |
+
con.execute("INSTALL httpfs; LOAD httpfs;")
|
| 27 |
+
|
| 28 |
+
URL_ICMR = "https://huggingface.co/datasets/kzropx/icmr-parquet/resolve/main/icmr.parquet"
|
| 29 |
+
con.execute(f"SELECT 1 FROM read_parquet('{URL_ICMR}') LIMIT 1")
|
| 30 |
+
print("🟢 DuckDB ready!")
|
| 31 |
yield
|
| 32 |
+
con.close()
|
| 33 |
+
|
| 34 |
+
app = FastAPI(lifespan=lifespan)
|
| 35 |
+
|
| 36 |
+
VALID_KEY = "@kzr0x"
|
| 37 |
|
| 38 |
@app.get("/")
|
| 39 |
+
def home(key: str = Query(..., description="API Key required")):
|
| 40 |
+
if not is_api_valid():
|
| 41 |
+
return {"status": "error", "message": "API Expired! Valid only for 3 weeks from 27-June-2026."}
|
| 42 |
+
if key != VALID_KEY:
|
| 43 |
+
return {"status": "error", "message": "Invalid API key"}
|
| 44 |
+
return {"status": "success", "message": "API Live! Use /search"}
|
| 45 |
|
| 46 |
@app.get("/search")
|
| 47 |
+
async def search_data(
|
| 48 |
+
key: str = Query(..., description="API Key required"),
|
| 49 |
+
number: str = Query(None, description="Search by Phone Number"),
|
| 50 |
+
aadhar: str = Query(None, description="Search by Aadhar Number"),
|
| 51 |
+
q: str = Query(None, description="Generic search")
|
| 52 |
+
):
|
| 53 |
+
# === EXPIRY CHECK (SAB SE PEHLE) ===
|
| 54 |
+
if not is_api_valid():
|
| 55 |
+
return {"status": "error", "message": "This API has expired. Valid only for 3 weeks (till 18-July-2026)."}
|
| 56 |
+
|
| 57 |
+
if key != VALID_KEY:
|
| 58 |
+
return {"status": "error", "message": "Invalid or missing API key"}
|
| 59 |
+
|
| 60 |
+
if not number and not aadhar and not q:
|
| 61 |
+
raise HTTPException(status_code=400, detail="Provide number, aadhar, or q")
|
| 62 |
+
|
| 63 |
+
search_term = q or number or aadhar
|
| 64 |
+
URL_ICMR = "https://huggingface.co/datasets/kzropx/icmr-parquet/resolve/main/icmr.parquet"
|
| 65 |
+
|
| 66 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
query = f"""
|
| 68 |
+
SELECT name, fathersName, phoneNumber, aadharNumber, address, district, pincode, state, town
|
| 69 |
+
FROM read_parquet('{URL_ICMR}')
|
| 70 |
+
WHERE phoneNumber = ? OR aadharNumber = ?
|
| 71 |
LIMIT 1
|
| 72 |
"""
|
| 73 |
+
result = con.execute(query, [search_term, search_term]).fetchall()
|
| 74 |
|
| 75 |
if not result:
|
| 76 |
+
return {
|
| 77 |
+
"status": "error",
|
| 78 |
+
"message": f"'{search_term}' not found.",
|
| 79 |
+
"developer": {"name": "@kzr0x", "team": "TEAM DARK KNIGHT APIs"}
|
| 80 |
+
}
|
| 81 |
|
| 82 |
columns = [desc[0] for desc in con.description]
|
| 83 |
+
return {
|
| 84 |
+
"status": "success",
|
| 85 |
+
"data": dict(zip(columns, result[0])),
|
| 86 |
+
"developer": {"name": "@kzr0x", "team": "TEAM DARK KNIGHT APIs"}
|
| 87 |
+
}
|
| 88 |
except Exception as e:
|
| 89 |
+
return {"status": "error", "message": str(e)}
|
|
|
|
|
|