Spaces:
Paused
Paused
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Query
|
| 2 |
+
import duckdb
|
| 3 |
+
|
| 4 |
+
app = FastAPI()
|
| 5 |
+
DB_PATH = "insta_hitech.duckdb"
|
| 6 |
+
|
| 7 |
+
@app.get("/search")
|
| 8 |
+
async def search(q: str = Query(None)):
|
| 9 |
+
if not q:
|
| 10 |
+
return {"error": "Query parameter 'q' missing"}
|
| 11 |
+
|
| 12 |
+
conn = duckdb.connect(DB_PATH, read_only=True)
|
| 13 |
+
try:
|
| 14 |
+
# Smart search: Username, ID, aur Phone teeno check karega
|
| 15 |
+
query = "SELECT * FROM users WHERE u = ? OR id = ? OR t = ?"
|
| 16 |
+
res = conn.execute(query, [q, q, q]).df()
|
| 17 |
+
|
| 18 |
+
if res.empty:
|
| 19 |
+
return {"status": "error", "message": "Record not found"}
|
| 20 |
+
|
| 21 |
+
data = res.rename(columns={'t':'phone', 'u':'username', 'e':'email', 'n':'name', 'id':'user_id'}).to_dict(orient='records')
|
| 22 |
+
return {"status": "success", "data": data}
|
| 23 |
+
finally:
|
| 24 |
+
conn.close()
|
| 25 |
+
|