# -*- coding: utf-8 -*-
import ssl,urllib.request,re,json,time
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) Chrome/124 Safari/537.36'}
def fetch(u): return urllib.request.urlopen(urllib.request.Request(u,headers=HDR),timeout=25,context=ctx).read()
UI=re.compile(r'banner|/common/|icon|btn|logo|bullet|sprite|blank|no_img|arrow|/dot|/bg|popup|/sns',re.I)
B='https://www.komipo.co.kr'
boards={
 '월간 인권소식지':'/esg/board/BRD_000117/boardMain.do?mnCd=ESG03030202',
 '전사 캠페인':'/esg/board/BRD_000096/boardMain.do?mnCd=ESG03030203',
 '인권공모전':'/esg/board/BRD_000115/boardMain.do?mnCd=ESG03030302',
 '인권경영 자료공유':'/esg/board/BRD_000097/boardMain.do?mnCd=ESG03030303',
 '확산활동 및 자료실':'/esg/board/BRD_000093/boardMain.do?mnCd=ESG04020302',
}
out={}
for lab,h in boards.items():
    u=B+h
    try:
        soup=BeautifulSoup(fetch(u),'html.parser')
        em=soup.select_one('div.total em.count') or soup.select_one('.total .count')
        M=int(re.sub(r'[^0-9]','',em.get_text())) if em else 0
        cont=soup.select_one('#content') or soup
        thumbs=[im for im in cont.find_all('img') if 'getImage' in im.get('src','') and not UI.search(im.get('src',''))]
        N='없음' if M==0 else ('어문,이미지' if thumbs else '어문')
        out[lab]={'url':u,'M':M,'N':N,'thumbs':len(thumbs)}
        print(f'{lab}: M={M} N={N} thumbs={len(thumbs)}')
    except Exception as e:
        print('ERR',lab,str(e)[:50]); out[lab]={'url':u,'M':0,'N':'어문'}
    time.sleep(0.7)
json.dump(out,open('boards.json','w',encoding='utf-8'),ensure_ascii=False,indent=1)
