#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
焦作市城乡一体化示范区管委会 - 公示公告
https://www.jzxq.gov.cn/zwgk/gsgg/

CMS: 焦作统一平台 (t.jiaozuo.gov.cn 资源 + paging.js 分页)
列表: div#pageDec > ul > li > a.item[href=/YYYY/MM-DD/N.html] > i + 标题文本 + span 日期
      每页 24 条 (pageDec pagesize=24 pagecount=1426 → 60页)
分页: 首页 /zwgk/gsgg/ (或 index.html)；第 N+1 页 = /zwgk/gsgg/index_{N}.html
      (paging.js goto: page-=1 → reLoadUrl + 'index' + returnp + '.html', 0-based)
详情: div.cont > div.newtit (标题) + div.newtime span (发表时间：YYYY-MM-DD) + div.newc (正文, p+table)
      meta SiteIDCode=4108900001, newsId hidden 字段
"""
import sys
import os
import re
import time
import html as html_lib
import urllib.parse
import sqlite3

try:
    from bs4 import BeautifulSoup
except ImportError:
    BeautifulSoup = None

# ---------------- config ----------------
BASE_URL = "https://www.jzxq.gov.cn"
LIST_PATH = "/zwgk/zfxxgkml/"
LIST_URL = BASE_URL + LIST_PATH
SITE_NAME = "焦作示范区管委会-政府信息公开目录"
GROUP_NAME = "河南"
SCRIPT_NAME = "crawl_jzxq_zfxxgkml.py"
DB_PATH = "/mnt/data/search.db"

HEADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.9",
}

# crawler_lib 所在目录 (classify_industry 行业分类复用)
for _p in ("/root/gov_crawler", "/root/gov_crawler"):
    if os.path.isdir(_p) and _p not in sys.path:
        sys.path.insert(0, _p)

# default: daily incremental = 1 page
_MAX_PAGES = 1
for i, a in enumerate(sys.argv):
    if a.startswith("--pages="):
        try:
            _MAX_PAGES = int(a.split("=", 1)[1])
        except ValueError:
            pass
    elif a == "--pages" and i + 1 < len(sys.argv):
        try:
            _MAX_PAGES = int(sys.argv[i + 1])
        except ValueError:
            pass

_PAGES = _MAX_PAGES if _MAX_PAGES >= 1 else 1


# ---------------- helpers ----------------
def clean_title(t):
    """unescape entities + strip &middot;&nbsp; prefix + strip zero-width + collapse whitespace."""
    if not t:
        return ""
    t = html_lib.unescape(t)
    t = re.sub(r"^\s*[·•]+\s*", "", t)
    t = t.replace("\xa0", " ").replace("\u200b", "").replace("\u200c", "").replace("\u200d", "").replace("\ufeff", "")
    t = re.sub(r"\s+", " ", t)
    return t.strip()


def fetch(url, session=None, timeout=30, retries=3):
    import requests
    last = None
    for i in range(retries):
        try:
            r = (session or requests).get(url, headers=HEADERS, timeout=timeout)
            if r.status_code == 200:
                r.encoding = r.apparent_encoding or "utf-8"
                return r
            last = r
        except Exception as e:
            last = e
        time.sleep(1.5 * (i + 1))
    if isinstance(last, Exception):
        raise last
    return last


def html_to_text(content, page_url):
    """Convert article inner HTML to stored format:
    - keep <table> HTML intact
    - \n\n between <p> blocks
    - attachment <a> links embedded with absolute URL
    """
    if not content:
        return "", 0, []
    content = re.sub(r"<!--.*?-->", "", content, flags=re.S)
    content = re.sub(r"<script[^>]*>.*?</script>", "", content, flags=re.S | re.I)
    content = re.sub(r"<style[^>]*>.*?</style>", "", content, flags=re.S | re.I)
    if BeautifulSoup:
        try:
            _soup = BeautifulSoup(content, "html.parser")
            for tag in _soup.find_all(True):
                for attr in ("style", "class", "lang", "dir", "align", "valign", "width", "height", "border", "cellpadding", "cellspacing"):
                    tag.attrs.pop(attr, None)
            content = str(_soup)
        except Exception:
            pass

    # 1) FIRST protect attachment <a> links
    content2 = content
    attachments = []
    link_protect = {}
    def _link_repl(m):
        href = m.group(1)
        inner = m.group(2)
        txt = re.sub(r"<[^>]+>", "", inner)
        txt = html_lib.unescape(txt).strip()
        txt = re.sub(r"^附件[:：]\s*", "", txt).strip()
        if not txt:
            txt = os.path.basename(href.split("?")[0]) or "附件"
        abs_url = urllib.parse.urljoin(page_url, html_lib.unescape(href))
        attachments.append((abs_url, txt))
        key = f"__LINK__{len(link_protect)}__"
        link_protect[key] = f'<a href="{abs_url}" target="_blank">{txt}</a>'
        return key
    content2 = re.sub(r'<a\s[^>]*href="([^"]+)"[^>]*>(.*?)</a>', _link_repl, content2, flags=re.S | re.I)

    # 2) THEN protect tables
    table_protect = []
    def _tbl_repl(m):
        table_protect.append(m.group(0))
        return f"__TBL__{len(table_protect)-1}__"
    content2 = re.sub(r"<table[^>]*>.*?</table>", _tbl_repl, content2, flags=re.S | re.I)
    has_table = 1 if re.search(r"<table[^>]*>", content, re.I) else 0

    content2 = re.sub(r"</p>", "</p>\n\n", content2, flags=re.I)
    content2 = re.sub(r"<br\s*/?>", "\n", content2, flags=re.I)
    content2 = re.sub(r"</?(?:span|font|o:p|st1?:[a-z]+)\b[^>]*>", "", content2, flags=re.I)

    parts = []
    for block in content2.split("\n\n"):
        b = block.strip()
        if not b:
            continue
        plain = re.sub(r"<[^>]+>", "", b)
        plain = html_lib.unescape(plain)
        plain = plain.replace("\xa0", " ").replace("&nbsp;", " ")
        plain = re.sub(r"[\s\u200b\u200c\u200d\ufeff]+", "", plain)
        if not plain:
            continue
        for k, v in link_protect.items():
            b = b.replace(k, v)
        for i, tbl in enumerate(table_protect):
            b = b.replace(f"__TBL__{i}__", tbl)
        parts.append(b.strip())

    out = "\n\n".join(parts)
    for k, v in link_protect.items():
        out = out.replace(k, v)
    for i, tbl in enumerate(table_protect):
        out = out.replace(f"__TBL__{i}__", tbl)

    out = re.sub(r"__LINK__\d+__", "", out)
    out = re.sub(r"__TBL__\d+__", "", out)

    out = html_lib.unescape(out)
    out = re.sub(r"[ \t]+\n", "\n", out)
    out = re.sub(r"\n{3,}", "\n\n", out)
    out = re.sub(r"[ \t]{2,}", " ", out)

    # dedupe paragraphs
    seen = set()
    final = []
    for p in out.split("\n\n"):
        key = re.sub(r"\s+", "", re.sub(r"<[^>]+>", "", p))
        if not key:
            continue
        if key in seen:
            continue
        seen.add(key)
        final.append(p)
    out = "\n\n".join(final).strip()

    return out, has_table, attachments


def parse_list(html_text, page_url):
    """Extract (url, title, date) from list page."""
    items = []
    if BeautifulSoup:
        soup = BeautifulSoup(html_text, "html.parser")
        # precise: a.item links with /YYYY/MM-DD/N.html
        for a in soup.find_all("a", class_=re.compile(r"item")):
            href = a.get("href", "").strip()
            if not re.search(r"/\d{4}/\d{2}-\d{2}/\d+\.html$", href):
                continue
            abs_url = urllib.parse.urljoin(LIST_URL, href)
            title = clean_title(a.get_text(" ", strip=True))
            # span 日期在 a 之后
            li = a.find_parent("li")
            date = ""
            if li:
                sp = li.find("span")
                if sp:
                    m = re.match(r"^(\d{4}-\d{2}-\d{2})", sp.get_text(strip=True))
                    if m:
                        date = m.group(1)
            items.append((abs_url, title, date))
    if not items:
        # regex fallback: li > a.item[href] > i + text + span
        for m in re.finditer(
            r'<a[^>]*class="item"[^>]*href="([^"]*?/\d{4}/\d{2}-\d{2}/\d+\.html)"[^>]*>(?:<i[^>]*></i>)?([^<]*?)</a>\s*<span[^>]*>([^<]*)</span>',
            html_text, re.S,
        ):
            abs_url = urllib.parse.urljoin(LIST_URL, m.group(1))
            items.append((abs_url, clean_title(m.group(2)), m.group(3).strip()))
    return items


def parse_detail(html_text, page_url):
    """Extract title, date, content from detail page."""
    title = ""
    date = ""
    content_html = ""
    if BeautifulSoup:
        soup = BeautifulSoup(html_text, "html.parser")
        # title: div.newtit
        nt = soup.find("div", class_=re.compile(r"newtit"))
        if nt:
            title = clean_title(nt.get_text(" ", strip=True))
        # date: div.newtime span (发表时间：YYYY-MM-DD)
        ntm = soup.find("div", class_=re.compile(r"newtime"))
        if ntm:
            txt = ntm.get_text(" ", strip=True)
            m = re.search(r"(\d{4})-(\d{2})-(\d{2})", txt)
            if m:
                date = m.group(0)
        # content: div.newc
        nc = soup.find("div", class_=re.compile(r"newc"))
        if nc:
            content_html = str(nc)
    if not title or not content_html:
        # regex fallback
        m = re.search(r'<div class="newtit">(.*?)</div>', html_text, re.S)
        if m:
            title = clean_title(re.sub(r"<[^>]+>", "", m.group(1)))
        m = re.search(r'发表时间[：:]\s*(\d{4}-\d{2}-\d{2})', html_text)
        if m:
            date = m.group(1)
        m = re.search(r'<div class="newc">(.*?)<div class="ht10"', html_text, re.S)
        if m:
            content_html = m.group(1)
    return title, date, content_html


def store_record(conn, url, title, content, date, has_table):
    cur = conn.execute(
        "SELECT id FROM gov_raw WHERE page_url=?",
        (url,),
    )
    if cur.fetchone():
        return False
    try:
        industry = "other"
        try:
            from crawler_lib import classify_industry
            industry = classify_industry(title)
        except Exception:
            pass
        cur = conn.execute(
            "INSERT OR IGNORE INTO gov_raw "
            "(page_url, source_url, title, content, publish_date, site_name, group_name, script_name, has_table, industry) "
            "VALUES (?,?,?,?,?,?,?,?,?,?)",
            (url, url, title, content, date, SITE_NAME, GROUP_NAME, SCRIPT_NAME, has_table, industry),
        )
        if cur.rowcount > 0:
            summary = re.sub(r"<[^>]+>", "", content)
            summary = html_lib.unescape(summary)
            summary = re.sub(r"\s+", " ", summary).strip()[:200]
            conn.execute(
                "INSERT OR IGNORE INTO gov_search (rowid, title, site_name, summary) VALUES (?,?,?,?)",
                (cur.lastrowid, title, SITE_NAME, summary),
            )
            return True
        return False
    except sqlite3.OperationalError as e:
        if "locked" in str(e):
            time.sleep(3)
            return store_record(conn, url, title, content, date, has_table)
        raise


def main():
    import requests
    s = requests.Session()
    s.headers.update(HEADERS)
    conn = sqlite3.connect(DB_PATH, timeout=60)
    conn.execute("PRAGMA busy_timeout=60000")
    conn.execute("PRAGMA journal_mode=WAL")

    new_count = 0
    skip_count = 0
    seen_urls = set()

    # 首页
    list_url = LIST_URL
    r = fetch(list_url, s)
    if not r:
        print("新增: 0")
        return
    html_text = r.text

    for page in range(1, _PAGES + 1):
        if page > 1:
            # 第2页起: index_{page-1}.html (paging.js 0-based)
            page_url = f"{LIST_URL}index_{page-1}.html"
            r = fetch(page_url, s)
            if not r:
                continue
            html_text = r.text
        else:
            page_url = list_url
        items = parse_list(html_text, page_url)
        if not items:
            continue
        for url, title, date in items:
            if url in seen_urls:
                continue
            seen_urls.add(url)
            try:
                dr = fetch(url, s)
            except Exception as e:
                print(f"  [ERR] {url}: {e}")
                skip_count += 1
                continue
            if not dr:
                skip_count += 1
                continue
            d_title, d_date, d_html = parse_detail(dr.text, url)
            if not d_title:
                d_title = title
            if not d_date:
                d_date = date
            content, has_table, attachments = html_to_text(d_html, url)
            if not content:
                skip_count += 1
                continue
            if store_record(conn, url, d_title, content, d_date, has_table):
                new_count += 1
            else:
                skip_count += 1

    conn.commit()
    conn.close()
    print(f"新增: {new_count}")


if __name__ == "__main__":
    main()
