# -*- coding: utf-8 -*-
import sys,io,re,time,warnings
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
warnings.filterwarnings('ignore')
import requests
from bs4 import BeautifulSoup
import openpyxl,importlib.util,os
# load engine for process_row
spec=importlib.util.spec_from_file_location('p234',r'D:\01.프로젝트\DB수집\_스크립트\_공공기관2_phase234.py')
p=importlib.util.module_from_spec(spec); spec.loader.exec_module(p)
XL=r'10.한국장애인고용공단\한국장애인고용공단.xlsx'
wb=openpyxl.load_workbook(XL); ws=wb.active
# 1) delete junk residue rows (no B, no labels)
junk=[]
for r in range(3,ws.max_row+1):
    B=ws.cell(r,2).value
    labels=[ws.cell(r,c).value for c in range(4,11) if ws.cell(r,c).value]
    anyval=any(ws.cell(r,c).value not in (None,'') for c in range(2,28))
    if anyval and B is None and not labels:
        junk.append(r)
for r in sorted(junk,reverse=True):
    ws.delete_rows(r,1)
print('deleted junk rows:',len(junk))
# 2) retry 접근 실패 rows sequentially with retries
sess=requests.Session(); sess.headers.update(p.H)
def fetch_retry(url,tries=4):
    for i in range(tries):
        try:
            r=sess.get(url,timeout=20,verify=False,allow_redirects=True)
            m=re.search(rb'charset=["\']?\s*([\w-]+)',r.content[:4096],re.I)
            r.encoding=m.group(1).decode(errors='ignore') if m else r.apparent_encoding
            if r.status_code==200:
                return BeautifulSoup(r.text,'html.parser'),r.url
        except Exception:
            pass
        time.sleep(1.0+i)
    return None,None
# monkeypatch engine fetch to use retry
p.fetch=lambda session,url,timeout=12: fetch_retry(url)
fails=[r for r in range(3,ws.max_row+1) if ws.cell(r,19).value=='접근 실패' or (ws.cell(r,11).value and not ws.cell(r,12).value and ws.cell(r,2).value is not None)]
print('rows to retry:',len(fails))
ok=0
for r in fails:
    url=ws.cell(r,11).value
    if not url or not isinstance(url,str) or not url.startswith('http'): continue
    res=p.process_row(sess,url,p.BODY_SEL)
    if res.get('L'):
        ws.cell(r,12).value=res['L']
        ws.cell(r,13).value=res['M'] if res['M']!='' else None
        if res.get('N'): ws.cell(r,14).value=res['N']
        ws.cell(r,15).value=res['O'] or '미부착'
        if res.get('P'): ws.cell(r,16).value=res['P']
        if res.get('Q'): ws.cell(r,17).value=res['Q']
        if ws.cell(r,19).value=='접근 실패': ws.cell(r,19).value=None
        ok+=1
    if ok%20==0 and ok: print('  retried',ok)
print('retry success:',ok,'/',len(fails))
wb.save(XL)
print('saved')
