#!/usr/bin/env python3
"""Patch /root/crawl_kaiyang.py and /root/crawl_juancheng.py - remove base64"""
import py_compile

# Patch /root/crawl_kaiyang.py
with open('crawl_kaiyang.py', 'r') as f:
    c = f.read()

old = """            if src and not src.startswith('data:'):
                try:
                    if src.startswith('/'):
                        abs_src = 'https://www.kaiyang.gov.cn' + src
                    elif not src.startswith('http'):
                        abs_src = 'https://www.kaiyang.gov.cn/' + src.lstrip('/')
                    else:
                        abs_src = src
                    img_req = urllib.request.Request(abs_src, headers={**HEADERS, 'Referer': url})
                    img_data = urllib.request.urlopen(img_req, timeout=15).read()
                    ext = abs_src.rsplit('.', 1)[-1].lower() if '.' in abs_src else 'jpg'
                    mime = {'jpg': 'image/jpeg', 'jpeg': 'image/jpeg', 'png': 'image/png',
                            'gif': 'image/gif', 'webp': 'image/webp'}.get(ext, 'image/jpeg')
                    b64_str = base64.b64encode(img_data).decode()
                    img['src'] = f'data:{mime};base64,{b64_str}'
                except Exception as e:
                    print(f"  [kaiyang] 图片下载失败 {src}: {e}")"""

new = """            if src and not src.startswith('data:'):
                if src.startswith('/'):
                    abs_src = 'https://www.kaiyang.gov.cn' + src
                elif not src.startswith('http'):
                    abs_src = 'https://www.kaiyang.gov.cn/' + src.lstrip('/')
                else:
                    abs_src = src
                img['src'] = abs_src"""

assert old in c, "kaiyang old not found!"
c = c.replace(old, new, 1)
with open('crawl_kaiyang.py', 'w') as f:
    f.write(c)
print("crawl_kaiyang.py: OK")

# Patch /root/crawl_juancheng.py
with open('crawl_juancheng.py', 'r') as f:
    c = f.read()

old2 = """            if src and not src.startswith('data:'):
                try:
                    if src.startswith('/'):
                        abs_src = BASE_URL + src
                    elif not src.startswith('http'):
                        abs_src = BASE_URL + '/' + src.lstrip('/')
                    else:
                        abs_src = src
                    img_req = urllib.request.Request(abs_src, headers={**HEADERS, 'Referer': url})
                    img_data = urllib.request.urlopen(img_req, timeout=15).read()
                    ext = abs_src.rsplit('.', 1)[-1].lower() if '.' in abs_src else 'jpg'
                    mime_map = {'jpg':'image/jpeg','jpeg':'image/jpeg','png':'image/png',
                                'gif':'image/gif','webp':'image/webp'}
                    img['src'] = f'data:{mime_map.get(ext, "image/jpeg")};base64,{base64.b64encode(img_data).decode()}'
                except:
                    pass"""

new2 = """            if src and not src.startswith('data:'):
                if src.startswith('/'):
                    abs_src = BASE_URL + src
                elif not src.startswith('http'):
                    abs_src = BASE_URL + '/' + src.lstrip('/')
                else:
                    abs_src = src
                img['src'] = abs_src"""

assert old2 in c, "juancheng old not found!"
c = c.replace(old2, new2, 1)
with open('crawl_juancheng.py', 'w') as f:
    f.write(c)
print("crawl_juancheng.py: OK")

# Verify
for fn in ['crawl_kaiyang.py', 'crawl_juancheng.py']:
    with open(fn, 'r') as f:
        cc = f.read()
    count = cc.count('b64encode')
    print("  {}: b64encode count = {}".format(fn, count))
    try:
        py_compile.compile(fn, doraise=True)
        print("  {}: syntax OK".format(fn))
    except py_compile.PyCompileError as e:
        print("  {}: SYNTAX ERROR: {}".format(fn, e))
