# -*- coding: utf-8 -*-
from playwright.sync_api import sync_playwright
targets={
 'r166_기후변화':'https://www.komipo.co.kr/esg/content/102/main.do?mnCd=ESG020201',
 'r179_인권위원회':'https://www.komipo.co.kr/esg/content/393/main.do?mnCd=ESG030301',
 'r222_홍보영상':'https://www.komipo.co.kr/kor/content/126/main.do?mnCd=FN0902',
 'r223_KOMIPOMEDIA':'https://www.komipo.co.kr/kor/content/215/main.do?mnCd=FN090202',
 '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':1280,'height':1400},ignore_https_errors=True)
    for n,u in targets.items():
        try:
            pg.goto(u,timeout=30000,wait_until='networkidle')
            pg.wait_for_timeout(1500)
            el=pg.query_selector('#content') or pg.query_selector('div.contents')
            info={}
            if el:
                el.screenshot(path=f'_render/{n}.png')
            # detect media
            vids=pg.eval_on_selector_all('#content video, #content iframe',
                'els=>els.map(e=>({t:e.tagName,src:e.src||e.currentSrc||""}))')
            imgs=pg.eval_on_selector_all('#content img',
                'els=>els.filter(i=>i.naturalWidth>=200&&i.naturalHeight>=150).map(i=>({w:i.naturalWidth,h:i.naturalHeight,alt:i.alt,src:i.src.split("/").pop()}))')
            print(n,'| media:',vids,'| bigimgs:',len(imgs))
            for im in imgs[:6]: print('    ',im['w'],'x',im['h'],im['alt'][:40],im['src'][:30])
        except Exception as e:
            print(n,'ERR',str(e)[:60])
    b.close()
