Files changed (1) hide show
  1. app.py +68 -22
app.py CHANGED
@@ -1,43 +1,89 @@
1
  from fastapi import FastAPI, HTTPException, Query
2
  import duckdb
3
  from contextlib import asynccontextmanager
 
4
 
5
- app = FastAPI()
 
 
 
 
 
 
 
 
6
 
7
  @asynccontextmanager
8
  async def lifespan(app: FastAPI):
9
- print("🟢 Space ready. No view pre-loaded.")
 
 
 
 
 
 
 
 
 
 
 
 
10
  yield
 
 
 
 
 
11
 
12
  @app.get("/")
13
- def home():
14
- return {"message": "API Live! Use /search?phone=9860319038"}
 
 
 
 
15
 
16
  @app.get("/search")
17
- async def search_phone(phone: str = Query(..., description="Phone number")):
18
- con = duckdb.connect()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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('{PARQUET_URL}')
29
- WHERE phoneNumber = ?
30
  LIMIT 1
31
  """
32
- result = con.execute(query, [str(phone)]).fetchall()
33
 
34
  if not result:
35
- return {"status": "error", "message": f"Number {phone} nahi mila."}
 
 
 
 
36
 
37
  columns = [desc[0] for desc in con.description]
38
- return {"status": "success", "data": dict(zip(columns, result[0]))}
39
-
 
 
 
40
  except Exception as e:
41
- raise HTTPException(status_code=500, detail=f"Query error: {str(e)}")
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)}