# -*- coding: utf-8 -*-
import urllib.request, ssl, re, io
from bs4 import BeautifulSoup
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
def get(u):
    req=urllib.request.Request(u,headers={"User-Agent":"Mozilla/5.0"})
    return urllib.request.urlopen(req,context=ctx,timeout=20).read()
def probe(u):
    raw=get(u); soup=BeautifulSoup(raw,"html.parser")
    # try content containers
    cont=None
    for sel in ["#contents","#content",".contents",".content",".sub_content","#container",".lay_content","#subContents"]:
        el=soup.select_one(sel)
        if el and len(el.get_text(strip=True))>20: cont=el; sel_used=sel; break
    if cont is None: cont=soup.body; sel_used="body"
    # videos
    vids=[]
    for v in cont.find_all("video"): vids.append("video-tag")
    for ifr in cont.find_all("iframe"):
        s=ifr.get("src","") or ""
        if any(k in s for k in ["youtube","youtu.be","vimeo","player"]): vids.append("iframe:"+s[:50])
    # imgs (content)
    imgs=[]
    for im in cont.find_all("img"):
        s=im.get("src","") or im.get("data-src","") or ""
        w=im.get("width");h=im.get("height")
        imgs.append("%s (w=%s h=%s)"%(s.split("/")[-1][:40],w,h))
    return sel_used,len(cont.get_text(strip=True)),vids,imgs
tests=[
 ("r3 상담안내/어문","https://www.k-medi.or.kr/web/lay1/S1T8C17/contents.do"),
 ("r9 조정신청방법/어문영상","https://www.k-medi.or.kr/web/lay1/S1T132C158/contents.do"),
 ("r13 조정신청서식/어문영상","https://www.k-medi.or.kr/web/lay1/S1T133C878/contents.do"),
 ("r22 안내서/어문이미지","https://www.k-medi.or.kr/web/lay1/bbs/S1T27C1148/A/26/view.do"),
 ("r19 흐름도/어문","https://www.k-medi.or.kr/web/lay1/S1T228C230/contents.do"),
]
o=io.open("_probe_out.txt","w",encoding="utf-8")
for nm,u in tests:
    try:
        sel,tl,vids,imgs=probe(u)
        o.write("### %s\n  sel=%s textlen=%d\n  VIDEOS(%d): %s\n  IMGS(%d):\n"%(nm,sel,tl,len(vids),vids,len(imgs)))
        for x in imgs: o.write("     %s\n"%x)
        o.write("\n")
    except Exception as e:
        o.write("### %s ERR %s\n\n"%(nm,e))
o.close();print("done")
