Datasets:
File size: 2,423 Bytes
1e6582e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | """Second pass over the same pages for more variety: two further scroll positions in light mode and one scrolled dark capture."""
import asyncio, hashlib, json, sys
from pathlib import Path
from playwright.async_api import async_playwright
import build
RAW=build.RAW; H=build.H
async def one(browser,url,sem,log):
uid=hashlib.sha1(url.encode()).hexdigest()[:10]; done=RAW/f"{uid}.more"
first=RAW/f"{uid}.done"
if done.exists() or not first.exists() or "FAILED" in first.read_text() or not (RAW/f"{uid}_scroll.json").exists(): return
async with sem:
ctx=await browser.new_context(viewport={"width":build.W,"height":H},device_scale_factor=1,color_scheme="light",locale="en-GB",reduced_motion="reduce")
page=await ctx.new_page(); note=""
try:
await page.goto(url,wait_until="domcontentloaded",timeout=40000)
try: await page.wait_for_load_state("networkidle",timeout=8000)
except Exception: pass
try: await page.add_style_tag(content=build.FREEZE)
except Exception:
cdp=await ctx.new_cdp_session(page); await cdp.send("Animation.enable"); await cdp.send("Animation.setPlaybackRate",{"playbackRate":0})
height=await page.evaluate("document.documentElement.scrollHeight"); n=[]
for k,name in ((2,"scroll2"),(3,"scroll3")):
if height>H*(k+0.6):
await page.evaluate(f"window.scrollTo(0,{H*k})"); await page.wait_for_timeout(900); n.append((await build.shot(page,uid,name,url))["n"])
await page.emulate_media(color_scheme="dark",reduced_motion="reduce"); await page.evaluate(f"window.scrollTo(0,{H})"); await page.wait_for_timeout(900)
n.append((await build.shot(page,uid,"darkscroll",url))["n"]); note=f"labels {n}"
except Exception as e: note=f"FAILED {type(e).__name__}: {str(e)[:80]}"
finally:
done.write_text(note); log(f"{uid} {url[:60]:60s} {note}"); await ctx.close()
async def main():
urls=[u.strip() for u in Path(sys.argv[1]).read_text().splitlines() if u.strip()]; sem=asyncio.Semaphore(8); c=[0]
def log(m): c[0]+=1; print(f"[{c[0]}] {m}",flush=True)
async with async_playwright() as p:
b=await p.chromium.launch(channel="chrome",headless=True)
await asyncio.gather(*[one(b,u,sem,log) for u in urls],return_exceptions=True); await b.close()
asyncio.run(main())
|