Update app.py
Browse files
app.py
CHANGED
|
@@ -5,38 +5,52 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
app.add_middleware(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
DB_PATH = "Telegram_Master_DB.db"
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@app.get("/search")
|
| 13 |
def search(q: str = None):
|
| 14 |
-
# Professional
|
| 15 |
if not q or not os.path.exists(DB_PATH):
|
| 16 |
return {"status": "error", "message": "Not Found"}
|
| 17 |
|
| 18 |
try:
|
| 19 |
-
conn =
|
| 20 |
-
conn.row_factory = sqlite3.Row
|
| 21 |
cursor = conn.cursor()
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
#
|
| 34 |
-
|
| 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 |
|