Spaces:
Paused
Paused
Update scraper.py
Browse files- scraper.py +16 -16
scraper.py
CHANGED
|
@@ -17,28 +17,27 @@ async def check_fragment(word: str) -> str:
|
|
| 17 |
for attempt in range(3):
|
| 18 |
headers = {"User-Agent": random.choice(USER_AGENTS)}
|
| 19 |
try:
|
| 20 |
-
#
|
| 21 |
-
async with session.get(url, headers=headers, allow_redirects=
|
| 22 |
|
| 23 |
if resp.status == 429:
|
| 24 |
wait = (2 ** attempt * 3) + random.uniform(1, 3)
|
| 25 |
await asyncio.sleep(wait)
|
| 26 |
continue
|
| 27 |
|
| 28 |
-
# FAST-FAIL REDIRECT CHECK:
|
| 29 |
-
# If Fragment issues a 302 redirect to the search page, the word is UNAVAILABLE.
|
| 30 |
-
# This skips downloading the HTML entirely, making the bot blazing fast.
|
| 31 |
-
if resp.status in [301, 302, 303, 307]:
|
| 32 |
-
location = resp.headers.get("Location", "")
|
| 33 |
-
if "/?query=" in location:
|
| 34 |
-
return "UNAVAILABLE"
|
| 35 |
-
continue # If it's a weird unknown redirect, retry
|
| 36 |
-
|
| 37 |
-
# If the page actually loads (Status 200)
|
| 38 |
if resp.status == 200:
|
| 39 |
html = await resp.text()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
# Extract the exact status from Fragment's HTML header
|
| 42 |
match = re.search(r'<span class="tm-section-header-status[^>]*>(.*?)</span>', html, re.IGNORECASE)
|
| 43 |
|
| 44 |
if match:
|
|
@@ -49,14 +48,15 @@ async def check_fragment(word: str) -> str:
|
|
| 49 |
if status_text == "on auction": return "ON_AUCTION"
|
| 50 |
if status_text == "available": return "AVAILABLE"
|
| 51 |
|
| 52 |
-
#
|
| 53 |
clean_status = status_text.upper()
|
| 54 |
with open("other.txt", "a", encoding="utf-8") as f:
|
| 55 |
f.write(f"{word} - {clean_status}\n")
|
| 56 |
return clean_status
|
| 57 |
|
| 58 |
-
#
|
| 59 |
if "tm-status-unavail" in html: return "UNAVAILABLE"
|
|
|
|
| 60 |
|
| 61 |
return "UNAVAILABLE"
|
| 62 |
|
|
@@ -65,4 +65,4 @@ async def check_fragment(word: str) -> str:
|
|
| 65 |
|
| 66 |
await asyncio.sleep(random.uniform(0.8, 2.0))
|
| 67 |
|
| 68 |
-
return "ERROR"
|
|
|
|
| 17 |
for attempt in range(3):
|
| 18 |
headers = {"User-Agent": random.choice(USER_AGENTS)}
|
| 19 |
try:
|
| 20 |
+
# We allow redirects so we can catch Fragment's final destination URL
|
| 21 |
+
async with session.get(url, headers=headers, allow_redirects=True, timeout=15) as resp:
|
| 22 |
|
| 23 |
if resp.status == 429:
|
| 24 |
wait = (2 ** attempt * 3) + random.uniform(1, 3)
|
| 25 |
await asyncio.sleep(wait)
|
| 26 |
continue
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
if resp.status == 200:
|
| 29 |
html = await resp.text()
|
| 30 |
+
final_url = str(resp.url)
|
| 31 |
+
|
| 32 |
+
# 1. THE SOFT-REDIRECT DETECTOR (Based on your uploaded HTML)
|
| 33 |
+
# If Fragment quietly drops us on the homepage, it means the word is Unavailable.
|
| 34 |
+
if final_url == "https://fragment.com/" or "/?query=" in final_url:
|
| 35 |
+
return "UNAVAILABLE"
|
| 36 |
+
|
| 37 |
+
if "<title>Fragment</title>" in html or 'href="https://fragment.com/"' in html:
|
| 38 |
+
return "UNAVAILABLE"
|
| 39 |
|
| 40 |
+
# 2. Extract the exact status from Fragment's HTML header
|
| 41 |
match = re.search(r'<span class="tm-section-header-status[^>]*>(.*?)</span>', html, re.IGNORECASE)
|
| 42 |
|
| 43 |
if match:
|
|
|
|
| 48 |
if status_text == "on auction": return "ON_AUCTION"
|
| 49 |
if status_text == "available": return "AVAILABLE"
|
| 50 |
|
| 51 |
+
# 3. Unknown status logging
|
| 52 |
clean_status = status_text.upper()
|
| 53 |
with open("other.txt", "a", encoding="utf-8") as f:
|
| 54 |
f.write(f"{word} - {clean_status}\n")
|
| 55 |
return clean_status
|
| 56 |
|
| 57 |
+
# 4. Final Fail-safes
|
| 58 |
if "tm-status-unavail" in html: return "UNAVAILABLE"
|
| 59 |
+
if "This username is unavailable" in html: return "UNAVAILABLE"
|
| 60 |
|
| 61 |
return "UNAVAILABLE"
|
| 62 |
|
|
|
|
| 65 |
|
| 66 |
await asyncio.sleep(random.uniform(0.8, 2.0))
|
| 67 |
|
| 68 |
+
return "ERROR"
|