Spaces:
Paused
Paused
Update scraper.py
Browse files- scraper.py +49 -29
scraper.py
CHANGED
|
@@ -18,52 +18,72 @@ async def check_fragment(word: str, proxy_url: str = None) -> str:
|
|
| 18 |
|
| 19 |
for attempt in range(3):
|
| 20 |
try:
|
| 21 |
-
#
|
|
|
|
|
|
|
|
|
|
| 22 |
timeout = aiohttp.ClientTimeout(total=15)
|
| 23 |
async with aiohttp.ClientSession(timeout=timeout) as session:
|
| 24 |
async with session.get(url, headers=headers, proxy=proxy_url, allow_redirects=True) as resp:
|
| 25 |
|
| 26 |
if resp.status in [429, 403]:
|
| 27 |
-
|
|
|
|
| 28 |
continue
|
| 29 |
|
| 30 |
html = await resp.text()
|
| 31 |
|
| 32 |
-
# Cloudflare block
|
| 33 |
if "Just a moment..." in html or "cloudflare" in html.lower():
|
| 34 |
-
await asyncio.sleep(random.uniform(
|
| 35 |
continue
|
| 36 |
|
| 37 |
-
|
| 38 |
-
canonical = re.search(r'<link rel="canonical" href="([^"]+)"', html, re.IGNORECASE)
|
| 39 |
-
if canonical and "fragment.com/username/" not in canonical.group(1):
|
| 40 |
-
return "UNAVAILABLE"
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
if
|
| 46 |
-
|
| 47 |
-
if
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
return "SOLD"
|
| 60 |
-
return "UNAVAILABLE"
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
-
# This logs the specific aiohttp timeout/connection error so you can see if a proxy dies
|
| 66 |
log.error(f"Request error for {word} via {proxy_url}: {str(e)}")
|
| 67 |
-
await asyncio.sleep(
|
| 68 |
|
| 69 |
return "ERROR"
|
|
|
|
| 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"
|