Spaces:
Sleeping
Sleeping
| import json | |
| import requests | |
| from bs4 import BeautifulSoup | |
| from .config_loader import load_app_config | |
| from .models import ProfileSnapshot | |
| APP_CONFIG = load_app_config() | |
| REQUESTS_CONFIG = APP_CONFIG["requests"] | |
| HEADERS = {"User-Agent": REQUESTS_CONFIG["user_agent"]} | |
| TIMEOUT = REQUESTS_CONFIG["timeout_seconds"] | |
| class ProfileNotFound(Exception): | |
| pass | |
| def fetch_profile(url: str) -> ProfileSnapshot: | |
| resp = requests.get(url, headers=HEADERS, timeout=TIMEOUT) | |
| if resp.status_code == 404: | |
| raise ProfileNotFound("HTTP 404") | |
| soup = BeautifulSoup(resp.text, "html.parser") | |
| app = soup.find("div", id="app") | |
| if not app or not app.has_attr("data-page"): | |
| raise RuntimeError("data-page JSON not found") | |
| data = json.loads(app["data-page"]) | |
| if data.get("component") == "Error": | |
| status = data.get("props", {}).get("status") | |
| if status == 404: | |
| raise ProfileNotFound("Profile not found (app-level 404)") | |
| creator = data["props"]["creator_data"]["data"] | |
| return ProfileSnapshot( | |
| bmc_url=url, | |
| name=creator.get("name"), | |
| slug=creator.get("slug"), | |
| bio=creator.get("description"), | |
| external_links=creator.get("links", []), | |
| has_nsfw=creator.get("has_nsfw"), | |
| payment_method=creator.get("payment_method"), | |
| ) | |