# -*- coding: utf-8 -*-
"""신규 게시판(페이지→게시판 전환) 중 N에 '이미지' 있는 행을 게시판 방식(상세글 렌더)으로 N 재판정.
대상: _lfix_result.txt 의 전환행 ∩ 현재 게시판&이미지. 사용: python _공공기관_nboard2.py
"""
import sys, os, re, importlib.util, warnings
import openpyxl
warnings.filterwarnings('ignore')
NB = importlib.util.module_from_spec(importlib.util.spec_from_file_location('nb', r'D:\01.프로젝트\DB수집\_스크립트\_공공기관_nboard.py'))
importlib.util.spec_from_file_location('nb', r'D:\01.프로젝트\DB수집\_스크립트\_공공기관_nboard.py').loader.exec_module(NB)
P = NB.P
OUTDIR = r'D:\01.프로젝트\DB수집\공공기관'
LOG = open(r'D:\01.프로젝트\DB수집\_스크립트\_nboard2_result.txt', 'w', encoding='utf-8')


def main():
    # lfix 로그 → 전환행
    newb = {}
    for line in open(r'D:\01.프로젝트\DB수집\_스크립트\_lfix_result.txt', encoding='utf-8'):
        m = re.match(r'  (\S+) r(\d+) 페이지→게시판', line)
        if m:
            newb.setdefault(m.group(1), set()).add(int(m.group(2)))
    from playwright.sync_api import sync_playwright
    with sync_playwright() as pw:
        b = pw.chromium.launch()
        pg = b.new_page(user_agent=NB.UA, viewport={'width': 1280, 'height': 1600})
        sess = P.make_session()
        tot_img = tot_txt = 0
        for name, rowset in newb.items():
            xlsx = os.path.join(OUTDIR, f'{name}.xlsx')
            wb = openpyxl.load_workbook(xlsx)
            ws = wb.active
            ic = tc = 0
            for r in sorted(rowset):
                if ws.cell(r, 12).value != '게시판':
                    continue
                N = ws.cell(r, 14).value or ''
                if '이미지' not in N:
                    continue
                url = ws.cell(r, 11).value
                try:
                    soup, _ = P.fetch(sess, url)
                    if not soup:
                        continue
                    body = P.get_body(soup, P.BODY_SEL)
                    found = False
                    for du in P.extract_detail_urls(body, url, limit=4):
                        try:
                            pg.goto(du, timeout=20000, wait_until='domcontentloaded'); pg.wait_for_timeout(1500)
                            if NB.has_content_img(pg.evaluate(NB.JS_IMGS)):
                                found = True; break
                        except Exception:
                            continue
                    if found:
                        ws.cell(r, 14).value = NB.add_image(N); ic += 1
                    else:
                        ws.cell(r, 14).value = NB.rm_image(N); tc += 1
                except Exception:
                    continue
            wb.save(xlsx)
            tot_img += ic; tot_txt += tc
            LOG.write(f'[{name}] 신규게시판 N재판정 → 이미지유지 {ic}·어문강등 {tc}\n'); LOG.flush()
        b.close()
        LOG.write(f'=== 합계 이미지유지 {tot_img}·어문강등 {tot_txt} ===\n')
    LOG.close()


if __name__ == '__main__':
    main()
