# coding: utf-8
import requests, re, io, sys, openpyxl
H={'User-Agent':'Mozilla/5.0','Referer':'https://www.kisa.or.kr/','X-Requested-With':'XMLHttpRequest'}
out=io.open('_scan_out.txt','w',encoding='utf-8')
def log(*a): out.write(' '.join(str(x) for x in a)+'\n')

wb=openpyxl.load_workbook('한국인터넷진흥원.xlsx')
ws=wb.active

def seq_of(url):
    if not url: return None
    m=re.search(r'kisa\.or\.kr/(\d+)',url)
    return m.group(1) if m else None

def tablist(seq):
    try:
        r=requests.get('https://www.kisa.or.kr/selectTabList.do',headers=H,
                       params={'menu_seq':seq,'lang_type':'KO'},timeout=20)
        if 'json' in r.headers.get('content-type','') and r.text.strip().startswith('['):
            return r.json()
    except Exception as e:
        return ('ERR',str(e))
    return None

# scan rows 14..max (B순번 12부터). Row index in sheet.
log('row Bnum url_seq -> tabcount [tabs]')
for row in range(14, ws.max_row+1):
    k=ws.cell(row,11).value  # K url
    bnum=ws.cell(row,2).value
    seq=seq_of(k)
    if not seq:
        log(row, bnum, k, '-> no-seq')
        continue
    data=tablist(seq)
    if isinstance(data,tuple):
        log(row,bnum,seq,'-> ',data); continue
    if not data:
        log(row,bnum,seq,'-> no-tabs')
        continue
    tabs=[(d.get('menu_seq'),d.get('menu_name'),d.get('content_type')) for d in data]
    log(row,bnum,seq,'-> TABS(%d)'%len(tabs))
    for t in tabs:
        log('     ',t[0],repr(t[1]),t[2])
out.close()
print('done')
