Update app.py
Browse files
app.py
CHANGED
|
@@ -26,26 +26,35 @@ if not os.path.exists(DB_PATH) and HF_TOKEN:
|
|
| 26 |
@app.get("/")
|
| 27 |
def home():
|
| 28 |
return {"status": "Online", "database": "Connected", "usage": "/search?num=PHONE"}
|
| 29 |
-
|
| 30 |
@app.get("/search")
|
| 31 |
def search(num: str = None, id: str = None):
|
| 32 |
if not os.path.exists(DB_PATH):
|
| 33 |
-
return {"error": "
|
| 34 |
|
| 35 |
conn = sqlite3.connect(DB_PATH)
|
| 36 |
conn.row_factory = sqlite3.Row
|
| 37 |
cursor = conn.cursor()
|
| 38 |
|
| 39 |
result = None
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
if result:
|
| 50 |
return {"status": "success", "data": dict(result)}
|
| 51 |
-
return {"status": "error", "message": "
|
|
|
|
| 26 |
@app.get("/")
|
| 27 |
def home():
|
| 28 |
return {"status": "Online", "database": "Connected", "usage": "/search?num=PHONE"}
|
| 29 |
+
|
| 30 |
@app.get("/search")
|
| 31 |
def search(num: str = None, id: str = None):
|
| 32 |
if not os.path.exists(DB_PATH):
|
| 33 |
+
return {"error": "Wait bhai, DB download ho raha hai!"}
|
| 34 |
|
| 35 |
conn = sqlite3.connect(DB_PATH)
|
| 36 |
conn.row_factory = sqlite3.Row
|
| 37 |
cursor = conn.cursor()
|
| 38 |
|
| 39 |
result = None
|
| 40 |
+
try:
|
| 41 |
+
if num:
|
| 42 |
+
# Smart Search: Ye string aur number dono format ko check karega
|
| 43 |
+
# Hum '%' isliye laga rahe hain taaki agar number ke aage '91' ho toh bhi mil jaye
|
| 44 |
+
query = "SELECT * FROM users WHERE phone = ? OR phone LIKE ?"
|
| 45 |
+
cursor.execute(query, (num, f"%{num}"))
|
| 46 |
+
result = cursor.fetchone()
|
| 47 |
+
elif id:
|
| 48 |
+
# User ID hamesha exact hoti hai, par hum ise bhi safe banate hain
|
| 49 |
+
query = "SELECT * FROM users WHERE user_id = ? OR CAST(user_id AS TEXT) = ?"
|
| 50 |
+
cursor.execute(query, (id, str(id)))
|
| 51 |
+
result = cursor.fetchone()
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return {"status": "error", "message": str(e)}
|
| 55 |
+
finally:
|
| 56 |
+
conn.close()
|
| 57 |
|
| 58 |
if result:
|
| 59 |
return {"status": "success", "data": dict(result)}
|
| 60 |
+
return {"status": "error", "message": "Record not found in 6.8M rows"}
|