Muttered3 commited on
Commit
13dd7ff
Β·
verified Β·
1 Parent(s): 6fe06d8

Update scanner.py

Browse files
Files changed (1) hide show
  1. scanner.py +70 -74
scanner.py CHANGED
@@ -1,112 +1,108 @@
 
 
 
 
1
  import asyncio
2
- import re
3
  import random
 
4
  from datetime import datetime, timezone
5
  from curl_cffi.requests import AsyncSession
6
- from curl_cffi.requests.errors import RequestsError
7
  import config
8
 
9
- async def check_fragment_status(
10
- session: AsyncSession,
11
- word: str,
12
- semaphore: asyncio.Semaphore,
13
- job_id: str,
14
- ) -> dict:
15
- """Worker engine. Checks a single username against Fragment."""
16
  async with semaphore:
17
- target_url = f"https://fragment.com/username/{word}"
18
  retries = 0
19
-
20
  while retries <= config.MAX_RETRIES:
 
 
 
 
 
21
  try:
22
- proxy_dict = None
23
- if config.PROXY_POOL:
24
- proxy_str = random.choice(config.PROXY_POOL)
25
- proxy_dict = {"http": proxy_str, "https": proxy_str}
26
-
27
- response = await session.get(target_url, timeout=10, proxies=proxy_dict)
28
- http_status = response.status_code
29
  text = response.text
30
- final_url = str(response.url)
31
-
32
- # CLOUDFLARE DETECTION
33
- if ("Just a moment..." in text or
34
- 'id="challenge-running"' in text or
35
- http_status in [429, 403, 503]):
36
- retries += 1
37
  await asyncio.sleep(2 ** retries)
 
38
  continue
39
-
40
- # ROUTE-AGNOSTIC PARSING
41
- if http_status in [200, 404]:
42
  if "/?query=" in final_url:
43
- row_pattern = rf'<div class="table-cell-value tm-value">@{word}</div>(.*?)</tr'
44
- row_match = re.search(row_pattern, text, re.IGNORECASE | re.DOTALL)
45
- if row_match:
46
- status_match = re.search(r'<div class="table-cell-value tm-value tm-status-[^>]*>(.*?)</div>', row_match.group(1))
47
- status = status_match.group(1).strip() if status_match else "Unknown State"
 
 
 
 
 
 
 
48
  else:
49
  status = "Not Found in Search Results"
50
  else:
51
- match = re.search(r'<span class="tm-section-header-status[^>]*>(.*?)</span>', text)
52
  if match:
53
- status = match.group(1).strip()
54
  elif "Someone already claimed this username" in text:
55
  status = "Taken"
56
  elif "is currently not for sale" in text:
57
  status = "Unavailable"
58
  else:
59
- status = f"Unknown Route: {final_url.replace('https://fragment.com', '')}"
60
-
61
- await asyncio.sleep(config.DELAY_BETWEEN_REQUESTS)
62
- return {
63
- "Username": word,
64
- "Status": status,
65
- "Timestamp": datetime.now(timezone.utc).isoformat(),
66
- "JobID": job_id
67
- }
68
  else:
69
- return {
70
- "Username": word,
71
- "Status": f"Failed: HTTP {http_status}",
72
- "Timestamp": datetime.now(timezone.utc).isoformat(),
73
- "JobID": job_id
74
- }
75
 
76
- except (RequestsError, asyncio.TimeoutError, Exception):
77
  retries += 1
78
  await asyncio.sleep(1)
79
-
 
 
 
 
 
 
80
  return {
81
- "Username": word,
82
- "Status": "BLOCKED: Max Retries Hit",
83
- "Timestamp": datetime.now(timezone.utc).isoformat(),
84
  "JobID": job_id
85
  }
86
 
87
- async def run_scan(
88
- words: list[str],
89
- job_id: str,
90
- stop_event: asyncio.Event,
91
- progress_callback
92
- ):
93
- """Main scan loop processing in batches via asyncio.as_completed."""
94
  semaphore = asyncio.Semaphore(config.WORKERS)
95
 
96
  async with AsyncSession(impersonate="chrome110") as session:
97
- for i in range(0, len(words), config.BATCH_SIZE):
 
 
 
98
  if stop_event.is_set():
99
- break
100
-
101
- batch = words[i : i + config.BATCH_SIZE]
102
- tasks = [check_fragment_status(session, w, semaphore, job_id) for w in batch]
 
 
 
103
 
104
  batch_results = []
105
  for coro in asyncio.as_completed(tasks):
