# -*- coding: utf-8 -*-
import sys,io
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
from playwright.sync_api import sync_playwright
C={
 'r8_CI':"https://www.nypi.re.kr/board?menuKey=36AnOd5WEI&bbsId=BOARD00043",
 'r17_정기간행물':"https://www.nypi.re.kr/board?menuKey=a2cArE1kDq&bbsId=BOARD00044",
 'r31_리플릿':"https://www.nypi.re.kr/board?menuKey=KHNEsLyaI5&bbsId=BOARD00013",
 'r18_발간물안내':"https://www.nypi.re.kr/lib/10450/custom/1063002/academic",
 'r19_데이터아카이브':"https://www.nypi.re.kr/archive/",
}
with sync_playwright() as p:
    br=p.chromium.launch(headless=True); ctx=br.new_context(ignore_https_errors=True,viewport={'width':1280,'height':2000})
    for nm,u in C.items():
        pg=ctx.new_page()
        try:
            pg.goto(u,wait_until='networkidle',timeout=35000); pg.wait_for_timeout(1500)
            # 본문 콘텐츠 이미지(원본>=200x150, 장식파일명 제외)
            r=pg.evaluate(r"""()=>{
              const dec=/logo|icon|btn|mark_|webwatch|eprivacy|sns|footer|header|banner|bg_|sprite|blank|no_img|arrow|family/i;
              let cont=document.querySelector('#contents,.contents,#content,.board_view,.view_content,.sub_content')||document.body;
              let imgs=[...cont.querySelectorAll('img')].filter(im=>im.naturalWidth>=200&&im.naturalHeight>=150&&!dec.test(im.src)).map(im=>im.src.split('/').pop().slice(0,30)+`(${im.naturalWidth}x${im.naturalHeight})`);
              let vids=[...cont.querySelectorAll('video,iframe[src*=\"youtube\"]')].length;
              let host=location.host;
              return {host,n_img:imgs.length,imgs:imgs.slice(0,6),vids, title:document.title.slice(0,40)};
            }""")
            print(nm,'::',r)
        except Exception as e:
            print(nm,'ERR',str(e)[:50])
        pg.close()
    br.close()
