#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
南充市人民政府 - 新闻动态/公示公告 (www.nanchong.gov.cn/xwdt/gsgg/)
列表: div.normal-list > ul.list-ul > li > a[href="./202608/t20260810_2353100.html"] + span 日期
      静态 1 页 13 条 (createPage(1,0,"index","html") 证实无真实分页, "共40页" 是模板注释残留)
详情: meta ArticleTitle/PubDate + div.TRS_UEDITOR 正文 (保留 <p>/<table>)
样本: https://www.nanchong.gov.cn/xwdt/gsgg/202608/t20260810_2353100.html
      https://www.nanchong.gov.cn/xwdt/gsgg/t_2351041.html (短ID变体)
用法: python3 crawl_nanchong_gsgg.py [--pages=N | N] [--sync]
"""
import requests
import re
import sys
import os
import json
import html as html_mod
from bs4 import BeautifulSoup

SITE_NAME = "南充市-公示公告"
BASE_URL = "https://www.nanchong.gov.cn"
LIST_URL = "https://www.nanchong.gov.cn/xwdt/gsgg/"
HEADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
    "Referer": "https://www.nanchong.gov.cn/xwdt/gsgg/",
}
CUTOFF = "2023-01-01"

QUALITY_DB = os.path.join(os.path.dirname(os.path.abspath(__file__)), "quality_results.db")
SEARCH_DB = "/root/search.db"
DOMAIN = "www.nanchong.gov.cn"


def push_to_searchdb(records, batch_label):
    import sqlite3
    conn = sqlite3.connect(QUALITY_DB, timeout=60)
    conn.execute("PRAGMA busy_timeout=300000")
    cur = conn.cursor()
    cur.execute("""CREATE TABLE IF NOT EXISTS crawl_results (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        site_id INTEGER DEFAULT 0,
        title TEXT, url TEXT, content TEXT,
        publish_date TEXT, summary TEXT,
        domain TEXT, category TEXT DEFAULT '',
        content_hash TEXT, crawl_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        industry TEXT DEFAULT '', group_name TEXT DEFAULT '',
        script_name TEXT DEFAULT '', attachments TEXT DEFAULT '[]',
        UNIQUE(domain, url)
    )""")
    n_new = 0
    for r in records:
        try:
            cur.execute(
                "INSERT OR IGNORE INTO crawl_results (domain, title, url, content, publish_date, summary, industry, group_name, script_name, attachments) VALUES (?,?,?,?,?,?,?,?,?,?)",
                (r.get("domain", ""), r.get("title", ""), r.get("url", ""),
                 r.get("content", ""), r.get("pub_date", ""), r.get("summary", ""),
                 r.get("industry", ""), r.get("group_name", ""),
                 r.get("script_name", ""), r.get("attachments", "[]")),
            )
            n_new += cur.rowcount
        except Exception as e:
            print(f"  [push] ERR {r.get('url','')}: {e}")
    conn.commit()
    conn.close()
    print(f"[push_to_searchdb] new={n_new} skip={len(records)-n_new}")
    return n_new


def sync_to_server():
    import sqlite3
    try:
        conn = sqlite3.connect(QUALITY_DB, timeout=60)
        conn.execute("PRAGMA busy_timeout=300000")
        rows = conn.execute(
            "SELECT title, url, content, publish_date, summary, industry, group_name, script_name, attachments FROM crawl_results WHERE domain=? ORDER BY id",
            (DOMAIN,),
        ).fetchall()
        conn.close()
    except Exception as e:
        print(f"[sync] local read ERR: {e}")
        return
    if not rows:
        print("[sync] 本地没有数据")
        return
    dst = sqlite3.connect(SEARCH_DB, timeout=300)
    dst.execute("PRAGMA journal_mode=WAL")
    dst.execute("PRAGMA busy_timeout=300000")
    upd = ins = 0
    try:
        for r in rows:
            title, url, content, pub_date, summary, ind, grp, sn, att = r
            cur = dst.execute("SELECT id FROM gov_raw WHERE page_url=?", (url,))
            row = cur.fetchone()
            try:
                if row:
                    dst.execute(
                        "UPDATE gov_raw SET title=?, content=?, publish_date=?, summary=?, industry=?, group_name=?, script_name=?, attachments=? WHERE id=?",
                        (title, (content or "")[:500000], pub_date or "", (summary or "")[:300],
                         ind or "", grp or "", sn or "", att or "[]", row[0]))
                    upd += 1
                else:
                    dst.execute(
                        "INSERT INTO gov_raw (title, page_url, content, publish_date, summary, industry, group_name, script_name, attachments, site_name, source_url) VALUES (?,?,?,?,?,?,?,?,?,?,?)",
                        (title, url, (content or "")[:500000], pub_date or "", (summary or "")[:300],
                         ind or "", grp or "", sn or "", att or "[]", SITE_NAME, url))
                    ins += 1
            except Exception as e:
                print(f"  [sync] ERR {url}: {e}")
        dst.commit()
        dst.execute(
            "INSERT INTO gov_search(rowid,title,site_name,summary) "
            "SELECT r.id,r.title,r.site_name,r.summary FROM gov_raw r "
            "WHERE r.id NOT IN (SELECT rowid FROM gov_search) AND r.site_name=?",
            (SITE_NAME,))
        dst.commit()
        total = dst.execute("SELECT COUNT(*) FROM gov_raw WHERE site_name=?", (SITE_NAME,)).fetchone()[0]
    finally:
        dst.close()
    print(f"[sync] OK updated={upd} inserted={ins} DB共{total}条")


def clean_title(t):
    t = re.sub(r"&middot;|&nbsp;|&#160;|\u200b|\ufeff", "", t or "")
    t = re.sub(r"^[•·]\s*", "", t)
    return re.sub(r"\s+", " ", t).strip()


def fetch_list():
    """南充 gsgg 静态 1 页 13 条"""
    try:
        r = requests.get(LIST_URL, headers=HEADERS, timeout=30)
        r.raise_for_status()
        r.encoding = "utf-8"
        soup = BeautifulSoup(r.text, "html.parser")
    except Exception as e:
        print(f"  [list] ERR: {e}")
        return []
    items = []
    for ul in soup.select("ul.list-ul"):
        for li in ul.find_all("li"):
            a = li.find("a", href=True)
            if not a:
                continue
            href = a["href"].strip()
            if not href.startswith("./"):
                continue
            title = clean_title(a.get_text(strip=True))
            if not title:
                continue
            span = li.find("span")
            pub_date = span.get_text(strip=True) if span else ""
            pub_date = re.match(r"(\d{4}-\d{2}-\d{2})", pub_date).group(1) if re.match(r"(\d{4}-\d{2}-\d{2})", pub_date) else ""
            url = "https://www.nanchong.gov.cn/xwdt/gsgg/" + href[2:]
            items.append({"title": title, "url": url, "pub_date": pub_date})
    # 去重
    seen = set()
    uniq = []
    for it in items:
        if it["url"] in seen:
            continue
        seen.add(it["url"])
        uniq.append(it)
    return uniq


def parse_detail(url):
    try:
        r = requests.get(url, headers=HEADERS, timeout=30)
        r.raise_for_status()
        r.encoding = "utf-8"
        soup = BeautifulSoup(r.text, "html.parser")
    except Exception as e:
        print(f"  [detail] {url} ERR: {e}")
        return None

    title = ""
    m = soup.find("meta", attrs={"name": "ArticleTitle"})
    if m and m.get("content"):
        title = clean_title(m["content"])
    if not title:
        h = soup.find("h1")
        if h:
            title = clean_title(h.get_text())
    if not title:
        m = re.search(r"<title>(.*?)</title>", r.text, re.S)
        if m:
            title = clean_title(re.sub(r"-\\s*南充市人民政府$", "", m.group(1).strip()))

    pub_date = ""
    m = soup.find("meta", attrs={"name": "PubDate"})
    if m and m.get("content"):
        pub_date = m["content"].strip()[:10]

    body_el = soup.select_one("div.TRS_UEDITOR") or soup.select_one("div.view")
    if body_el is None:
        for el in soup.find_all(class_=re.compile("TRS_UEDITOR")):
            body_el = el
            break
    if body_el is None:
        return {"title": title, "content": "", "pub_date": pub_date, "attachments": []}

    # 附件
    atts = []
    for a in body_el.find_all("a", href=True):
        href = a["href"].strip()
        if re.search(r"\.(pdf|doc|docx|xls|xlsx|zip|rar|wps|7z)$", href, re.I):
            abs_url = href if href.startswith("http") else ("https:" + href if href.startswith("//") else BASE_URL + (href if href.startswith("/") else "/" + href))
            name = clean_title(a.get_text(strip=True)) or href.split("/")[-1]
            if not any(x["url"] == abs_url for x in atts):
                atts.append({"name": name, "url": abs_url})
    page_att = soup.select_one("div.attachment")
    if page_att:
        for a in page_att.find_all("a", href=True):
            href = a["href"].strip()
            if href:
                abs_url = href if href.startswith("http") else ("https:" + href if href.startswith("//") else BASE_URL + (href if href.startswith("/") else "/" + href))
                name = clean_title(a.get_text(strip=True)) or href.split("/")[-1]
                if not any(x["url"] == abs_url for x in atts):
                    atts.append({"name": name, "url": abs_url})

    # 附件段落从正文移除（末尾统一追加）
    for a in body_el.find_all("a", href=True):
        href = a["href"].strip()
        if re.search(r"\.(pdf|doc|docx|xls|xlsx|zip|rar|wps|7z)$", href, re.I):
            p = a.find_parent("p")
            if p:
                p.decompose()
    # 绝对化
    for a in body_el.find_all("a", href=True):
        href = a["href"].strip()
        if href.startswith("/") or href.startswith("./") or (not href.startswith("http") and not href.startswith("javascript")):
            a["href"] = ("https:" + href if href.startswith("//") else BASE_URL + (href if href.startswith("/") else "/" + href))
    for img in body_el.find_all("img"):
        src = img.get("src", "")
        if src and not src.startswith("http"):
            img["src"] = ("https:" + src if src.startswith("//") else BASE_URL + (src if src.startswith("/") else "/" + src))
    # 二维码/分享区清理
    for el in body_el.select("div.share, div.ewm, div.qrcode, div.jzgov-qrcode-div, div.dzzw-share"):
        el.decompose()
    for el in body_el.find_all(string=re.compile("扫一扫|分享到|打印本页")):
        p = el.find_parent("p") or el.find_parent("div")
        if p:
            p.decompose()

    body_html = str(body_el.decode_contents())
    body_html = re.sub(r"<script[^>]*>.*?</script>", "", body_html, flags=re.S | re.I)
    body_html = re.sub(r"<style[^>]*>.*?</style>", "", body_html, flags=re.S | re.I)
    body_html = re.sub(r"<p[^>]*>", "<p>", body_html)
    body_html = re.sub(r"<p>\s*</p>", "", body_html)
    body_html = re.sub(r'\s*<div id="docAppendix".*$', "", body_html, flags=re.S)
    body_html = re.sub(r'\s*<div class="video".*$', "", body_html, flags=re.S)

    if atts:
        has_att = any(a["url"] in body_html for a in atts)
        if not has_att:
            att_html = "".join(f'<p><a href="{a["url"]}">{html_mod.escape(a["name"])}</a></p>' for a in atts)
            body_html = body_html.rstrip() + "\n" + att_html

    text_len = len(re.sub(r"<[^>]+>", "", body_html).strip())
    has_img = "<img" in body_html
    if text_len < 5 and not has_img and not atts:
        return {"title": title, "content": "", "pub_date": pub_date, "attachments": atts}
    return {"title": title, "content": body_html, "pub_date": pub_date, "attachments": atts}


def main():
    max_pages = None
    for a in sys.argv[1:]:
        if a.startswith("--pages="):
            try:
                max_pages = int(a.split("=", 1)[1])
            except ValueError:
                pass
        elif a.isdigit():
            max_pages = int(a)
    print(f"[AutoPg] max_pages={max_pages}")

    items = fetch_list()
    print(f"[list] items={len(items)}")
    valid = []
    for it in items:
        if it["pub_date"] and it["pub_date"] < CUTOFF:
            print(f"  [cutoff] {it['pub_date']} {it['title'][:40]}")
            continue
        det = parse_detail(it["url"])
        if det is None:
            print(f"  [fail] {it['title'][:50]}")
            det = {"title": it["title"], "content": "", "pub_date": it["pub_date"], "attachments": []}
        rec = {
            "domain": DOMAIN,
            "site_name": SITE_NAME,
            "title": det["title"] or it["title"],
            "url": it["url"],
            "content": det["content"],
            "pub_date": det["pub_date"] or it["pub_date"],
            "summary": re.sub(r"<[^>]+>", "", det["content"])[:200],
            "industry": "power" if re.search(r"风电|风力发电|海上风电|燃煤发电|光伏|太阳能|电力", det["title"] or it["title"]) else "",
            "group_name": "四川",
            "script_name": os.path.basename(sys.argv[0]),
            "attachments": json.dumps(det["attachments"], ensure_ascii=False) if det["attachments"] else "[]",
        }
        valid.append(rec)
    print(f"[total] valid={len(valid)}")
    if valid:
        push_to_searchdb(valid, os.path.basename(sys.argv[0]))
        if "--sync" in sys.argv:
            sync_to_server()


if __name__ == "__main__":
    main()
