Spaces:
Paused
Paused
Update scraper.py
Browse files- scraper.py +36 -39
scraper.py
CHANGED
|
@@ -1,54 +1,51 @@
|
|
| 1 |
import asyncio
|
| 2 |
import random
|
| 3 |
-
import aiohttp
|
| 4 |
import re
|
| 5 |
-
|
| 6 |
-
USER_AGENTS = [
|
| 7 |
-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0 Safari/537.36",
|
| 8 |
-
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 Safari/605.1.15",
|
| 9 |
-
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/123.0 Safari/537.36",
|
| 10 |
-
]
|
| 11 |
|
| 12 |
async def check_fragment(word: str) -> str:
|
| 13 |
url = f"https://fragment.com/username/{word}"
|
| 14 |
|
| 15 |
-
|
|
|
|
| 16 |
for _ in range(4):
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# 2. EXACT STATUS EXTRACTION (From your uploaded HTML tags)
|
| 35 |
-
status_match = re.search(r'class="tm-section-header-status[^>]*>([^<]+)</span>', html, re.IGNORECASE)
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
if "available" in s: return "AVAILABLE"
|
| 43 |
-
|
| 44 |
-
# 3. LOG UNKNOWN STATUSES
|
| 45 |
-
clean_status = s.upper()
|
| 46 |
-
with open("other.txt", "a", encoding="utf-8") as f:
|
| 47 |
-
f.write(f"{word} - {clean_status}\n")
|
| 48 |
-
return clean_status
|
| 49 |
-
|
| 50 |
-
# 4. Final Fallback
|
| 51 |
-
if "tm-status-unavail" in html: return "UNAVAILABLE"
|
| 52 |
|
| 53 |
except Exception:
|
| 54 |
await asyncio.sleep(1)
|
|
|
|
| 1 |
import asyncio
|
| 2 |
import random
|
|
|
|
| 3 |
import re
|
| 4 |
+
from curl_cffi.requests import AsyncSession
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
async def check_fragment(word: str) -> str:
|
| 7 |
url = f"https://fragment.com/username/{word}"
|
| 8 |
|
| 9 |
+
# Impersonate Google Chrome so Fragment stops shadow-banning/redirecting you
|
| 10 |
+
async with AsyncSession(impersonate="chrome120") as session:
|
| 11 |
for _ in range(4):
|
| 12 |
try:
|
| 13 |
+
# We allow redirects so we can catch Fragment's final destination URL
|
| 14 |
+
resp = await session.get(url, timeout=12, allow_redirects=True)
|
| 15 |
+
|
| 16 |
+
if resp.status_code in [429, 403]:
|
| 17 |
+
await asyncio.sleep(random.uniform(1.5, 3.5))
|
| 18 |
+
continue
|
| 19 |
+
|
| 20 |
+
html = resp.text
|
| 21 |
+
|
| 22 |
+
# 1. THE CANONICAL RULE (100% accurate Soft-404 detector based on your HTML)
|
| 23 |
+
# If Fragment tries to shadow-ban us to the homepage, this catches it instantly.
|
| 24 |
+
canonical = re.search(r'<link rel="canonical" href="([^"]+)"', html)
|
| 25 |
+
if canonical and "/username/" not in canonical.group(1):
|
| 26 |
+
return "UNAVAILABLE"
|
| 27 |
|
| 28 |
+
# 2. EXACT STATUS EXTRACTION (From your uploaded HTML tags)
|
| 29 |
+
status_match = re.search(r'class="tm-section-header-status[^>]*>([^<]+)</span>', html, re.IGNORECASE)
|
| 30 |
+
|
| 31 |
+
if status_match:
|
| 32 |
+
s = status_match.group(1).strip().lower()
|
| 33 |
+
if "sold" in s: return "SOLD"
|
| 34 |
+
if "taken" in s: return "TAKEN"
|
| 35 |
+
if "auction" in s: return "ON_AUCTION"
|
| 36 |
+
if "available" in s: return "AVAILABLE"
|
| 37 |
|
| 38 |
+
# 3. LOG UNKNOWN STATUSES TO other.txt
|
| 39 |
+
clean_status = s.upper()
|
| 40 |
+
with open("other.txt", "a", encoding="utf-8") as f:
|
| 41 |
+
f.write(f"{word} - {clean_status}\n")
|
| 42 |
+
return clean_status
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# 4. Final Fallbacks
|
| 45 |
+
if "tm-status-unavail" in html: return "UNAVAILABLE"
|
| 46 |
+
if "Just a moment..." in html or "cloudflare" in html.lower():
|
| 47 |
+
await asyncio.sleep(random.uniform(2, 4))
|
| 48 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
except Exception:
|
| 51 |
await asyncio.sleep(1)
|