Files changed (1) hide show
  1. app.py +61 -27
app.py CHANGED
@@ -1,55 +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.")
 
 
 
 
 
 
 
 
 
 
 
 
10
  yield
 
 
 
 
 
11
 
12
  @app.get("/")
13
- def home():
14
- return {"message": "API Live! Use /search?phone=9860319038 OR /search?aadhar=912712797710 OR /search?q=9860319038"}
 
 
 
 
15
 
16
  @app.get("/search")
17
  async def search_data(
18
- phone: str = Query(None, description="Search by Phone Number"),
 
19
  aadhar: str = Query(None, description="Search by Aadhar Number"),
20
- q: str = Query(None, description="Search by either Phone or Aadhar (generic)")
21
  ):
22
- # Agar koi bhi parameter nahi diya toh error
23
- if not phone and not aadhar and not q:
24
- raise HTTPException(status_code=400, detail="Provide phone, aadhar, or q parameter")
 
 
 
 
 
 
 
 
 
25
 
26
- # Priority: q > phone > aadhar
27
- search_term = q or phone or aadhar
28
-
29
- con = duckdb.connect()
30
  try:
31
- con.execute("PRAGMA memory_limit='800MB'")
32
- con.execute("PRAGMA threads=2")
33
- con.execute("INSTALL httpfs; LOAD httpfs;")
34
-
35
- PARQUET_URL = "https://huggingface.co/datasets/kzropx/icmr-parquet/resolve/main/icmr.parquet"
36
-
37
- # Dono columns (Phone aur Aadhar) mein search karo
38
  query = f"""
39
  SELECT name, fathersName, phoneNumber, aadharNumber, address, district, pincode, state, town
40
- FROM read_parquet('{PARQUET_URL}')
41
  WHERE phoneNumber = ? OR aadharNumber = ?
42
  LIMIT 1
43
  """
44
  result = con.execute(query, [search_term, search_term]).fetchall()
45
 
46
  if not result:
47
- return {"status": "error", "message": f"'{search_term}' se koi data nahi mila."}
 
 
 
 
48
 
49
  columns = [desc[0] for desc in con.description]
50
- return {"status": "success", "data": dict(zip(columns, result[0]))}
51
-
 
 
 
52
  except Exception as e:
53
- raise HTTPException(status_code=500, detail=f"Query error: {str(e)}")
54
- finally:
55
- 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)}