Muttered3 commited on
Commit
8c15670
Β·
verified Β·
1 Parent(s): 733a4f0

Update scraper.py

Browse files
Files changed (1) hide show
  1. scraper.py +89 -62
scraper.py CHANGED
@@ -1,89 +1,116 @@
1
  import asyncio
2
  import random
3
  import re
 
4
  import aiohttp
5
  from logger import get_logger
6
 
7
  log = get_logger()
8
 
9
  USER_AGENTS = [
10
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0 Safari/537.36",
11
- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 Safari/605.1.15",
12
- "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/123.0 Safari/537.36"
 
13
  ]
14
 
 
 
 
15
  async def check_fragment(word: str, proxy_url: str = None) -> str:
 
16
  url = f"https://fragment.com/username/{word}"
17
- headers = {"User-Agent": random.choice(USER_AGENTS)}
18
-
19
- for attempt in range(3):
 
 
 
 
20
  try:
21
- # Random jitter to prevent Cloudflare from detecting a fixed bot rhythm
22
- if attempt > 0:
23
- await asyncio.sleep(random.uniform(1.0, 3.0))
24
-
25
- timeout = aiohttp.ClientTimeout(total=15)
 
 
 
26
  async with aiohttp.ClientSession(timeout=timeout) as session:
27
- async with session.get(url, headers=headers, proxy=proxy_url, allow_redirects=True) as resp:
 
 
 
 
28
 
29
- if resp.status in [429, 403]:
30
- log.warning(f"Rate limited on {word}. Retrying...")
31
- await asyncio.sleep(random.uniform(3.0, 5.0))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  continue
33
-
 
 
 
 
34
  html = await resp.text()
35
-
36
- if "Just a moment..." in html or "cloudflare" in html.lower():
37
  await asyncio.sleep(random.uniform(3.0, 5.0))
38
  continue
39
-
40
- final_url = str(resp.url)
41
-
42
  # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
43
- # ENGINE 1: WE ARE ON THE PROFILE PAGE
44
  # ━━━━━━━���━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
45
- if "/username/" in final_url:
46
- status_match = re.search(r'class="tm-section-header-status[^"]*"[^>]*>\s*([^<]+?)\s*</span>', html, re.IGNORECASE)
47
- if status_match:
48
- s = status_match.group(1).strip().lower()
49
- if "sold" in s: return "SOLD"
50
- if "taken" in s: return "TAKEN"
51
- if "auction" in s: return "ON_AUCTION"
52
- if "available" in s: return "AVAILABLE"
53
- if "sale" in s or "purchase" in s: return "FOR_SALE"
54
- return s.upper()
55
-
56
- if 'class="tm-status-taken"' in html: return "TAKEN"
57
- if 'class="tm-status-unavail"' in html:
58
- if ">Sold<" in html or ">sold<" in html.lower() or "recently sold" in html.lower():
59
- return "SOLD"
60
- return "UNAVAILABLE"
61
 
 
 
 
 
 
 
62
  # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
63
- # ENGINE 2: WE WERE REDIRECTED TO THE SEARCH PAGE
 
64
  # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
65
- else:
66
- # Clean the HTML to make it a single searchable line
67
- clean_html = re.sub(r'\s+', ' ', html)
68
-
69
- # Find the exact username in the table, then grab the very next status block
70
- search_regex = re.compile(rf'>@{word}<.*?class="[^"]*status[^"]*"[^>]*>\s*([^<]+?)\s*<', re.IGNORECASE)
71
- match = search_regex.search(clean_html)
72
-
73
- if match:
74
- s = match.group(1).strip().lower()
75
- if "taken" in s: return "TAKEN"
76
- if "available" in s: return "AVAILABLE"
77
- if "sold" in s: return "SOLD"
78
- if "auction" in s: return "ON_AUCTION"
79
- if "unavailable" in s: return "UNAVAILABLE"
80
- return s.upper()
81
-
82
- # If the word is entirely missing from the search table, it is a banned/prohibited word.
83
- return "UNAVAILABLE"
84
 
85
  except Exception as e:
86
- log.error(f"Request error for {word} via {proxy_url}: {str(e)}")
87
- await asyncio.sleep(2.0)
88
-
89
- return "ERROR"
 
1
  import asyncio
2
  import random
3
  import re
4
+ import time
5
  import aiohttp
6
  from logger import get_logger
7
 
8
  log = get_logger()
9
 
10
  USER_AGENTS = [
11
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36",
12
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_4_1) AppleWebKit/605.1.15 Version/17.4.1 Safari/605.1.15",
13
+ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/123.0.0.0 Safari/537.36",
14
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0"
15
  ]
16
 
17
+ # Track adaptive rate limits per proxy instance in memory safely
18
+ _rate_limited_until = 0.0
19
+
20
  async def check_fragment(word: str, proxy_url: str = None) -> str:
21
+ global _rate_limited_until
22
  url = f"https://fragment.com/username/{word}"
