Spaces:
Sleeping
Sleeping
File size: 4,433 Bytes
a3f0e31 f41da16 a8f9e0c f87e442 a3f0e31 d1bc8c9 a3f0e31 a8f9e0c 666196b f41da16 a3f0e31 a8f9e0c d1bc8c9 a8f9e0c f41da16 a8f9e0c f41da16 08196a0 a8f9e0c d1bc8c9 f87e442 d1bc8c9 f87e442 a8f9e0c f87e442 a8f9e0c d1bc8c9 a8f9e0c f41da16 a8f9e0c a3f0e31 d1bc8c9 a3f0e31 d1bc8c9 a3f0e31 a8f9e0c d1bc8c9 a3f0e31 a8f9e0c d1bc8c9 f87e442 a8f9e0c d1bc8c9 a8f9e0c a3f0e31 d1bc8c9 a3f0e31 d1bc8c9 a3f0e31 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | 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"
@app.on_event("startup")
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)}")
@app.get("/")
def read_root():
return {
"success": True,
"message": "Custom JSON API is 100% Online!",
"developer": "coder petro"
}
@app.get("/search")
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
}
|