import openpyxl,requests,re,io,json,time,sys
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 maxno(url):
    try: r=fetch(url); s=BeautifulSoup(r.content,'html.parser')
    except Exception as e: return ('ERR',str(e)[:25])
    rows=s.select('table tbody tr')
    pi=s.find(string=re.compile(r'(\d+)\s*/\s*(\d+)\s*페이지'))
    P=int(re.search(r'(\d+)\s*/\s*(\d+)',pi).group(2)) if pi else None
    nums=[]
    for tr in rows:
        tds=tr.find_all(['td','th'])
        if tds and tds[0].get_text(strip=True).isdigit(): nums.append(int(tds[0].get_text(strip=True)))
    if nums: return (max(nums),f'maxno P{P}')
    if rows: return (len([t for t in rows if t.find_all('td')]), f'rowcnt P{P}')
    return (0,'norows')
wb=openpyxl.load_workbook('한국지역난방공사.xlsx'); ws=wb.active
rep=[]
for r in range(3,ws.max_row+1):
    if ws.cell(r,12).value!='게시판': continue
    k=ws.cell(r,11).value; curM=ws.cell(r,13).value
    F=ws.cell(r,6).value; G=ws.cell(r,7).value
    M,tag=maxno(k); time.sleep(0.08)
    rep.append({'r':r,'curM':curM,'newM':M,'tag':tag,'label':G or F,'k':k})
io.open('_boardM_all.json','w',encoding='utf-8').write(json.dumps(rep,ensure_ascii=False,indent=0))
chg=[x for x in rep if str(x['curM'])!=str(x['newM']) and isinstance(x['newM'],int)]
print('total boards',len(rep),'would change',len(chg))
