from fastapi import FastAPI, Query, HTTPException import duckdb app = FastAPI( title="Telegram Cross-Reference Matrix Intelligence API", description="High-speed dual-routing query database engine created by coderpetro.", version="3.0.0" ) # Cloud Storage Dataset Connection Path DATA_URL = "hf://datasets/coderpetro/my-fast-data/data.parquet" # ========================================== # ROUTE 1: TELEGRAM ID TO PHONE NUMBER (TG TO NUM) # ========================================== @app.get("/tg-to-num", summary="Query phone records using numeric Telegram ID") def telegram_to_number( tg_id: str = Query(..., description="The target numeric Telegram ID to look up") ): if not tg_id.isdigit(): raise HTTPException(status_code=400, detail="Query validation error: Telegram ID must be completely numeric.") try: con = duckdb.connect() sql_query = f""" SELECT "Номер телефона" FROM '{DATA_URL}' WHERE "Telegram ID" = '{tg_id}' LIMIT 1 """ db_result = con.execute(sql_query).fetchall() if db_result and len(db_result) > 0: raw_phone = str(db_result[0][0]).strip() clean_phone = raw_phone.replace("+", "").strip() country_code = clean_phone[:2] if len(clean_phone) > 5 else "Unknown" return { "telegram id": f"telegram id ; {tg_id}", "id": tg_id, "number": raw_phone, "country": f"+{country_code}", "developer": "coderpetro" } else: return { "status": 404, "id": tg_id, "message": "No matching log records found in database matrix." } except Exception as e: raise HTTPException(status_code=500, detail=str(e)) # ========================================== # ROUTE 2: PHONE NUMBER TO TELEGRAM ID (NUM TO TG) # ========================================== @app.get("/num-to-tg", summary="Query Telegram ID using Phone Number") def number_to_telegram( phone: str = Query(..., description="The target phone number") ): p1 = phone.strip() p2 = f"+{p1}" if not p1.startswith("+") else p1.replace("+", "") try: con = duckdb.connect() sql_query = f""" SELECT "Telegram ID" FROM '{DATA_URL}' WHERE "Номер телефона" = '{p1}' OR "Номер телефона" = '{p2}' LIMIT 1 """ db_result = con.execute(sql_query).fetchall() if db_result and len(db_result) > 0: found_tg_id = str(db_result[0][0]).strip() display_phone = p1 if p1.startswith("+") else f"+{p1}" clean_digits = display_phone.replace("+", "").strip() country_code = clean_digits[:2] if len(clean_digits) > 5 else "Unknown" return { "telegram id": f"telegram id ; {found_tg_id}", "id": found_tg_id, "number": display_phone, "country": f"+{country_code}", "developer": "coderpetro" } else: return { "status": 404, "number": phone, "message": "No matching Telegram logs found for this phone number." } except Exception as e: raise HTTPException(status_code=500, detail=str(e))