23
+
24
+ # 4 Retries matching your advanced optimization matrix
25
+ for attempt in range(1, 5):
26
+ current_time = time.time()
27
+ if current_time < _rate_limited_until:
28
+ await asyncio.sleep(_rate_limited_until - current_time + random.uniform(0.1, 0.4))
29
+
30
  try:
31
+ headers = {
32
+ "User-Agent": random.choice(USER_AGENTS),
33
+ "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
34
+ "Accept-Language": "en-US,en;q=0.5",
35
+ "Referer": "https://fragment.com/"
36
+ }
37
+
38
+ timeout = aiohttp.ClientTimeout(total=10)
39
  async with aiohttp.ClientSession(timeout=timeout) as session:
40
+
41
+ # OPTIMIZATION: Start with allow_redirects=False to read metadata headers instantly
42
+ async with session.get(url, headers=headers, proxy=proxy_url, allow_redirects=False) as resp:
43
+ status = resp.status
44
+ location = resp.headers.get("Location", "")
45
 
46
+ # πŸš€ TWO-WAY METADATA REDIRECT DETECTOR (Zero body-parsing overhead)
47
+ if status in (301, 302, 303, 307, 308):
48
+ location_lower = location.lower()
49
+
50
+ # Case A: Explicit Search Page Redirect (Dropped / Unassigned Limbo)
51
+ if "?query=" in location_lower or f"query={word.lower()}" in location_lower:
52
+ return "UNAVAILABLE"
53
+
54
+ # Case B: Firewall Soft-Ban Redirect (Kicked back to clean root index)
55
+ elif location == "/" or location.rstrip('/') in ["https://fragment.com", "https://www.fragment.com"]:
56
+ backoff = (2 ** attempt) + random.uniform(0.5, 1.5)
57
+ _rate_limited_until = time.time() + backoff
58
+ log.warning(f"⚠️ Proxy soft-banned on '{word}'. Backing off {backoff:.2f}s...")
59
+ continue
60
+
61
+ if status in [429, 403]:
62
+ backoff = 4 + (2 ** attempt) + random.uniform(0.5, 1.5)
63
+ _rate_limited_until = time.time() + backoff
64
  continue
65
+
66
+ if status == 404:
67
+ return "UNAVAILABLE"
68
+
69
+ # Standard Profile Resolution Block (No Redirects)
70
  html = await resp.text()
71
+
72
+ if "Just a moment..." in html or "cloudflare" in html.lower() or 'cf-browser-verification' in html:
73
  await asyncio.sleep(random.uniform(3.0, 5.0))
74
  continue
75
+
 
 
76
  # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
77
+ # ENGINE 1: PROFILE DOM TARGETING
78
  # ━━━━━━━���━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
79
+ status_match = re.search(r'class="tm-section-header-status[^"]*"[^>]*>\s*([^<]+?)\s*</span>', html, re.IGNORECASE)
80
+ if status_match:
81
+ s = status_match.group(1).strip().lower()
82
+ if "sold" in s: return "SOLD"
83
+ if "taken" in s: return "TAKEN"
84
+ if "auction" in s: return "ON_AUCTION"
85
+ if "available" in s: return "AVAILABLE"
86
+ if "sale" in s or "purchase" in s: return "FOR_SALE"
87
+ return s.upper()
 
 
 
 
 
 
 
88
 
89
+ if 'class="tm-status-taken"' in html: return "TAKEN"
90
+ if 'class="tm-status-unavail"' in html or 'tm-section-header-status tm-status-unavail' in html:
91
+ if "sold for" in html.lower() or "tm-username-usable" in html.lower() or "recently sold" in html.lower():
92
+ return "SOLD"
93
+ return "UNAVAILABLE"
94
+
95
  # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
96
+ # ENGINE 2: FAILSAFE CONVERTED SEARCH LOOKUP LOOP
97
+ # (Runs if 200 OK was returned but page layout looks structural like raw queries)
98
  # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
99
+ clean_html = re.sub(r'\s+', ' ', html)
100
+ search_regex = re.compile(rf'>@{word}<.*?class="[^"]*status[^"]*"[^>]*>\s*([^<]+?)\s*TL', re.IGNORECASE)
101
+ match = search_regex.search(clean_html)
102
+
103
+ if match:
104
+ s = match.group(1).strip().lower()
105
+ if "taken" in s: return "TAKEN"
106
+ if "available" in s: return "AVAILABLE"
107
+ if "sold" in s: return "SOLD"
108
+ if "auction" in s: return "ON_AUCTION"
109
+ if "unavailable" in s: return "UNAVAILABLE"
110
+ return s.upper()
 
 
 
 
 
 
 
111
 
112
  except Exception as e:
113
+ log.error(f"Execution fault for '{word}' over proxy: {str(e)}")
114
+ await asyncio.sleep(1.5 * attempt)
115
+
116
+ return "ERROR"