Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -5,7 +5,7 @@ import sqlite3
|
|
| 5 |
import csv
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
app = FastAPI(title="Telegram Database API", description="
|
| 9 |
|
| 10 |
API_KEY = "demo"
|
| 11 |
API_KEY_NAME = "x-api-key"
|
|
@@ -17,9 +17,8 @@ FILENAME = "Telegram_27.csv"
|
|
| 17 |
|
| 18 |
@app.on_event("startup")
|
| 19 |
def setup_sqlite_database():
|
| 20 |
-
# If database is already created, skip the heavy conversion process
|
| 21 |
if os.path.exists(DB_PATH) and os.path.getsize(DB_PATH) > 1000000:
|
| 22 |
-
print("SQLite Database already exists
|
| 23 |
return
|
| 24 |
|
| 25 |
try:
|
|
@@ -30,35 +29,27 @@ def setup_sqlite_database():
|
|
| 30 |
print("Initializing SQLite local engine...")
|
| 31 |
conn = sqlite3.connect(DB_PATH)
|
| 32 |
cursor = conn.cursor()
|
| 33 |
-
|
| 34 |
-
# Create a highly optimized single text column table for global fast scanning
|
| 35 |
cursor.execute("CREATE TABLE IF NOT EXISTS telegram (raw_data TEXT)")
|
| 36 |
|
| 37 |
-
print("Converting
|
| 38 |
with open(csv_path, mode='r', encoding='utf-8', errors='ignore') as f:
|
| 39 |
reader = csv.reader(f, delimiter='|')
|
| 40 |
-
|
| 41 |
|
| 42 |
batch = []
|
| 43 |
for row in reader:
|
| 44 |
if row:
|
| 45 |
-
# Merge row to string for global quick wild-card searching
|
| 46 |
row_str = " | ".join(row)
|
| 47 |
batch.append((row_str,))
|
| 48 |
-
|
| 49 |
-
# Insert in batches of 50,000 rows to keep RAM empty
|
| 50 |
if len(batch) >= 50000:
|
| 51 |
cursor.executemany("INSERT INTO telegram (raw_data) VALUES (?)", batch)
|
| 52 |
batch = []
|
| 53 |
-
|
| 54 |
if batch:
|
| 55 |
cursor.executemany("INSERT INTO telegram (raw_data) VALUES (?)", batch)
|
| 56 |
|
| 57 |
conn.commit()
|
| 58 |
conn.close()
|
| 59 |
-
print("Success!
|
| 60 |
-
|
| 61 |
-
# Safely delete the huge raw CSV to free server disk space
|
| 62 |
if os.path.exists(csv_path):
|
| 63 |
os.remove(csv_path)
|
| 64 |
|
|
@@ -68,7 +59,8 @@ def setup_sqlite_database():
|
|
| 68 |
@app.get("/")
|
| 69 |
def read_root():
|
| 70 |
return {
|
| 71 |
-
"
|
|
|
|
| 72 |
"developer": "coder petro"
|
| 73 |
}
|
| 74 |
|
|
@@ -80,31 +72,50 @@ def search_data(
|
|
| 80 |
):
|
| 81 |
provided_key = api_key_header or key
|
| 82 |
if not provided_key or provided_key != API_KEY:
|
| 83 |
-
raise HTTPException(status_code=401, detail={"developer": "coder petro", "error": "Unauthorized.
|
| 84 |
|
| 85 |
if not os.path.exists(DB_PATH):
|
| 86 |
-
raise HTTPException(status_code=500, detail="Database generation in progress. Try again in
|
| 87 |
|
| 88 |
search_term = f"%{query.lower()}%"
|
| 89 |
-
|
| 90 |
|
| 91 |
try:
|
| 92 |
conn = sqlite3.connect(DB_PATH)
|
| 93 |
cursor = conn.cursor()
|
| 94 |
-
|
| 95 |
-
# Blazing fast sub-second row matching using SQLite compiled C-level engine
|
| 96 |
cursor.execute("SELECT raw_data FROM telegram WHERE lower(raw_data) LIKE ? LIMIT 500", (search_term,))
|
| 97 |
rows = cursor.fetchall()
|
| 98 |
conn.close()
|
| 99 |
|
| 100 |
for row in rows:
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
raise HTTPException(status_code=500, detail=f"Query failed: {str(e)}")
|
| 105 |
|
| 106 |
return {
|
|
|
|
| 107 |
"developer": "coder petro",
|
| 108 |
-
"total_results": len(
|
| 109 |
-
"
|
| 110 |
}
|
|
|
|
| 5 |
import csv
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
app = FastAPI(title="Telegram Database API", description="Custom JSON Output Engine by coder petro")
|
| 9 |
|
| 10 |
API_KEY = "demo"
|
| 11 |
API_KEY_NAME = "x-api-key"
|
|
|
|
| 17 |
|
| 18 |
@app.on_event("startup")
|
| 19 |
def setup_sqlite_database():
|
|
|
|
| 20 |
if os.path.exists(DB_PATH) and os.path.getsize(DB_PATH) > 1000000:
|
| 21 |
+
print("SQLite Database already exists. Ready for queries!")
|
| 22 |
return
|
| 23 |
|
| 24 |
try:
|
|
|
|
| 29 |
print("Initializing SQLite local engine...")
|
| 30 |
conn = sqlite3.connect(DB_PATH)
|
| 31 |
cursor = conn.cursor()
|
|
|
|
|
|
|
| 32 |
cursor.execute("CREATE TABLE IF NOT EXISTS telegram (raw_data TEXT)")
|
| 33 |
|
| 34 |
+
print("Converting CSV data into indexed SQLite tokens...")
|
| 35 |
with open(csv_path, mode='r', encoding='utf-8', errors='ignore') as f:
|
| 36 |
reader = csv.reader(f, delimiter='|')
|
| 37 |
+
next(reader, None) # Skip header
|
| 38 |
|
| 39 |
batch = []
|
| 40 |
for row in reader:
|
| 41 |
if row:
|
|
|
|
| 42 |
row_str = " | ".join(row)
|
| 43 |
batch.append((row_str,))
|
|
|
|
|
|
|
| 44 |
if len(batch) >= 50000:
|
| 45 |
cursor.executemany("INSERT INTO telegram (raw_data) VALUES (?)", batch)
|
| 46 |
batch = []
|
|
|
|
| 47 |
if batch:
|
| 48 |
cursor.executemany("INSERT INTO telegram (raw_data) VALUES (?)", batch)
|
| 49 |
|
| 50 |
conn.commit()
|
| 51 |
conn.close()
|
| 52 |
+
print("Success! Data fully migrated to SQLite.")
|
|
|
|
|
|
|
| 53 |
if os.path.exists(csv_path):
|
| 54 |
os.remove(csv_path)
|
| 55 |
|
|
|
|
| 59 |
@app.get("/")
|
| 60 |
def read_root():
|
| 61 |
return {
|
| 62 |
+
"success": True,
|
| 63 |
+
"message": "Custom JSON API is 100% Online!",
|
| 64 |
"developer": "coder petro"
|
| 65 |
}
|
| 66 |
|
|
|
|
| 72 |
):
|
| 73 |
provided_key = api_key_header or key
|
| 74 |
if not provided_key or provided_key != API_KEY:
|
| 75 |
+
raise HTTPException(status_code=401, detail={"success": False, "developer": "coder petro", "error": "Unauthorized."})
|
| 76 |
|
| 77 |
if not os.path.exists(DB_PATH):
|
| 78 |
+
raise HTTPException(status_code=500, detail="Database generation in progress. Try again in a bit.")
|
| 79 |
|
| 80 |
search_term = f"%{query.lower()}%"
|
| 81 |
+
formatted_results = []
|
| 82 |
|
| 83 |
try:
|
| 84 |
conn = sqlite3.connect(DB_PATH)
|
| 85 |
cursor = conn.cursor()
|
|
|
|
|
|
|
| 86 |
cursor.execute("SELECT raw_data FROM telegram WHERE lower(raw_data) LIKE ? LIMIT 500", (search_term,))
|
| 87 |
rows = cursor.fetchall()
|
| 88 |
conn.close()
|
| 89 |
|
| 90 |
for row in rows:
|
| 91 |
+
raw_str = row[0]
|
| 92 |
+
# Split the pipe separated string back to clean components
|
| 93 |
+
parts = [p.strip() for p in raw_str.split("|")]
|
| 94 |
+
|
| 95 |
+
# Formatting data dynamically to match your exact request
|
| 96 |
+
name = parts[1] if len(parts) > 1 else ""
|
| 97 |
+
last_name = parts[2] if len(parts) > 2 else ""
|
| 98 |
+
full_name = f"{name} {last_name}".strip()
|
| 99 |
+
|
| 100 |
+
phone = parts[3] if len(parts) > 3 else ""
|
| 101 |
+
user_id = parts[4] if len(parts) > 4 else ""
|
| 102 |
+
username = parts[5] if len(parts) > 5 else ""
|
| 103 |
+
|
| 104 |
+
formatted_results.append({
|
| 105 |
+
"query": query,
|
| 106 |
+
"name": full_name,
|
| 107 |
+
"username": username,
|
| 108 |
+
"phone": phone,
|
| 109 |
+
"user_id": user_id,
|
| 110 |
+
"country_code": "91" if phone.startswith("91") else "Unknown"
|
| 111 |
+
})
|
| 112 |
|
| 113 |
except Exception as e:
|
| 114 |
raise HTTPException(status_code=500, detail=f"Query failed: {str(e)}")
|
| 115 |
|
| 116 |
return {
|
| 117 |
+
"success": True,
|
| 118 |
"developer": "coder petro",
|
| 119 |
+
"total_results": len(formatted_results),
|
| 120 |
+
"results": formatted_results
|
| 121 |
}
|