# -*- coding: utf-8 -*-
"""Lấy HTML trang tạo hỗn hợp từ server CALIB + in repr đoạn <script> để soi ký tự thật."""
import http.cookiejar, re, urllib.parse, urllib.request

mk = open(r'C:\Claw 2\Nghien cuu\Du-an-B-He-mau-Stain-Glaze\calib-app\mat_khau.txt', encoding='utf-8').read().strip()
o = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()))
o.open('http://127.0.0.1:8099/dang-nhap', urllib.parse.urlencode({'mat_khau': mk}).encode(), timeout=20).read()
h = o.open('http://127.0.0.1:8099/', timeout=20).read().decode('utf-8')
open(r'C:\Users\admin\AppData\Local\Temp\calib_trang.html', 'w', encoding='utf-8').write(h)
print('HTML:', len(h), 'byte')
i = h.find('<script>')
j = h.find('</script>')
sc = h[i:j + 9]
print('--- 3 dòng JS quan trọng (repr) ---')
for l in sc.split('\n'):
    if 'themHang' in l or 'closest' in l or 'const DS' in l:
        print(repr(l[:400]))
print('--- kiểm ký tự ---')
print('  có "\\\\\'" (nháy đơn escape):', "\\'" in sc)
print('  số dấu % trong JS:', sc.count('%'))
print('  số DS option:', h.count('<option'))
