Watchhrr commited on
Commit
bd82b98
·
verified ·
1 Parent(s): 621646f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -21
app.py CHANGED
@@ -5,38 +5,52 @@ from fastapi.middleware.cors import CORSMiddleware
5
 
6
  app = FastAPI()
7
 
8
- app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
 
 
 
 
 
9
 
10
  DB_PATH = "Telegram_Master_DB.db"
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  @app.get("/search")
13
  def search(q: str = None):
14
- # Professional message only
15
  if not q or not os.path.exists(DB_PATH):
16
  return {"status": "error", "message": "Not Found"}
17
 
18
  try:
19
- conn = sqlite3.connect(DB_PATH)
20
- conn.row_factory = sqlite3.Row
21
  cursor = conn.cursor()
22
 
23
- # 1. Pehle table ka naam pata karo (Automatic)
24
- cursor.execute("SELECT name FROM sqlite_master WHERE type='table' LIMIT 1;")
25
- table = cursor.fetchone()
26
- if not table: return {"status": "error", "message": "Not Found"}
27
- t_name = table[0]
28
-
29
- # 2. Saare columns ke naam pata karo (Automatic)
30
- cursor.execute(f"PRAGMA table_info({t_name})")
31
- cols = [c[1] for c in cursor.fetchall()]
32
-
33
- # 3. Har column mein dhoondho (Partial & Exact Search)
34
- # Ye 'account_id', 'phone', 'name' sabme ek saath dhoondhega
35
- clauses = [f"CAST({col} AS TEXT) LIKE ?" for col in cols]
36
- query = f"SELECT * FROM {t_name} WHERE {' OR '.join(clauses)} LIMIT 1"
37
- params = [f"%{q}%"] * len(cols)
38
-
39
- cursor.execute(query, params)
40
  result = cursor.fetchone()
41
  conn.close()
42
 
 
5
 
6
  app = FastAPI()
7
 
8
+ app.add_middleware(
9
+ CORSMiddleware,
10
+ allow_origins=["*"],
11
+ allow_methods=["*"],
12
+ allow_headers=["*"],
13
+ )
14
 
15
  DB_PATH = "Telegram_Master_DB.db"
16
 
17
+ def get_db_connection():
18
+ conn = sqlite3.connect(DB_PATH)
19
+ conn.row_factory = sqlite3.Row
20
+ return conn
21
+
22
+ @app.on_event("startup")
23
+ async def startup():
24
+ if os.path.exists(DB_PATH):
25
+ conn = get_db_connection()
26
+ # Speed badhane ke liye Indexing (Sirf ek baar chalega)
27
+ conn.execute("CREATE INDEX IF NOT EXISTS idx_phone ON users(phone)")
28
+ conn.execute("CREATE INDEX IF NOT EXISTS idx_id ON users(user_id)")
29
+ conn.commit()
30
+ conn.close()
31
+
32
  @app.get("/search")
33
  def search(q: str = None):
34
+ # Professional error message
35
  if not q or not os.path.exists(DB_PATH):
36
  return {"status": "error", "message": "Not Found"}
37
 
38
  try:
39
+ conn = get_db_connection()
 
40
  cursor = conn.cursor()
41
 
42
+ # Super-Smart Query: Har column check karega jo aapne bataye the
43
+ query = """
44
+ SELECT * FROM users
45
+ WHERE user_id = ?
46
+ OR phone = ?
47
+ OR account_id = ?
48
+ OR adapterUserId = ?
49
+ OR phone LIKE ?
50
+ LIMIT 1
51
+ """
52
+ # Hum 'q' ko har possible column mein match karenge
53
+ cursor.execute(query, (q, q, q, q, f"%{q}"))
 
 
 
 
 
54
  result = cursor.fetchone()
55
  conn.close()
56