Spaces:
Paused
Paused
Update scraper.py
Browse files- scraper.py +40 -32
scraper.py
CHANGED
|
@@ -1,54 +1,62 @@
|
|
| 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 |
-
#
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
try:
|
| 13 |
-
|
| 14 |
-
|
| 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 |
-
#
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
if canonical and "/username/" not in canonical.group(1):
|
| 26 |
return "UNAVAILABLE"
|
| 27 |
-
|
| 28 |
-
# 2.
|
| 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 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
if "
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
except Exception:
|
| 51 |
await asyncio.sleep(1)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
return "ERROR"
|
|
|
|
| 1 |
import asyncio
|
| 2 |
import random
|
| 3 |
import re
|
| 4 |
+
import aiofiles
|
| 5 |
from curl_cffi.requests import AsyncSession
|
| 6 |
|
| 7 |
+
async def check_fragment(word: str, proxy_url: str = None) -> str:
|
| 8 |
url = f"https://fragment.com/username/{word}"
|
| 9 |
|
| 10 |
+
# Format proxy for curl_cffi (Applies to both HTTP and HTTPS traffic)
|
| 11 |
+
proxies = {"http": proxy_url, "https": proxy_url} if proxy_url else None
|
| 12 |
+
|
| 13 |
+
async with AsyncSession(impersonate="chrome120", proxies=proxies) as session:
|
| 14 |
+
for attempt in range(4):
|
| 15 |
try:
|
| 16 |
+
resp = await session.get(url, timeout=15, allow_redirects=True)
|
| 17 |
+
|
|
|
|
| 18 |
if resp.status_code in [429, 403]:
|
| 19 |
await asyncio.sleep(random.uniform(1.5, 3.5))
|
| 20 |
continue
|
| 21 |
+
|
| 22 |
html = resp.text
|
| 23 |
+
|
| 24 |
+
# Cloudflare challenge - rotate proxy or wait
|
| 25 |
+
if "Just a moment..." in html or "cloudflare" in html.lower():
|
| 26 |
+
await asyncio.sleep(random.uniform(2, 4))
|
| 27 |
+
continue
|
| 28 |
+
|
| 29 |
+
# 1. Canonical Rule: If redirected to homepage, username is soft-banned/Unavailable
|
| 30 |
+
canonical = re.search(r'<link rel="canonical" href="([^"]+)"', html, re.IGNORECASE)
|
| 31 |
if canonical and "/username/" not in canonical.group(1):
|
| 32 |
return "UNAVAILABLE"
|
| 33 |
+
|
| 34 |
+
# 2. Extract specific status text
|
| 35 |
+
status_match = re.search(r'class="tm-section-header-status[^"]*"[^>]*>\s*([^<]+?)\s*</span>', html, re.IGNORECASE)
|
| 36 |
+
|
| 37 |
if status_match:
|
| 38 |
s = status_match.group(1).strip().lower()
|
| 39 |
if "sold" in s: return "SOLD"
|
| 40 |
if "taken" in s: return "TAKEN"
|
| 41 |
if "auction" in s: return "ON_AUCTION"
|
| 42 |
if "available" in s: return "AVAILABLE"
|
| 43 |
+
if "sale" in s or "purchase" in s: return "FOR_SALE"
|
| 44 |
+
|
| 45 |
+
# Async I/O for unknown statuses
|
| 46 |
+
clean = s.upper()
|
| 47 |
+
async with aiofiles.open("other.txt", "a", encoding="utf-8") as f:
|
| 48 |
+
await f.write(f"{word} - {clean} (Proxy: {proxy_url})\n")
|
| 49 |
+
return clean
|
| 50 |
+
|
| 51 |
+
# 3. Fallbacks
|
| 52 |
+
if "tm-status-taken" in html: return "TAKEN"
|
| 53 |
+
if "tm-status-unavail" in html:
|
| 54 |
+
# Prevent false Unavailable markings for Sold pages
|
| 55 |
+
if ">Sold<" in html or ">sold<" in html.lower():
|
| 56 |
+
return "SOLD"
|
| 57 |
+
return "UNAVAILABLE"
|
| 58 |
+
|
| 59 |
except Exception:
|
| 60 |
await asyncio.sleep(1)
|
| 61 |
+
|
| 62 |
+
return "ERROR"
|
|
|