Wasifali95 commited on
Commit
ef6b6af
·
verified ·
1 Parent(s): 92fc824

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +236 -0
main.py ADDED
@@ -0,0 +1,236 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # main.py
2
+ from fastapi import FastAPI, Query
3
+ from fastapi.responses import JSONResponse
4
+ from fastapi.middleware.cors import CORSMiddleware
5
+ import requests
6
+ import re
7
+ from bs4 import BeautifulSoup
8
+ import uvicorn
9
+
10
+ app = FastAPI(title="SIM Tracker API", version="1.0.0", docs_url=None, redoc_url=None)
11
+
12
+ # CORS middleware
13
+ app.add_middleware(
14
+ CORSMiddleware,
15
+ allow_origins=["*"],
16
+ allow_credentials=True,
17
+ allow_methods=["*"],
18
+ allow_headers=["*"],
19
+ )
20
+
21
+ # Headers exactly as in your request
22
+ HEADERS = {
23
+ "Host": "simlivetracker.com.pk",
24
+ "sec-ch-ua-platform": "Android",
25
+ "user-agent": "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Mobile Safari/537.36",
26
+ "sec-ch-ua": '"Not:A-Brand";v="99", "Google Chrome";v="145", "Chromium";v="145"',
27
+ "dnt": "1",
28
+ "sec-ch-ua-mobile": "?1",
29
+ "accept": "*/*",
30
+ "sec-fetch-site": "same-origin",
31
+ "sec-fetch-mode": "cors",
32
+ "sec-fetch-dest": "empty",
33
+ "referer": "https://simlivetracker.com.pk/",
34
+ "accept-encoding": "gzip, deflate, br, zstd",
35
+ "accept-language": "en-US,en;q=0.9,ur-PK;q=0.8,ur;q=0.7",
36
+ "priority": "u=1, i"
37
+ }
38
+
39
+ def detect_network(mobile: str) -> str:
40
+ if not mobile or len(mobile) < 4:
41
+ return "Unknown"
42
+ try:
43
+ prefix = int(mobile[:4])
44
+ if 301 <= prefix <= 329:
45
+ return "Jazz"
46
+ elif 310 <= prefix <= 319:
47
+ return "Zong"
48
+ elif 330 <= prefix <= 339:
49
+ return "Ufone"
50
+ elif 340 <= prefix <= 359:
51
+ return "Telenor"
52
+ else:
53
+ return "Unknown"
54
+ except:
55
+ return "Unknown"
56
+
57
+ def parse_html_to_json(html: str, search: str, search_type: str) -> dict:
58
+ """Convert HTML response to clean JSON"""
59
+ soup = BeautifulSoup(html, 'html.parser')
60
+ text = html
61
+
62
+ if search_type == "mobile":
63
+ # Extract single record
64
+ name_match = re.search(r'Name[:\s]*([^<\n]+)', text, re.IGNORECASE)
65
+ mobile_match = re.search(r'Mobile[:\s]*([0-9]+)', text, re.IGNORECASE)
66
+ cnic_match = re.search(r'CNIC[:\s]*([0-9-]+)', text, re.IGNORECASE)
67
+ address_match = re.search(r'Address[:\s]*([^<\n]+)', text, re.IGNORECASE)
68
+
69
+ result = {}
70
+ if mobile_match:
71
+ mobile_num = mobile_match.group(1).strip()
72
+ result["mobile"] = mobile_num
73
+ result["network"] = detect_network(mobile_num)
74
+ if name_match:
75
+ result["name"] = name_match.group(1).strip()
76
+ if cnic_match:
77
+ result["cnic"] = cnic_match.group(1).strip()
78
+ if address_match:
79
+ result["address"] = address_match.group(1).strip()
80
+
81
+ return result
82
+
83
+ else: # CNIC search
84
+ records = []
85
+
86
+ # Try table format
87
+ rows = soup.find_all('tr')
88
+ for row in rows:
89
+ cols = row.find_all('td')
90
+ if len(cols) >= 3:
91
+ mobile = cols[0].get_text(strip=True)
92
+ name = cols[1].get_text(strip=True)
93
+ cnic_val = cols[2].get_text(strip=True)
94
+ address_val = cols[3].get_text(strip=True) if len(cols) > 3 else ""
95
+
96
+ if mobile and re.match(r'^03[0-9]{9}$', mobile):
97
+ records.append({
98
+ "name": name,
99
+ "mobile": mobile,
100
+ "cnic": cnic_val,
101
+ "network": detect_network(mobile),
102
+ "address": address_val
103
+ })
104
+
105
+ # If no table, try regex
106
+ if not records:
107
+ pattern = r'([0-9]{11}).*?([A-Za-z\s]+).*?([0-9-]{13,15})'
108
+ matches = re.findall(pattern, text, re.DOTALL)
109
+ for match in matches:
110
+ mobile = match[0].strip()
111
+ name = match[1].strip()
112
+ cnic_val = match[2].strip()
113
+ if mobile and re.match(r'^03[0-9]{9}$', mobile):
114
+ records.append({
115
+ "name": name,
116
+ "mobile": mobile,
117
+ "cnic": cnic_val,
118
+ "network": detect_network(mobile)
119
+ })
120
+
121
+ return {
122
+ "cnic": search,
123
+ "total_sims": len(records),
124
+ "records": records
125
+ }
126
+
127
+ def fetch_from_your_api(search: str, search_type: str) -> dict:
128
+ """Fetch from simlivetracker.com.pk/api.php"""
129
+ url = "https://simlivetracker.com.pk/api.php"
130
+ params = {
131
+ "search": search,
132
+ "type": search_type
133
+ }
134
+
135
+ try:
136
+ response = requests.get(url, params=params, headers=HEADERS, timeout=15)
137
+
138
+ if response.status_code == 200:
139
+ html = response.text
140
+
141
+ # Parse HTML to JSON
142
+ data = parse_html_to_json(html, search, search_type)
143
+
144
+ if data:
145
+ return {"success": True, "data": data}
146
+ else:
147
+ return {"success": False, "message": "Could not parse data from HTML"}
148
+ else:
149
+ return {"success": False, "message": f"API returned status {response.status_code}"}
150
+
151
+ except Exception as e:
152
+ return {"success": False, "message": str(e)}
153
+
154
+ @app.get("/")
155
+ async def root():
156
+ return JSONResponse({
157
+ "status": "active",
158
+ "service": "SIM Database Tracker",
159
+ "endpoints": {
160
+ "mobile": "/api?search=03007058868",
161
+ "cnic": "/api?search=35201-1234567-8"
162
+ },
163
+ "developer": "WASIF ALI",
164
+ "telegram": "@THE_FREE_HACKS"
165
+ })
166
+
167
+ @app.get("/api")
168
+ async def search(search: str = Query(..., description="Mobile number or CNIC")):
169
+ search_clean = re.sub(r'\s+', '', search)
170
+
171
+ # Detect type
172
+ if re.match(r'^03[0-9]{9}$', search_clean):
173
+ search_type = "mobile"
174
+ elif re.match(r'^[0-9]{5}-[0-9]{7}-[0-9]$', search_clean) or re.match(r'^[0-9]{13}$', search_clean):
175
+ search_type = "cnic"
176
+ if re.match(r'^[0-9]{13}$', search_clean):
177
+ search_clean = f"{search_clean[:5]}-{search_clean[5:12]}-{search_clean[12]}"
178
+ else:
179
+ return JSONResponse({
180
+ "status": False,
181
+ "message": "Invalid format. Use mobile (03xxxxxxxxx) or CNIC (xxxxx-xxxxxxx-x)",
182
+ "developer": "WASIF ALI",
183
+ "telegram": "@THE_FREE_HACKS"
184
+ }, status_code=400)
185
+
186
+ # Fetch from your API
187
+ result = fetch_from_your_api(search_clean, search_type)
188
+
189
+ if result.get("success"):
190
+ response_data = {
191
+ "status": True,
192
+ "developer": "WASIF ALI",
193
+ "telegram": "@THE_FREE_HACKS"
194
+ }
195
+
196
+ if search_type == "mobile":
197
+ data = result["data"]
198
+ if data.get("name"):
199
+ response_data["name"] = data["name"]
200
+ if data.get("mobile"):
201
+ response_data["mobile"] = data["mobile"]
202
+ if data.get("cnic"):
203
+ response_data["cnic"] = data["cnic"]
204
+ if data.get("network"):
205
+ response_data["network"] = data["network"]
206
+ if data.get("address"):
207
+ response_data["address"] = data["address"]
208
+ else:
209
+ data = result["data"]
210
+ if data.get("cnic"):
211
+ response_data["cnic"] = data["cnic"]
212
+ if data.get("total_sims"):
213
+ response_data["total_sims"] = data["total_sims"]
214
+ if data.get("records"):
215
+ response_data["records"] = data["records"]
216
+
217
+ return JSONResponse(response_data)
218
+ else:
219
+ return JSONResponse({
220
+ "status": False,
221
+ "message": result.get("message", "No data found"),
222
+ "developer": "WASIF ALI",
223
+ "telegram": "@THE_FREE_HACKS"
224
+ }, status_code=404)
225
+
226
+ @app.get("/test")
227
+ async def test():
228
+ return JSONResponse({
229
+ "status": True,
230
+ "message": "API is working. Using your API: simlivetracker.com.pk",
231
+ "developer": "WASIF ALI",
232
+ "telegram": "@THE_FREE_HACKS"
233
+ })
234
+
235
+ if __name__ == "__main__":
236
+ uvicorn.run(app, host="0.0.0.0", port=7860)