# -*- coding: utf-8 -*-
import urllib.request, ssl, hashlib, re, io
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
def fetch(u):
    try:
        req=urllib.request.Request(u,headers={"User-Agent":"Mozilla/5.0"})
        r=urllib.request.urlopen(req,context=ctx,timeout=20)
        raw=r.read()
        final=r.geturl()
        try: html=raw.decode("utf-8")
        except: html=raw.decode("euc-kr","ignore")
        # extract title + main content text
        t=re.search(r'<title>(.*?)</title>',html,re.S)
        title=t.group(1).strip() if t else ""
        # strip tags, collapse ws
        body=re.sub(r'<script.*?</script>','',html,flags=re.S)
        body=re.sub(r'<style.*?</style>','',body,flags=re.S)
        txt=re.sub(r'<[^>]+>',' ',body)
        txt=re.sub(r'\s+',' ',txt).strip()
        return final,title,len(txt),hashlib.md5(txt.encode()).hexdigest()[:10],txt
    except Exception as e:
        return "ERR:"+str(e),"",0,"",""
pairs=[
 ("신재생에너지","https://www.kdhc.co.kr/kdhc/main/contents.do?menuNo=200495","https://www.kdhc.co.kr/kdhc/main/contents.do?menuNo=200150"),
 ("환경친화설비","https://www.kdhc.co.kr/kdhc/main/contents.do?menuNo=200362","https://www.kdhc.co.kr/kdhc/main/contents.do?menuNo=200154"),
 ("연혁","https://www.kdhc.co.kr/kdhc/bbs/B0000066/list.do?menuNo=200226","https://www.kdhc.co.kr/kdhc/main/contents.do?menuNo=200169"),
 ("조직도","https://www.kdhc.co.kr/kdhc/main/contents.do?menuNo=200246","https://www.kdhc.co.kr/kdhc/main/contents.do?menuNo=200319"),
]
out=io.open("_cmp_out.txt","w",encoding="utf-8")
for nm,a,b in pairs:
    fa=fetch(a); fb=fetch(b)
    same = fa[3]==fb[3] and fa[3]!=""
    out.write(f"### {nm}  SAME_CONTENT={same}\n")
    out.write(f"  A {a}\n    final={fa[0]} len={fa[2]} md5={fa[3]} title={fa[1]}\n")
    out.write(f"  B {b}\n    final={fb[0]} len={fb[2]} md5={fb[3]} title={fb[1]}\n")
    # show distinctive snippet
    out.write(f"  A-snip: {fa[4][:200]}\n")
    out.write(f"  B-snip: {fb[4][:200]}\n\n")
out.close(); print("done")
