import requests,json,io,re,time
from bs4 import BeautifulSoup
import urllib3; urllib3.disable_warnings()
H={'User-Agent':'Mozilla/5.0'}
def fetch(u): return requests.get(u,headers=H,timeout=25,verify=False)
def bcount(url):
    try:
        r=fetch(url); s=BeautifulSoup(r.content,'html.parser')
    except Exception as e:
        return ('ERR',str(e)[:30])
    rows=s.select('table tbody tr')
    if not rows:
        # maybe gallery ul or no table
        pi=s.find(string=re.compile(r'\d+\s*/\s*\d+\s*페이지'))
        return (0,'norows')
    # collect first-cell numbers across page1
    nums=[]
    for tr in rows:
        tds=tr.find_all(['td','th'])
        if not tds: continue
        c0=tds[0].get_text(strip=True)
        if c0.isdigit(): nums.append(int(c0))
    # page total
    pi=s.find(string=re.compile(r'(\d+)\s*/\s*(\d+)\s*페이지'))
    P=None
    if pi:
        m=re.search(r'(\d+)\s*/\s*(\d+)\s*페이지',pi); P=int(m.group(2))
    if nums:
        maxn=max(nums)
        # if multipage, max number should reflect total; but page1 maxnum may be ~total
        return (maxn,f'maxno P{P}')
    else:
        # no numbered rows (gallery/notice-only) -> use pages*perpage
        per=len([t for t in rows if t.find_all('td')])
        if P: return (max(0,(P-1)*per+per) if P else per,f'estP{P}x{per}')
        return (len(rows),'rowcount')

out=json.load(io.open('_tabgroups.json',encoding='utf-8'))
res={}
seen=set()
for g in out:
    for t in g:
        if t['board'] and not t['in_sheet'] and t['menuNo'] and t['url'] not in seen:
            seen.add(t['url'])
            res[t['url']]={'label':t['label'],'M':bcount(t['url'])}
            time.sleep(0.1)
io.open('_newboardM.json','w',encoding='utf-8').write(json.dumps(res,ensure_ascii=False,indent=1))
for u,d in res.items():
    print(d['M'], d['label'])
print("=== G7 사전정보공표 categories ===")
g7={}
for i in range(1,9):
    u=f"https://www.kdhc.co.kr/kdhc/infoOthbc/infoPrmlgt/list.do?searchPublictRealmCd=G0{i}&menuNo=200013"
    g7[f'G0{i}']=bcount(u); time.sleep(0.1)
base=bcount("https://www.kdhc.co.kr/kdhc/infoOthbc/infoPrmlgt/list.do?menuNo=200013")
print('BASE(no cat)',base)
for k,v in g7.items(): print(k,v)
io.open('_g7M.json','w',encoding='utf-8').write(json.dumps({'base':base,'cats':g7},ensure_ascii=False))
