Spaces:
Running
Running
Download main.py from coderpetro/tgtonum-api: direct link, hf CLI and curl.
- Browser
- Download file 4.43 kB
-
https://huggingface.co/spaces/coderpetro/tgtonum-api/resolve/main/main.py
- Command line
-
hf download hf://spaces/coderpetro/tgtonum-api/main.py
-
curl -L -o main.py https://huggingface.co/spaces/coderpetro/tgtonum-api/resolve/main/main.py
4.43 kB
| from fastapi import FastAPI, HTTPException, Security, Query | |
| from fastapi.security.api_key import APIKeyHeader | |
| from huggingface_hub import hf_hub_download | |
| import sqlite3 | |
| import csv | |
| import os | |
| app = FastAPI(title="Telegram Database API", description="Custom JSON Output Engine by coder petro") | |
| API_KEY = "demo" | |
| API_KEY_NAME = "x-api-key" | |
| api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False) | |
| DB_PATH = "telegram_records.db" | |
| REPO_ID = "coderpetro/telegram-data" | |
| FILENAME = "Telegram_27.csv" | |
| def setup_sqlite_database(): | |
| if os.path.exists(DB_PATH) and os.path.getsize(DB_PATH) > 1000000: | |
| print("SQLite Database already exists. Ready for queries!") | |
| return | |
| try: | |
| print("Downloading raw database from private dataset...") | |
| token = os.environ.get("HF_TOKEN") | |
| csv_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME, repo_type="dataset", token=token) | |
| print("Initializing SQLite local engine...") | |
| conn = sqlite3.connect(DB_PATH) | |
| cursor = conn.cursor() | |
| cursor.execute("CREATE TABLE IF NOT EXISTS telegram (raw_data TEXT)") | |
| print("Converting CSV data into indexed SQLite tokens...") | |
| with open(csv_path, mode='r', encoding='utf-8', errors='ignore') as f: | |
| reader = csv.reader(f, delimiter='|') | |
| next(reader, None) # Skip header | |
| batch = [] | |
| for row in reader: | |
| if row: | |
| row_str = " | ".join(row) | |
| batch.append((row_str,)) | |
| if len(batch) >= 50000: | |
| cursor.executemany("INSERT INTO telegram (raw_data) VALUES (?)", batch) | |
| batch = [] | |
| if batch: | |
| cursor.executemany("INSERT INTO telegram (raw_data) VALUES (?)", batch) | |
| conn.commit() | |
| conn.close() | |
| print("Success! Data fully migrated to SQLite.") | |
| if os.path.exists(csv_path): | |
| os.remove(csv_path) | |
| except Exception as e: | |
| print(f"Database generation failed: {str(e)}") | |
| def read_root(): | |
| return { | |
| "success": True, | |
| "message": "Custom JSON API is 100% Online!", | |
| "developer": "coder petro" | |
| } | |
| def search_data( | |
| query: str = Query(..., description="The value to search for"), | |
| key: str = Query(None, description="API Key via URL"), | |
| api_key_header: str = Security(api_key_header) | |
| ): | |
| provided_key = api_key_header or key | |
| if not provided_key or provided_key != API_KEY: | |
| raise HTTPException(status_code=401, detail={"success": False, "developer": "coder petro", "error": "Unauthorized."}) | |
| if not os.path.exists(DB_PATH): | |
| raise HTTPException(status_code=500, detail="Database generation in progress. Try again in a bit.") | |
| search_term = f"%{query.lower()}%" | |
| formatted_results = [] | |
| try: | |
| conn = sqlite3.connect(DB_PATH) | |
| cursor = conn.cursor() | |
| cursor.execute("SELECT raw_data FROM telegram WHERE lower(raw_data) LIKE ? LIMIT 500", (search_term,)) | |
| rows = cursor.fetchall() | |
| conn.close() | |
| for row in rows: | |
| raw_str = row[0] | |
| # Split the pipe separated string back to clean components | |
| parts = [p.strip() for p in raw_str.split("|")] | |
| # Formatting data dynamically to match your exact request | |
| name = parts[1] if len(parts) > 1 else "" | |
| last_name = parts[2] if len(parts) > 2 else "" | |
| full_name = f"{name} {last_name}".strip() | |
| phone = parts[3] if len(parts) > 3 else "" | |
| user_id = parts[4] if len(parts) > 4 else "" | |
| username = parts[5] if len(parts) > 5 else "" | |
| formatted_results.append({ | |
| "query": query, | |
| "name": full_name, | |
| "username": username, | |
| "phone": phone, | |
| "user_id": user_id, | |
| "country_code": "91" if phone.startswith("91") else "Unknown" | |
| }) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Query failed: {str(e)}") | |
| return { | |
| "success": True, | |
| "developer": "coder petro", | |
| "total_results": len(formatted_results), | |
| "results": formatted_results | |
| } | |