coderpetro commited on
Commit
3ee2c54
·
verified ·
1 Parent(s): 35bb9ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -91
app.py CHANGED
@@ -1,5 +1,4 @@
1
  from fastapi import FastAPI, Query, HTTPException
2
- from fastapi.responses import HTMLResponse
3
  import duckdb
4
 
5
  app = FastAPI(
@@ -8,107 +7,90 @@ app = FastAPI(
8
  version="3.0.0"
9
  )
10
 
 
11
  DATA_URL = "hf://datasets/coderpetro/my-fast-data/data.parquet"
12
 
13
- @app.get("/", response_class=HTMLResponse, include_in_schema=False)
14
- def web_ui():
15
- html_content = """
16
- <!DOCTYPE html>
17
- <html lang="en">
18
- <head>
19
- <meta charset="UTF-8">
20
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
21
- <title>Telegram Intelligence Matrix</title>
22
- <link href="https://jsdelivr.net" rel="stylesheet">
23
- <style>
24
- body { background: #0f172a; color: #f8fafc; min-height: 100vh; display: flex; align-items: center; justify-content: center; }
25
- .card { background: #1e293b; border: 1px solid rgba(255,255,255,0.1); border-radius: 16px; width: 100%; max-width: 550px; padding: 30px; box-shadow: 0 20px 40px rgba(0,0,0,0.5); }
26
- .nav-link { color: #94a3b8; cursor: pointer; font-weight: 600; }
27
- .nav-link.active { color: #38bdf8 !important; background: transparent !important; border-bottom: 2px solid #38bdf8; }
28
- input { background: #0f172a !important; color: #fff !important; border: 1px solid rgba(255,255,255,0.1) !important; padding: 12px !important; border-radius: 8px !important; }
29
- .btn-search { background: linear-gradient(to right, #38bdf8, #6366f1); color: #fff; font-weight: 600; border: none; padding: 12px; border-radius: 8px; width: 100%; }
30
- .res-box { background: #0f172a; border-radius: 8px; padding: 15px; margin-top: 20px; display: none; border: 1px solid rgba(255,255,255,0.05); }
31
- </style>
32
- </head>
33
- <body>
34
- <div class="card">
35
- <h2 class="text-center mb-1" style="color: #38bdf8;">Intelligence Matrix</h2>
36
- <p class="text-center text-muted small mb-4">Cloud Data Engine by coderpetro</p>
37
- <ul class="nav nav-tabs nav-fill mb-4">
38
- <li class="nav-item"><a class="nav-link active" id="tgTab" onclick="setMode('tg')">TG to Number</a></li>
39
- <li class="nav-item"><a class="nav-link" id="numTab" onclick="setMode('num')">Number to TG</a></li>
40
- </ul>
41
- <div class="mb-3"><input type="text" id="inp" class="form-control" placeholder="Enter Numeric Telegram ID..."></div>
42
- <button class="btn-search" onclick="doSearch()">Search Repository</button>
43
- <div id="ld" class="text-center text-muted small mt-3" style="display:none;">Searching global matrix...</div>
44
- <div id="box" class="res-box">
45
- <div class="d-flex justify-content-between py-1"><span>ID:</span><span id="rId" style="color:#38bdf8;">-</span></div>
46
- <div class="d-flex justify-content-between py-1"><span>Number:</span><span id="rNum" style="color:#38bdf8;">-</span></div>
47
- <div class="d-flex justify-content-between py-1"><span>Country:</span><span id="rC" style="color:#38bdf8;">-</span></div>
48
- <div class="d-flex justify-content-between py-1"><span>Developer:</span><span id="rD" style="color:#38bdf8;">-</span></div>
49
- </div>
50
- </div>
51
- <script>
52
- let mode = 'tg';
53
- function setMode(m) {
54
- mode = m;
55
- document.getElementById('tgTab').classList.toggle('active', m=='tg');
56
- document.getElementById('numTab').classList.toggle('active', m=='num');
57
- document.getElementById('inp').placeholder = m=='tg' ? 'Enter Numeric Telegram ID...' : 'Enter Phone Number...';
58
- document.getElementById('box').style.display = 'none';
59
- }
60
- async function doSearch() {
61
- let val = document.getElementById('inp').value.strip();
62
- if(!val) return;
63
- document.getElementById('ld').style.display = 'block';
64
- document.getElementById('box').style.display = 'none';
65
- let url = mode == 'tg' ? `/tg-to-num?tg_id=${val}` : `/num-to-tg?phone=${encodeURIComponent(val)}`;
66
- try {
67
- let r = await fetch(url);
68
- let d = await r.json();
69
- document.getElementById('ld').style.display = 'none';
70
- if(r.status == 200 && !d.status) {
71
- document.getElementById('rId').innerText = d.id;
72
- document.getElementById('rNum').innerText = d.number;
73
- document.getElementById('rC').innerText = d.country;
74
- document.getElementById('rD').innerText = d.developer;
75
- document.getElementById('box').style.display = 'block';
76
- } else { alert(d.message || "Record not tracking."); }
77
- } catch(e) { document.getElementById('ld').style.display = 'none'; alert("Gateway Failure."); }
78
- }
79
- </script>
80
- </body>
81
- </html>
82
- """
83
- return html_content
84
-
85
- @app.get("/tg-to-num")
86
- def telegram_to_number(tg_id: str = Query(..., description="Telegram ID")):
87
  if not tg_id.isdigit():
88
- raise HTTPException(status_code=400, detail="Must be numeric.")
 
89
  try:
90
  con = duckdb.connect()
91
- res = con.execute(f'SELECT "Номер телефона" FROM "{DATA_URL}" WHERE "Telegram ID" = \'{tg_id}\' LIMIT 1').fetchall()
92
- if res and len(res) > 0:
93
- p = str(res[0][0]).strip()
94
- c = p.replace("+", "").strip()[:2] if len(p) > 5 else "Unknown"
95
- return {"telegram id": f"telegram id ; {tg_id}", "id": tg_id, "number": p, "country": f"+{c}", "developer": "coderpetro"}
96
- return {"status": 404, "id": tg_id, "message": "Not found."}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  except Exception as e:
98
  raise HTTPException(status_code=500, detail=str(e))
99
 
100
- @app.get("/num-to-tg")
101
- def number_to_telegram(phone: str = Query(..., description="Phone Number")):
 
 
 
 
 
 
102
  p1 = phone.strip()
103
  p2 = f"+{p1}" if not p1.startswith("+") else p1.replace("+", "")
 
104
  try:
105
  con = duckdb.connect()
106
- res = con.execute(f'SELECT "Telegram ID" FROM "{DATA_URL}" WHERE "Номер телефона" = \'{p1}\' OR "Номер телефона" = \'{p2}\' LIMIT 1').fetchall()
107
- if res and len(res) > 0:
108
- tid = str(res[0][0]).strip()
109
- num = p1 if p1.startswith("+") else f"+{p1}"
110
- c = num.replace("+", "").strip()[:2] if len(num) > 5 else "Unknown"
111
- return {"telegram id": f"telegram id ; {tid}", "id": tid, "number": num, "country": f"+{c}", "developer": "coderpetro"}
112
- return {"status": 404, "number": phone, "message": "Not found."}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  except Exception as e:
114
  raise HTTPException(status_code=500, detail=str(e))
 
1
  from fastapi import FastAPI, Query, HTTPException
 
2
  import duckdb
3
 
4
  app = FastAPI(
 
7
  version="3.0.0"
8
  )
9
 
10
+ # Cloud Storage Dataset Connection Path
11
  DATA_URL = "hf://datasets/coderpetro/my-fast-data/data.parquet"
12
 
13
+ # ==========================================
14
+ # ROUTE 1: TELEGRAM ID TO PHONE NUMBER (TG TO NUM)
15
+ # ==========================================
16
+ @app.get("/tg-to-num", summary="Query phone records using numeric Telegram ID")
17
+ def telegram_to_number(
18
+ tg_id: str = Query(..., description="The target numeric Telegram ID to look up")
19
+ ):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  if not tg_id.isdigit():
21
+ raise HTTPException(status_code=400, detail="Query validation error: Telegram ID must be completely numeric.")
22
+
23
  try:
24
  con = duckdb.connect()
25
+ sql_query = f"""
26
+ SELECT "Номер телефона"
27
+ FROM '{DATA_URL}'
28
+ WHERE "Telegram ID" = '{tg_id}'
29
+ LIMIT 1
30
+ """
31
+ db_result = con.execute(sql_query).fetchall()
32
+
33
+ if db_result and len(db_result) > 0:
34
+ raw_phone = str(db_result[0][0]).strip()
35
+ clean_phone = raw_phone.replace("+", "").strip()
36
+ country_code = clean_phone[:2] if len(clean_phone) > 5 else "Unknown"
37
+
38
+ return {
39
+ "telegram id": f"telegram id ; {tg_id}",
40
+ "id": tg_id,
41
+ "number": raw_phone,
42
+ "country": f"+{country_code}",
43
+ "developer": "coderpetro"
44
+ }
45
+ else:
46
+ return {
47
+ "status": 404,
48
+ "id": tg_id,
49
+ "message": "No matching log records found in database matrix."
50
+ }
51
  except Exception as e:
52
  raise HTTPException(status_code=500, detail=str(e))
53
 
54
+
55
+ # ==========================================
56
+ # ROUTE 2: PHONE NUMBER TO TELEGRAM ID (NUM TO TG)
57
+ # ==========================================
58
+ @app.get("/num-to-tg", summary="Query Telegram ID using Phone Number")
59
+ def number_to_telegram(
60
+ phone: str = Query(..., description="The target phone number")
61
+ ):
62
  p1 = phone.strip()
63
  p2 = f"+{p1}" if not p1.startswith("+") else p1.replace("+", "")
64
+
65
  try:
66
  con = duckdb.connect()
67
+ sql_query = f"""
68
+ SELECT "Telegram ID"
69
+ FROM '{DATA_URL}'
70
+ WHERE "Номер телефона" = '{p1}'
71
+ OR "Номер телефона" = '{p2}'
72
+ LIMIT 1
73
+ """
74
+ db_result = con.execute(sql_query).fetchall()
75
+
76
+ if db_result and len(db_result) > 0:
77
+ found_tg_id = str(db_result[0][0]).strip()
78
+ display_phone = p1 if p1.startswith("+") else f"+{p1}"
79
+ clean_digits = display_phone.replace("+", "").strip()
80
+ country_code = clean_digits[:2] if len(clean_digits) > 5 else "Unknown"
81
+
82
+ return {
83
+ "telegram id": f"telegram id ; {found_tg_id}",
84
+ "id": found_tg_id,
85
+ "number": display_phone,
86
+ "country": f"+{country_code}",
87
+ "developer": "coderpetro"
88
+ }
89
+ else:
90
+ return {
91
+ "status": 404,
92
+ "number": phone,
93
+ "message": "No matching Telegram logs found for this phone number."
94
+ }
95
  except Exception as e:
96
  raise HTTPException(status_code=500, detail=str(e))