# -*- coding: utf-8 -*-
from playwright.sync_api import sync_playwright
targets={
 'r222_홍보영상':'https://www.komipo.co.kr/kor/content/126/main.do?mnCd=FN0902',
 'r40_정보보호':'https://www.komipo.co.kr/kor/content/446/main.do?mnCd=FN0107',
 'r16_489':'https://www.komipo.co.kr/kor/content/489/main.do',
 'r84_24':'https://www.komipo.co.kr/kor/content/24/main.do',
 'r221_135':'https://www.komipo.co.kr/kor/content/135/main.do',
}
import os
os.makedirs('_render',exist_ok=True)
with sync_playwright() as p:
    b=p.chromium.launch()
    pg=b.new_page(viewport={'width':1100,'height':1500},ignore_https_errors=True)
    for n,u in targets.items():
        try:
            pg.goto(u,timeout=30000,wait_until='networkidle')
            pg.wait_for_timeout(1500)
            # main content area: try several
            el=pg.query_selector('#content')or pg.query_selector('.contents')or pg.query_selector('#container')or pg.query_selector('main')
            (el or pg).screenshot(path=f'_render/{n}.png')
            vids=pg.eval_on_selector_all('video, iframe',
                'els=>els.filter(e=>{s=(e.src||e.currentSrc||"");return /youtube|vimeo|\\.mp4|player/.test(s)}).map(e=>e.src||e.currentSrc)')
            bigimg=pg.eval_on_selector_all('img','els=>els.filter(i=>i.naturalWidth>=250&&i.naturalHeight>=170).map(i=>i.alt+"|"+i.src.split("/").pop()).slice(0,8)')
            txt=(pg.inner_text('body')or'')[:160].replace('\n',' ')
            print('==',n,'==')
            print('  vids:',vids[:4])
            print('  bigimg:',bigimg)
            print('  txt:',txt)
        except Exception as e:
            print(n,'ERR',str(e)[:70])
    b.close()
