File size: 2,094 Bytes
abad761
877711d
 
1d4e97b
abad761
877711d
285534e
877711d
436720a
877711d
 
285534e
877711d
 
436720a
 
877711d
 
 
 
 
436720a
877711d
 
 
 
 
 
 
 
436720a
 
 
 
 
 
 
 
 
 
abad761
436720a
285534e
 
436720a
 
877711d
 
436720a
877711d
285534e
 
436720a
 
877711d
d91e9fa
877711d
436720a
d91e9fa
436720a
285534e
 
1d4e97b
 
d91e9fa
1d4e97b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import sqlite3
import requests
import uvicorn
from fastapi import FastAPI, Query
from contextlib import asynccontextmanager

DB_PATH = "insta_hitech.db"
# Dataset direct download link
DB_URL = "https://huggingface.co/datasets/Watchhrr/Insta17m/resolve/main/insta_hitech.db"
TOKEN = os.getenv("MY_HF_TOKEN")

def download_db():
    if not os.path.exists(DB_PATH):
        print("📥 Downloading DB from Dataset...")
        headers = {"Authorization": f"Bearer {TOKEN}"} if TOKEN else {}
        with requests.get(DB_URL, headers=headers, stream=True) as r:
            r.raise_for_status()
            with open(DB_PATH, 'wb') as f:
                for chunk in r.iter_content(chunk_size=1024*1024):
                    f.write(chunk)
        print("✅ DB Downloaded!")

@asynccontextmanager
async def lifespan(app: FastAPI):
    download_db()
    yield

app = FastAPI(lifespan=lifespan)

# 1. Total Count Check karne ke liye: /count
@app.get("/count")
def get_count():
    if not os.path.exists(DB_PATH): return {"error": "DB not ready"}
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    cursor.execute("SELECT COUNT(*) FROM users")
    total = cursor.fetchone()[0]
    conn.close()
    return {"total_records": total}

# 2. Main Search: /search?q=XYZ
@app.get("/search")
async def search(q: str = Query(None)):
    if not q: return {"error": "Bhai, 'q' parameter missing hai!"}
    if not os.path.exists(DB_PATH): return {"error": "DB downloading..."}

    conn = sqlite3.connect(DB_PATH)
    conn.row_factory = sqlite3.Row
    cursor = conn.cursor()
    
    try:
        # Search query jo Username(u), ID(id), aur Phone(t) teeno check karega
        cursor.execute("SELECT * FROM users WHERE u=? OR id=? OR t=? LIMIT 10", (q, q, q))
        rows = cursor.fetchall()
        
        if not rows:
            return {"status": "error", "message": "No Record Found"}
        
        return {"status": "success", "results": [dict(r) for r in rows]}
    finally:
        conn.close()

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)