| import os |
| import duckdb |
| import uvicorn |
| from fastapi import FastAPI |
| from huggingface_hub import hf_hub_download |
|
|
| app = FastAPI(title="Telegram API") |
|
|
| DB_PATH = None |
|
|
| def get_db(): |
| global DB_PATH |
|
|
| if DB_PATH is None: |
| DB_PATH = hf_hub_download( |
| repo_id="fdsgfuisfydgiuef/Telegram", |
| repo_type="dataset", |
| filename="Midnight_Master_v2.db", |
| token=os.getenv("HF_TOKEN") |
| ) |
|
|
| return DB_PATH |
|
|
|
|
| @app.get("/") |
| def home(): |
| return { |
| "status": True, |
| "message": "Telegram API Running" |
| } |
|
|
|
|
| @app.get("/search") |
| def search(q: str): |
|
|
| db = get_db() |
|
|
| con = duckdb.connect() |
|
|
| con.execute("INSTALL sqlite;") |
| con.execute("LOAD sqlite;") |
|
|
| try: |
| result = con.execute(f""" |
| SELECT * |
| FROM sqlite_scan('{db}','telegram_users') |
| WHERE user_id LIKE '{q}%' |
| LIMIT 20 |
| """).fetchall() |
|
|
| return { |
| "status": True, |
| "count": len(result), |
| "results": result |
| } |
|
|
| except Exception as e: |
| return { |
| "status": False, |
| "error": str(e) |
| } |
|
|
| finally: |
| con.close() |
|
|
|
|
| if __name__ == "__main__": |
| uvicorn.run(app, host="0.0.0.0", port=7860) |