Watchhrr commited on
Commit
436720a
·
verified ·
1 Parent(s): 9ba745e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +22 -24
main.py CHANGED
@@ -6,20 +6,20 @@ from fastapi import FastAPI, Query
6
  from contextlib import asynccontextmanager
7
 
8
  DB_PATH = "insta_hitech.db"
9
- # Aapka dataset direct download link (Token ke saath)
10
  DB_URL = "https://huggingface.co/datasets/Watchhrr/Insta17m/resolve/main/insta_hitech.db"
11
  TOKEN = os.getenv("MY_HF_TOKEN")
12
 
13
  def download_db():
14
  if not os.path.exists(DB_PATH):
15
- print("📥 Database missing. Downloading from Dataset...")
16
- headers = {"Authorization": f"Bearer {TOKEN}"}
17
  with requests.get(DB_URL, headers=headers, stream=True) as r:
18
  r.raise_for_status()
19
  with open(DB_PATH, 'wb') as f:
20
  for chunk in r.iter_content(chunk_size=1024*1024):
21
  f.write(chunk)
22
- print("✅ Download Complete!")
23
 
24
  @asynccontextmanager
25
  async def lifespan(app: FastAPI):
@@ -28,38 +28,36 @@ async def lifespan(app: FastAPI):
28
 
29
  app = FastAPI(lifespan=lifespan)
30
 
31
- @app.get("/")
32
- def home():
33
- return {"message": "Insta Search API is Live!", "endpoint": "/search?q=YOUR_QUERY"}
 
 
 
 
 
 
 
34
 
 
35
  @app.get("/search")
36
  async def search(q: str = Query(None)):
37
- if not q:
38
- return {"error": "Bhai, search query 'q' miss hai. Example: /search?q=username"}
39
-
40
- if not os.path.exists(DB_PATH):
41
- return {"error": "Database file abhi download ho rahi hai, thoda sabar rakhein."}
42
 
43
  conn = sqlite3.connect(DB_PATH)
44
- conn.row_factory = sqlite3.Row # JSON format output ke liye
45
  cursor = conn.cursor()
46
 
47
  try:
48
- # Optimized query jo indexes use karegi
49
- # 'u' (username), 'id' (user_id), 't' (phone) teeno check honge
50
- sql_query = "SELECT * FROM users WHERE u = ? OR id = ? OR t = ? LIMIT 10"
51
- cursor.execute(sql_query, (q, q, q))
52
  rows = cursor.fetchall()
53
 
54
  if not rows:
55
- return {"status": "error", "message": "No Record Found in Database"}
56
 
57
- # Result ko list of dict mein badalna
58
- results = [dict(row) for row in rows]
59
- return {"status": "success", "count": len(results), "data": results}
60
-
61
- except Exception as e:
62
- return {"status": "error", "message": str(e)}
63
  finally:
64
  conn.close()
65
 
 
6
  from contextlib import asynccontextmanager
7
 
8
  DB_PATH = "insta_hitech.db"
9
+ # Dataset direct download link
10
  DB_URL = "https://huggingface.co/datasets/Watchhrr/Insta17m/resolve/main/insta_hitech.db"
11
  TOKEN = os.getenv("MY_HF_TOKEN")
12
 
13
  def download_db():
14
  if not os.path.exists(DB_PATH):
15
+ print("📥 Downloading DB from Dataset...")
16
+ headers = {"Authorization": f"Bearer {TOKEN}"} if TOKEN else {}
17
  with requests.get(DB_URL, headers=headers, stream=True) as r:
18
  r.raise_for_status()
19
  with open(DB_PATH, 'wb') as f:
20
  for chunk in r.iter_content(chunk_size=1024*1024):
21
  f.write(chunk)
22
+ print("✅ DB Downloaded!")
23
 
24
  @asynccontextmanager
25
  async def lifespan(app: FastAPI):
 
28
 
29
  app = FastAPI(lifespan=lifespan)
30
 
31
+ # 1. Total Count Check karne ke liye: /count
32
+ @app.get("/count")
33
+ def get_count():
34
+ if not os.path.exists(DB_PATH): return {"error": "DB not ready"}
35
+ conn = sqlite3.connect(DB_PATH)
36
+ cursor = conn.cursor()
37
+ cursor.execute("SELECT COUNT(*) FROM users")
38
+ total = cursor.fetchone()[0]
39
+ conn.close()
40
+ return {"total_records": total}
41
 
42
+ # 2. Main Search: /search?q=XYZ
43
  @app.get("/search")
44
  async def search(q: str = Query(None)):
45
+ if not q: return {"error": "Bhai, 'q' parameter missing hai!"}
46
+ if not os.path.exists(DB_PATH): return {"error": "DB downloading..."}
 
 
 
47
 
48
  conn = sqlite3.connect(DB_PATH)
49
+ conn.row_factory = sqlite3.Row
50
  cursor = conn.cursor()
51
 
52
  try:
53
+ # Search query jo Username(u), ID(id), aur Phone(t) teeno check karega
54
+ cursor.execute("SELECT * FROM users WHERE u=? OR id=? OR t=? LIMIT 10", (q, q, q))
 
 
55
  rows = cursor.fetchall()
56
 
57
  if not rows:
58
+ return {"status": "error", "message": "No Record Found"}
59
 
60
+ return {"status": "success", "results": [dict(r) for r in rows]}
 
 
 
 
 
61
  finally:
62
  conn.close()
63