Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,68 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
|
|
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
|
| 6 |
app = FastAPI(title="Telegram API")
|
| 7 |
|
| 8 |
-
|
| 9 |
|
| 10 |
def get_db():
|
| 11 |
-
global
|
| 12 |
|
| 13 |
-
if
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
repo_id="fdsgfuisfydgiuef/Telegram",
|
| 18 |
-
repo_type="dataset",
|
| 19 |
-
filename="Midnight_Master_v2.db",
|
| 20 |
-
token=os.getenv("HF_TOKEN")
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
return DB_FILE
|
| 24 |
|
| 25 |
|
| 26 |
@app.get("/")
|
| 27 |
def home():
|
| 28 |
return {
|
| 29 |
"status": True,
|
| 30 |
-
"message": "API Running"
|
| 31 |
}
|
| 32 |
|
| 33 |
|
| 34 |
-
@app.get("/
|
| 35 |
-
def
|
| 36 |
-
db = get_db()
|
| 37 |
-
|
| 38 |
-
conn = sqlite3.connect(db)
|
| 39 |
-
cur = conn.cursor()
|
| 40 |
-
|
| 41 |
-
cur.execute("SELECT name FROM sqlite_master WHERE type='table'")
|
| 42 |
-
tables = [x[0] for x in cur.fetchall()]
|
| 43 |
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
-
conn.close()
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import duckdb
|
| 3 |
+
import uvicorn
|
| 4 |
from fastapi import FastAPI
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
|
| 7 |
app = FastAPI(title="Telegram API")
|
| 8 |
|
| 9 |
+
DB_PATH = None
|
| 10 |
|
| 11 |
def get_db():
|
| 12 |
+
global DB_PATH
|
| 13 |
|
| 14 |
+
if DB_PATH is None:
|
| 15 |
+
DB_PATH = hf_hub_download(
|
| 16 |
+
repo_id="fdsgfuisfydgiuef/Telegram",
|
| 17 |
+
repo_type="dataset",
|
| 18 |
+
filename="Midnight_Master_v2.db",
|
| 19 |
+
token=os.getenv("HF_TOKEN")
|
| 20 |
+
)
|
| 21 |
|
| 22 |
+
return DB_PATH
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
@app.get("/")
|
| 26 |
def home():
|
| 27 |
return {
|
| 28 |
"status": True,
|
| 29 |
+
"message": "Telegram API Running"
|
| 30 |
}
|
| 31 |
|
| 32 |
|
| 33 |
+
@app.get("/search")
|
| 34 |
+
def search(q: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
db = get_db()
|
| 37 |
|
| 38 |
+
con = duckdb.connect()
|
| 39 |
|
| 40 |
+
con.execute("INSTALL sqlite;")
|
| 41 |
+
con.execute("LOAD sqlite;")
|
| 42 |
|
| 43 |
+
try:
|
| 44 |
+
result = con.execute(f"""
|
| 45 |
+
SELECT *
|
| 46 |
+
FROM sqlite_scan('{db}','telegram_users')
|
| 47 |
+
WHERE user_id LIKE '{q}%'
|
| 48 |
+
LIMIT 20
|
| 49 |
+
""").fetchall()
|
| 50 |
|
| 51 |
+
return {
|
| 52 |
+
"status": True,
|
| 53 |
+
"count": len(result),
|
| 54 |
+
"results": result
|
| 55 |
+
}
|
| 56 |
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return {
|
| 59 |
+
"status": False,
|
| 60 |
+
"error": str(e)
|
| 61 |
+
}
|
| 62 |
|
| 63 |
+
finally:
|
| 64 |
+
con.close()
|
| 65 |
|
|
|
|
| 66 |
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|