# -*- coding: utf-8 -*-
import openpyxl, time, json, re, sys
import urllib.request, ssl
from bs4 import BeautifulSoup

ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
HDR={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124 Safari/537.36'}

def fetch(url):
    req=urllib.request.Request(url, headers=HDR)
    with urllib.request.urlopen(req, timeout=20, context=ctx) as r:
        return r.read()

wb=openpyxl.load_workbook('한국중부발전.xlsx'); ws=wb.active
# collect candidate rows: B순번>=41, URL is komipo content page
cands=[]
for r in range(3, ws.max_row+1):
    b=ws.cell(r,2).value
    if not isinstance(b,int) or b<41: continue
    k=ws.cell(r,11).value; L=ws.cell(r,12).value
    if not k or 'komipo.co.kr' not in str(k): continue
    if '/content/' not in str(k) and 'main.do?mnCd' not in str(k): continue
    cands.append((r,b,L,ws.cell(r,6).value or ws.cell(r,5).value or ws.cell(r,4).value, str(k)))
print('후보',len(cands),'행', file=sys.stderr)
found={}
for i,(r,b,L,leaf,k) in enumerate(cands):
    try:
        html=fetch(k)
        soup=BeautifulSoup(html,'html.parser')
        # find ul with class containing tab_link
        uls=soup.find_all('ul', class_=lambda c: c and 'tab_link' in ' '.join(c if isinstance(c,list) else [c]))
        for ul in uls:
            tabs=[]
            for li in ul.find_all('li', recursive=False):
                a=li.find('a')
                if not a: continue
                href=a.get('href','')
                lab=a.get_text(strip=True)
                cur='current' in (li.get('class') or [])
                tabs.append({'label':lab,'href':href,'current':cur})
            if len(tabs)>=2:
                found[r]={'b':b,'leaf':leaf,'url':k,'tabs':tabs}
    except Exception as e:
        print('ERR',b,leaf,str(e)[:60], file=sys.stderr)
    time.sleep(0.8)
print(json.dumps(found, ensure_ascii=False, indent=1))