106
- if stop_event.is_set():
107
- break
108
- result = await coro
109
- batch_results.append(result)
110
-
111
- completed = min(i + len(batch_results), len(words))
112
- await progress_callback(completed, len(words), batch_results)
 
 
1
+ # ════════════════════════════════════════
2
+ # FILE: scanner.py
3
+ # ════════════════════════════════════════
4
+
5
  import asyncio
 
6
  import random
7
+ import re
8
  from datetime import datetime, timezone
9
  from curl_cffi.requests import AsyncSession
 
10
  import config
11
 
12
+ async def check_fragment_status(session: AsyncSession, word: str, semaphore: asyncio.Semaphore, job_id: str) -> dict:
 
 
 
 
 
 
13
  async with semaphore:
 
14
  retries = 0
15
+ status = "Unknown"
16
  while retries <= config.MAX_RETRIES:
17
+ proxy_dict = None
18
+ if config.PROXY_POOL:
19
+ proxy_str = random.choice(config.PROXY_POOL)
20
+ proxy_dict = {"http": proxy_str, "https": proxy_str}
21
+
22
  try:
23
+ url = f"https://fragment.com/username/{word}"
24
+ response = await session.get(url, timeout=10, proxies=proxy_dict)
 
 
 
 
 
25
  text = response.text
26
+
27
+ if response.status_code in [429, 403, 503] or "Just a moment..." in text or 'id="challenge-running"' in text:
 
 
 
 
 
28
  await asyncio.sleep(2 ** retries)
29
+ retries += 1
30
  continue
31
+
32
+ if response.status_code in [200, 404]:
33
+ final_url = str(response.url)
34
  if "/?query=" in final_url:
35
+ pattern = rf'<div class="table-cell-value tm-value">@{re.escape(word)}</div>(.*?)</tr>'
36
+ match = re.search(pattern, text, re.DOTALL)
37
+ if match:
38
+ status_html = match.group(1)
39
+ if "tm-status-available" in status_html:
40
+ status = "Available"
41
+ elif "tm-status-taken" in status_html:
42
+ status = "Taken"
43
+ elif "tm-status-unavail" in status_html:
44
+ status = "Unavailable"
45
+ else:
46
+ status = "Unknown Status Fragment"
47
  else:
48
  status = "Not Found in Search Results"
49
  else:
50
+ match = re.search(r'<span class="tm-section-header-status[^>]*">(.*?)</span>', text)
51
  if match:
52
+ status = re.sub(r'<[^>]+>', '', match.group(1)).strip()
53
  elif "Someone already claimed this username" in text:
54
  status = "Taken"
55
  elif "is currently not for sale" in text:
56
  status = "Unavailable"
57
  else:
58
+ status = f"Unknown: {final_url.replace('https://fragment.com', '')}"
59
+ break
 
 
 
 
 
 
 
60
  else:
61
+ status = f"Failed: HTTP {response.status_code}"
62
+ retries += 1
63
+ await asyncio.sleep(1)
 
 
 
64
 
65
+ except Exception as e:
66
  retries += 1
67
  await asyncio.sleep(1)
68
+ status = f"Error: {type(e).__name__}"
69
+
70
+ if retries > config.MAX_RETRIES and status.startswith("Unknown"):
71
+ status = "BLOCKED: Max Retries Hit"
72
+
73
+ await asyncio.sleep(config.DELAY_BETWEEN_REQUESTS)
74
+
75
  return {
76
+ "Username": word,
77
+ "Status": status,
78
+ "Timestamp": datetime.now(timezone.utc).isoformat(),
79
  "JobID": job_id
80
  }
81
 
82
+ async def run_scan(words: list[str], job_id: str, stop_event: asyncio.Event, progress_callback):
 
 
 
 
 
 
83
  semaphore = asyncio.Semaphore(config.WORKERS)
84
 
85
  async with AsyncSession(impersonate="chrome110") as session:
86
+ completed_so_far = 0
87
+ total = len(words)
88
+
89
+ for i in range(0, total, config.BATCH_SIZE):
90
  if stop_event.is_set():
91
+ return
92
+
93
+ batch_words = words[i:i + config.BATCH_SIZE]
94
+ tasks = [
95
+ asyncio.create_task(check_fragment_status(session, word, semaphore, job_id))
96
+ for word in batch_words
97
+ ]
98
 
99
  batch_results = []
100
  for coro in asyncio.as_completed(tasks):
101
+ res = await coro
102
+ batch_results.append(res)
103
+ completed_so_far += 1
104
+
105
+ if stop_event.is_set():
106
+ return
107
+
108
+ await progress_callback(completed_so_far, total, batch_results)