Spaces:
Sleeping
Sleeping
Update app.py
Browse files24 unmodified lines252627282930617 unmodified lines6486496506516526536546 unmodified lines6616626636646656666676686696706716722 unmodified lines67567667767867968068168268368468568668768868969069169254 unmodified lines74774874975075175275375475524 unmodified linesplaywright = Nonebrowser = Noneapp.add_middleware(CORSMiddleware,allow_origins=["*"],617 unmodified linesif browser is None:return {"scrape_success": False, "error": "Browser not initialized"}# Unstop: try API firstif platform == "Unstop":opp_id = extract_unstop_id(url)print(f"[Unstop] Extracted ID: {opp_id}")6 unmodified linesreturn resultprint("[Unstop] API failed, falling back to Playwright")context = await browser.new_context(user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",viewport={"width": 1920, "height": 1080},)try:page = await context.new_page()print(f"[Scraper] => {url} platform={platform}")wait_until = "load" if platform in ("Devfolio", "MLH") else "domcontentloaded"try:2 unmodified linesif "Timeout" not in str(e): raiseprint("[Scraper] goto timeout, proceeding anyway")wait_map = {"Unstop": 9, "DoraHacks": 8, "Devfolio": 8, "MLH": 4}wait_sec = wait_map.get(platform, 5)print(f"[Scraper] Waiting {wait_sec}s for JS...")await page.wait_for_timeout(wait_sec * 1000)for frac in [0.33, 0.66, 1.0, 0.0]:try:await page.evaluate(f"window.scrollTo(0, document.body.scrollHeight * {frac})")except Exception:passawait asyncio.sleep(0.6)await asyncio.sleep(1.0)if platform == "Devfolio":raw = await safe_evaluate(page, DEVFOLIO_SCRIPT, GENERIC_SCRIPT)54 unmodified linesplaywright = await async_playwright().start()browser = await playwright.chromium.launch(headless=True,args=["--no-sandbox", "--disable-setuid-sandbox", "--disable-dev-shm-usage"],)print("[Scraper] v5.0 ready - structured extraction (no LLM)")@app.on_event("shutdown")24 unmodified lines25262728293031323334617 unmodified lines6526536546556566576586 unmodified lines6656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107112 unmodified lines71471571671771871972072172272372472572672772872973073173273354 unmodified lines78878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083124 unmodified linesplaywright = Nonebrowser = None# Semaphore: only 1 Playwright scrape at a time on HuggingFace free tier.# Prevents two concurrent requests from doubling RAM usage (~1.2GB peak)._scrape_sem = asyncio.Semaphore(1)app.add_middleware(CORSMiddleware,allow_origins=["*"],617 unmodified linesif browser is None:return {"scrape_success": False, "error": "Browser not initialized"}# Unstop: try API first β no Playwright needed, saves all memory for this callif platform == "Unstop":opp_id = extract_unstop_id(url)print(f"[Unstop] Extracted ID: {opp_id}")6 unmodified linesreturn resultprint("[Unstop] API failed, falling back to Playwright")async with _scrape_sem:# Only one Playwright scrape runs at a time to stay within HuggingFace RAM limits.# Concurrent requests queue here and are processed sequentially.print(f"[Scraper] Semaphore acquired for {platform}")return await _do_playwright_scrape(url, platform)async def _do_playwright_scrape(url: str, platform: str) -> dict:"""Inner function β runs inside the semaphore."""global browsercontext = await browser.new_context(user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",# Smaller viewport = less GPU memory for compositingviewport={"width": 1280, "height": 800},# Block credentials/service workers to reduce overheadjava_script_enabled=True,bypass_csp=False,)try:page = await context.new_page()# Block images, fonts, media, and tracking β saves 30-60% of page RAM# We only need DOM text and __NEXT_DATA__, not rendered assetsasync def block_resources(route, request):BLOCK_TYPES = {"image", "media", "font", "stylesheet", "other","ping", "websocket"}BLOCK_DOMAINS = {"google-analytics", "googletagmanager", "facebook","hotjar", "intercom", "amplitude", "segment","cloudflare.com/beacon", "sentry.io"}if request.resource_type in BLOCK_TYPES:await route.abort()returnurl_lower = request.url.lower()if any(d in url_lower for d in BLOCK_DOMAINS):await route.abort()returnawait route.continue_()await page.route("**/*", block_resources)print(f"[Scraper] => {url} platform={platform}")wait_until = "load" if platform in ("Devfolio", "MLH") else "domcontentloaded"try:2 unmodified linesif "Timeout" not in str(e): raiseprint("[Scraper] goto timeout, proceeding anyway")# Reduced wait times β blocking assets means pages settle fasterwait_map = {"Unstop": 6, "DoraHacks": 6, "Devfolio": 5, "MLH": 3}wait_sec = wait_map.get(platform, 4)print(f"[Scraper] Waiting {wait_sec}s for JS...")await page.wait_for_timeout(wait_sec * 1000)# Light scroll only β no heavy scroll since images are blocked anywayfor frac in [0.5, 1.0, 0.0]:try:await page.evaluate(f"window.scrollTo(0, document.body.scrollHeight * {frac})")except Exception:passawait asyncio.sleep(0.4)await asyncio.sleep(0.5)if platform == "Devfolio":raw = await safe_evaluate(page, DEVFOLIO_SCRIPT, GENERIC_SCRIPT)54 unmodified linesplaywright = await async_playwright().start()browser = await playwright.chromium.launch(headless=True,args=[# ββ Security (required for containers) ββββββββββββββββββββββββββ"--no-sandbox","--disable-setuid-sandbox",# ββ Memory reduction βββββββββββββββββββββββββββββββββββββββββββββ"--disable-dev-shm-usage", # use /tmp instead of /dev/shm"--disable-gpu", # no GPU process (~50MB saved)"--no-zygote", # skip zygote process fork"--single-process", # single process mode (~150MB saved)"--disable-extensions", # no extension processes"--disable-background-networking","--disable-background-timer-throttling","--disable-backgrounding-occluded-windows","--disable-breakpad", # no crash reporter"--disable-client-side-phishing-detection","--disable-component-update","--disable-default-apps","--disable-domain-reliability","--disable-features=AudioServiceOutOfProcess,IsolateOrigins,site-per-process","--disable-hang-monitor","--disable-ipc-flooding-protection","--disable-popup-blocking","--disable-prompt-on-repost","--disable-renderer-backgrounding","--disable-sync","--disable-translate","--metrics-recording-only","--mute-audio","--no-first-run","--safebrowsing-disable-auto-update","--password-store=basic","--use-mock-keychain",# ββ Reduce per-page memory ββββββββββββββββββββββββββββββββββββββββ"--js-flags=--max-old-space-size=256", # cap JS heap to 256MB"--renderer-process-limit=2",],)print("[Scraper] v5.0 ready - memory-optimised Chromium on HuggingFace")@app.on_event("shutdown")
app.py+85-9