File size: 3,527 Bytes
3874081
254e83c
 
 
26cf726
 
 
254e83c
 
3ee2c54
e3fa5c5
254e83c
3ee2c54
 
 
 
 
 
 
254e83c
3ee2c54
 
254e83c
 
3ee2c54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254e83c
 
26cf726
3ee2c54
 
 
 
 
 
 
 
35bb9ae
 
3ee2c54
35bb9ae
 
3ee2c54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35bb9ae
 
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
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))