# -*- coding: utf-8 -*-
import openpyxl,sys,urllib.request,ssl,re,bs4,time
sys.stdout.reconfigure(encoding='utf-8')
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
def fetch(u,tries=3):
    for i in range(tries):
        try:
            req=urllib.request.Request(u,headers={'User-Agent':'Mozilla/5.0'})
            return urllib.request.urlopen(req,timeout=20,context=ctx).read()
        except Exception as e:
            if i==tries-1: raise
            time.sleep(2)
def maxnum(soup):
    nums=[]
    for el in soup.select('.b-left-n, td.b-left-n, th.b-left-n'):
        t=el.get_text(strip=True)
        if t.isdigit(): nums.append(int(t))
    return max(nums) if nums else None
wb=openpyxl.load_workbook('한국청소년활동진흥원.xlsx'); ws=wb.active
out=[]
for r in range(3,ws.max_row+1):
    url=ws.cell(r,11).value
    curL=ws.cell(r,12).value
    if not url or not str(url).startswith('http'):
        out.append(f'{r}\tEMPTY/NOURL'); continue
    if 'kywa.or.kr' not in url or url.startswith('https://booking'):
        out.append(f'{r}\tEXT\t{url}'); continue
    try:
        h=fetch(url); soup=bs4.BeautifulSoup(h,'html.parser')
        tc=[(a.get_text(strip=True),a.get('href','')) for a in soup.select('ul.tab-clear li a.tap-setting, ul.tab-clear li a')]
        has_all=any(t[0]=='전체' for t in tc)
        is_content_tab = tc and not has_all and len(tc)>=2 and all('.jsp' in (t[1] or '') for t in tc)
        mx=maxnum(soup)
        gp=[int(x) for x in re.findall(r'go_page\((\d+)\)',h.decode('utf-8','ignore'))]
        lastpg=max(gp) if gp else None
        board = (mx is not None) or has_all or (lastpg and lastpg>1)
        kind='CONTENT_TAB' if is_content_tab else ('CAT_BOARD' if (has_all or board) else 'PAGE')
        info=f'{r}\t{kind}\tcurL={curL}\tM(maxnum)={mx}\tlastpg={lastpg}\ttabs={len(tc)}'
        if is_content_tab: info+='\tCONTENT:'+' | '.join(f'{t[0]}->{t[1]}' for t in tc)
        elif tc: info+='\tCATS:'+'/'.join(t[0] for t in tc)
        out.append(info+'\t'+url.split('/')[-1][:30])
    except Exception as e:
        out.append(f'{r}\tERR\t{str(e)[:40]}\t{url[:50]}')
open('_scan_out.txt','w',encoding='utf-8').write('\n'.join(out))
print('\n'.join(out))
