coderpetro commited on
Commit
26cf726
·
verified ·
1 Parent(s): 3874081

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -15
app.py CHANGED
@@ -1,21 +1,145 @@
1
  from fastapi import FastAPI, Query, HTTPException
 
2
  import duckdb
3
 
4
  app = FastAPI(
5
- title="Telegram Cloud Core Matrix Infrastructure API",
6
- description="Public high-speed query routing engine created by coderpetro.",
7
- version="2.0.0"
8
  )
9
 
10
- # Cloud Storage Dataset Connection Path mapped directly to your user repo
11
  DATA_URL = "hf://datasets/coderpetro/my-fast-data/data.parquet"
12
 
13
- @app.get("/tg-to-num")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def telegram_to_number(
15
- tg_id: str = Query(..., description="The target numeric Telegram ID")
16
  ):
17
  if not tg_id.isdigit():
18
- raise HTTPException(status_code=400, detail="Telegram ID query sequence must be completely numeric.")
19
 
20
  try:
21
  con = duckdb.connect()
@@ -27,14 +151,9 @@ def telegram_to_number(
27
  """
28
  db_result = con.execute(sql_query).fetchall()
29
 
30
- if db_result:
31
  raw_phone = str(db_result[0][0]).strip()
32
-
33
- # Remove leading plus signs or empty spaces to isolate digits cleanly
34
  clean_phone = raw_phone.replace("+", "").strip()
35
-
36
- # Country Code Extraction Logic
37
- # Slices the first two character indexes to find country blocks safely
38
  country_code = clean_phone[:2] if len(clean_phone) > 5 else "Unknown"
39
 
40
  return {
@@ -47,8 +166,15 @@ def telegram_to_number(
47
  else:
48
  return {
49
  "status": 404,
50
- "telegram_id": tg_id,
51
- "message": "No matching log blocks found in archive matrix."
52
  }
53
  except Exception as e:
54
  raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI, Query, HTTPException
2
+ from fastapi.responses import HTMLResponse
3
  import duckdb
4
 
5
  app = FastAPI(
6
+ title="Telegram Cross-Reference Matrix Intelligence API",
7
+ description="High-speed dual-routing query database engine created by coderpetro.",
8
+ version="3.0.0"
9
  )
10
 
11
+ # Cloud Storage Dataset Connection Path
12
  DATA_URL = "hf://datasets/coderpetro/my-fast-data/data.parquet"
13
 
14
+ # ==========================================
15
+ # HOME ROUTE: BEAUTIFUL WEB USER INTERFACE (HTML/CSS)
16
+ # ==========================================
17
+ @app.get("/", response_class=HTMLResponse, include_in_schema=False)
18
+ def web_ui():
19
+ html_content = """
20
+ <!DOCTYPE html>
21
+ <html lang="en">
22
+ <head>
23
+ <meta charset="UTF-8">
24
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
25
+ <title>Telegram Intelligence Matrix</title>
26
+ <link href="https://googleapis.com" rel="stylesheet">
27
+ <style>
28
+ * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Inter', sans-serif; }
29
+ body { background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%); color: #f8fafc; min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 20px; }
30
+ .container { background: rgba(30, 41, 59, 0.7); backdrop-filter: blur(16px); border: 1px solid rgba(255,255,255,0.1); border-radius: 24px; padding: 40px; width: 100%; max-width: 650px; box-shadow: 0 25px 50px -12px rgba(0,0,0,0.5); text-align: center; }
31
+ h1 { font-size: 2.5rem; font-weight: 700; margin-bottom: 8px; background: linear-gradient(to right, #38bdf8, #818cf8); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
32
+ .subtitle { color: #94a3b8; font-size: 0.95rem; margin-bottom: 35px; }
33
+ .tabs { display: flex; gap: 10px; margin-bottom: 25px; background: rgba(15, 23, 42, 0.5); padding: 6px; border-radius: 12px; }
34
+ .tab-btn { flex: 1; padding: 12px; border: none; border-radius: 8px; background: transparent; color: #94a3b8; font-weight: 600; cursor: pointer; transition: all 0.3s ease; }
35
+ .tab-btn.active { background: #38bdf8; color: #0f172a; box-shadow: 0 4px 12px rgba(56, 189, 248, 0.3); }
36
+ .input-group { position: relative; margin-bottom: 20px; }
37
+ input { width: 100%; padding: 16px 20px; background: #0f172a; border: 2px solid rgba(255,255,255,0.05); border-radius: 14px; color: #fff; font-size: 1.1rem; transition: all 0.3s ease; outline: none; }
38
+ input:focus { border-color: #38bdf8; box-shadow: 0 0 0 4px rgba(56, 189, 248, 0.15); }
39
+ .search-btn { width: 100%; padding: 16px; background: linear-gradient(to right, #38bdf8, #6366f1); color: #fff; border: none; border-radius: 14px; font-size: 1.1rem; font-weight: 600; cursor: pointer; transition: all 0.3s ease; box-shadow: 0 10px 20px -10px rgba(99, 102, 241, 0.5); }
40
+ .search-btn:hover { transform: translateY(-2px); box-shadow: 0 15px 25px -10px rgba(99, 102, 241, 0.6); }
41
+ .result-box { margin-top: 30px; display: none; text-align: left; background: rgba(15, 23, 42, 0.6); border-radius: 16px; padding: 25px; border: 1px solid rgba(255,255,255,0.05); }
42
+ .result-row { display: flex; justify-content: space-between; padding: 12px 0; border-bottom: 1px solid rgba(255,255,255,0.05); font-size: 1rem; }
43
+ .result-row:last-child { border: none; }
44
+ .label { color: #94a3b8; font-weight: 500; }
45
+ .value { color: #38bdf8; font-weight: 600; }
46
+ .error { color: #f43f5e; text-align: center; margin-top: 15px; font-weight: 500; display: none; }
47
+ .loader { width: 24px; height: 24px; border: 3px solid #94a3b8; border-bottom-color: transparent; border-radius: 50%; display: inline-block; box-sizing: border-box; animation: rotation 1s linear infinite; display: none; margin-top: 15px; }
48
+ @keyframes rotation { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
49
+ footer { margin-top: 40px; color: #64748b; font-size: 0.85rem; font-weight: 500; }
50
+ </style>
51
+ </head>
52
+ <body>
53
+ <div class="container">
54
+ <h1>Intelligence Matrix</h1>
55
+ <div class="subtitle">Secure high-speed query routing system by coderpetro</div>
56
+
57
+ <div class="tabs">
58
+ <button class="tab-btn active" onclick="switchTab('tg')">TG to Number</button>
59
+ <button class="tab-btn" onclick="switchTab('num')">Number to TG</button>
60
+ </div>
61
+
62
+ <div class="input-group">
63
+ <input type="text" id="searchInput" placeholder="Enter Numeric Telegram ID...">
64
+ </div>
65
+
66
+ <button class="search-btn" onclick="executeSearch()">Search Repository</button>
67
+
68
+ <div id="loader" class="loader"></div>
69
+ <div id="errorBox" class="error"></div>
70
+
71
+ <div id="resultBox" class="result-box">
72
+ <div class="result-row"><span class="label">Telegram ID:</span><span class="value" id="resId">-</span></div>
73
+ <div class="result-row"><span class="label">Phone Number:</span><span class="value" id="resNum">-</span></div>
74
+ <div class="result-row"><span class="label">Country:</span><span class="value" id="resCountry">-</span></div>
75
+ <div class="result-row"><span class="label">Developer:</span><span class="value" id="resDev">-</span></div>
76
+ </div>
77
+ </div>
78
+ <footer>Designed & Developed by coderpetro &copy; 2026</footer>
79
+
80
+ <script>
81
+ let currentMode = 'tg';
82
+
83
+ function switchTab(mode) {
84
+ currentMode = mode;
85
+ document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active'));
86
+ event.target.classList.add('active');
87
+ document.getElementById('searchInput').placeholder = mode === 'tg' ? 'Enter Numeric Telegram ID...' : 'Enter Phone Number (e.g. 9198765...)';
88
+ document.getElementById('resultBox').style.display = 'none';
89
+ document.getElementById('errorBox').style.display = 'none';
90
+ }
91
+
92
+ async function executeSearch() {
93
+ const queryVal = document.getElementById('searchInput').value.trim();
94
+ const loader = document.getElementById('loader');
95
+ const errorBox = document.getElementById('errorBox');
96
+ const resultBox = document.getElementById('resultBox');
97
+
98
+ if (!queryVal) return;
99
+
100
+ loader.style.display = 'inline-block';
101
+ errorBox.style.display = 'none';
102
+ resultBox.style.display = 'none';
103
+
104
+ let endpoint = currentMode === 'tg' ? `/tg-to-num?tg_id=${queryVal}` : `/num-to-tg?phone=${encodeURIComponent(queryVal)}`;
105
+
106
+ try {
107
+ const response = await fetch(endpoint);
108
+ const data = await response.json();
109
+
110
+ loader.style.display = 'none';
111
+
112
+ if (response.status === 200 && !data.status) {
113
+ document.getElementById('resId').innerText = data.id;
114
+ document.getElementById('resNum').innerText = data.number;
115
+ document.getElementById('resCountry').innerText = data.country;
116
+ document.getElementById('resDev').innerText = data.developer;
117
+ resultBox.style.display = 'block';
118
+ } else {
119
+ errorBox.innerText = data.detail || data.message || "No records tracking this parameter.";
120
+ errorBox.style.display = 'block';
121
+ }
122
+ } catch (err) {
123
+ loader.style.display = 'none';
124
+ errorBox.innerText = "Mainframe gateway connection failure.";
125
+ errorBox.style.display = 'block';
126
+ }
127
+ }
128
+ </script>
129
+ </body>
130
+ </html>
131
+ """
132
+ return html_content
133
+
134
+ # ==========================================
135
+ # ROUTE 2: TELEGRAM ID TO PHONE NUMBER (TG TO NUM)
136
+ # ==========================================
137
+ @app.get("/tg-to-num", summary="Query phone records using numeric Telegram ID")
138
  def telegram_to_number(
139
+ tg_id: str = Query(..., description="The target numeric Telegram ID to look up")
140
  ):
141
  if not tg_id.isdigit():
142
+ raise HTTPException(status_code=400, detail="Query validation error: Telegram ID must be completely numeric.")
143
 
144
  try:
145
  con = duckdb.connect()
 
151
  """
152
  db_result = con.execute(sql_query).fetchall()
153
 
154
+ if db_result and len(db_result) > 0:
155
  raw_phone = str(db_result[0][0]).strip()
 
 
156
  clean_phone = raw_phone.replace("+", "").strip()
 
 
 
157
  country_code = clean_phone[:2] if len(clean_phone) > 5 else "Unknown"
158
 
159
  return {
 
166
  else:
167
  return {
168
  "status": 404,
169
+ "id": tg_id,
170
+ "message": "No matching log records found in database matrix."
171
  }
172
  except Exception as e:
173
  raise HTTPException(status_code=500, detail=str(e))
174
+
175
+
176
+ # ==========================================
177
+ # ROUTE 3: PHONE NUMBER TO TELEGRAM ID (NUM TO TG)
178
+ # ==========================================
179
+ @app.get("/num-to-tg", summary="Query Telegram ID using Phone Number")
180
+ def number_to_telegram(