#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
crawl_shenghong_notice - 盛虹控股-集团公告
https://www.shenghongholding.com/news/notice
列表: Vite SSR 静态, div.news-item (news-date/news-title/a.public-more)
分页: /news/notice/page/{n}/page_size/{size}  (size 10/20/50/100, 全量 12 条 1 页)
详情: h1.news-content-title + div.news-content-body (braft 富文本)
附件: span.bf-attachment (data-name/data-url) -> <p><a href=绝对URL>附件名</a></p>
"""
import sys
import os
import re
import time
import html as html_lib
import urllib.parse
import sqlite3
import json

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

# ---------------- config ----------------
BASE_URL = "https://www.shenghongholding.com"
LIST_URL = "https://www.shenghongholding.com/news/notice"
SITE_NAME = "盛虹控股-集团公告"
GROUP_NAME = "江苏"
SCRIPT_NAME = "crawl_shenghong_notice.py"
DB_PATH = os.environ.get("DB_PATH", "/mnt/data/search.db")
JSONL_PATH = os.environ.get("JSONL_PATH", "")

HEADERS = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.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",
}

_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


def clean_title(t):
    if not t:
        return ""
    t = html_lib.unescape(t)
    t = t.replace("\u200b", "").replace("\u200c", "").replace("\u200d", "").replace("\ufeff", "")
    # strip &middot;&nbsp; 实体前缀
    t = re.sub(r"^\s*(?:&middot;|·|\u00b7)?\s*(?:&nbsp;|\u00a0)?\s*", "", t)
    t = re.sub(r"\s+", " ", t)
    return t.strip()


def fetch_list(session, page):
    if page == 1:
        url = LIST_URL + "/page/1/page_size/100"
    else:
        url = LIST_URL + f"/page/{page}/page_size/100"
    r = session.get(url, headers=HEADERS, timeout=30)
    r.encoding = "utf-8"
    return r.text


def parse_list(html_text):
    items = []
    if not BeautifulSoup:
        return items
    soup = BeautifulSoup(html_text, "html.parser")
    for it in soup.select("div.news-item"):
        t = it.select_one(".news-title")
        d = it.select_one(".news-date")
        a = it.select_one("a.public-more")
        if not t or not a:
            continue
        title = clean_title(t.get_text(" ", strip=True))
        if not title or len(title) < 4:
            continue
        href = a.get("href", "")
        if not href:
            continue
        abs_url = urllib.parse.urljoin(LIST_URL, href)
        date = ""
        if d:
            date = d.get_text(strip=True).strip()
            dm = re.match(r"(\d{4}-\d{2}-\d{2})", date)
            if dm:
                date = dm.group(1)
        items.append((abs_url, title, date))
    return items


def parse_detail(html_text, page_url):
    title = ""
    date = ""
    content_html = ""
    if BeautifulSoup:
        soup = BeautifulSoup(html_text, "html.parser")
        h1 = soup.select_one("h1.news-content-title")
        if h1:
            title = clean_title(h1.get_text(" ", strip=True))
        dc = soup.select_one("div.news-content-body")
        if dc:
            content_html = "".join(str(c) for c in dc.contents)
        dd = soup.select_one("div.news-content-date")
        if dd:
            raw = dd.get_text(" ", strip=True)
            dm = re.match(r"(\d{4}-\d{2}-\d{2})", raw)
            if dm:
                date = dm.group(1)
    if not title:
        m = re.search(r"<h1[^>]*class=\"[^\"]*news-content-title[^\"]*\"[^>]*>(.*?)</h1>", html_text, re.S | re.I)
        if m:
            title = clean_title(re.sub(r"<[^>]+>", "", m.group(1)))
    if not date:
        m = re.search(r"(\d{4})-(\d{2})-(\d{2})", html_text)
        if m:
            date = m.group(0)
    return title, date, content_html


def html_to_text(content, page_url):
    if not content:
        return "", 0, []
    content = re.sub(r"<!--.*?-->", "", content, flags=re.S)
    content = re.sub(r"<meta[^>]*>", "", content, flags=re.I)
    content = re.sub(r"<script[^>]*>.*?</script>", "", content, flags=re.S | re.I)
    content = re.sub(r"<style[^>]*>.*?</style>", "", content, flags=re.S | re.I)

    attachments = []
    link_protect = {}

    # braft 附件: <span class="bf-attachment" data-name=".." data-url="/uploads/..">..</span>
    # 必须在属性清理之前处理(否则 class 被删后无法定位)
    if BeautifulSoup:
        try:
            _soup = BeautifulSoup(content, "html.parser")
            for sp in _soup.find_all("span", class_=re.compile(r"bf-attachment", re.I)):
                name = (sp.get("data-name") or "").strip()
                url = (sp.get("data-url") or "").strip()
                if not url:
                    sp.decompose()
                    continue
                name = html_lib.unescape(name).strip()
                if not name:
                    name = os.path.basename(url.split("?")[0]) or "附件"
                abs_url = urllib.parse.urljoin(page_url, url)
                if abs_url.startswith("//"):
                    abs_url = "http:" + abs_url
                attachments.append((abs_url, name))
                key = f"__ATTACH__{len(link_protect)}__"
                link_protect[key] = f'<a href="{abs_url}" target="_blank">{name}</a>'
                sp.replace_with(key)
            content = str(_soup)
        except Exception:
            pass

    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
    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, href)
        if abs_url.startswith("//"):
            abs_url = "http:" + abs_url
        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
    content = re.sub(r'<a\s[^>]*href="([^"]+)"[^>]*>(.*?)</a>', _link_repl, content, flags=re.S | re.I)

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

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

    parts = []
    for block in content.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)

    # 去重
    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 store_record(conn, url, title, content, date, has_table):
    cur = conn.execute("SELECT id FROM gov_raw WHERE page_url=? AND site_name=?", (url, SITE_NAME))
    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 fetch_detail(session, url):
    r = session.get(url, headers=HEADERS, timeout=30)
    if r.status_code == 200:
        r.encoding = "utf-8"
        return r
    raise RuntimeError(f"HTTP {r.status_code}")


def main():
    import requests
    s = requests.Session()
    s.headers.update(HEADERS)
    conn = None
    if not JSONL_PATH:
        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()
    jsonl_rows = []

    for page in range(1, _PAGES + 1):
        try:
            html_text = fetch_list(s, page)
        except Exception as e:
            print(f"Page {page} fetch error: {e}", flush=True)
            break
        items = parse_list(html_text)
        print(f"Page {page}: found {len(items)} items", flush=True)
        if not items:
            print("Empty page, stop pagination", flush=True)
            break
        for abs_url, title, date in items:
            if abs_url in seen_urls:
                continue
            seen_urls.add(abs_url)
            try:
                r = fetch_detail(s, abs_url)
                dhtml = r.content.decode("utf-8", errors="replace")
            except Exception as e:
                print(f"  detail error {abs_url}: {e}", flush=True)
                skip_count += 1
                continue
            dtitle, ddate, content_html = parse_detail(dhtml, abs_url)
            if not dtitle:
                dtitle = title
            if not ddate:
                ddate = date
            content, has_table, atts = html_to_text(content_html, abs_url)
            plain_len = len(re.sub(r"<[^>]+>", "", content).strip())
            if plain_len < 10:
                print(f"  skip empty: {dtitle[:30]}", flush=True)
                skip_count += 1
                continue
            if JSONL_PATH:
                jsonl_rows.append({
                    "page_url": abs_url, "title": dtitle, "content": content,
                    "publish_date": ddate, "has_table": has_table,
                    "site_name": SITE_NAME, "group_name": GROUP_NAME,
                    "script_name": SCRIPT_NAME,
                })
                print(f"  JSONL + {dtitle[:40]} ({ddate})", flush=True)
                new_count += 1
                continue
            ok = store_record(conn, abs_url, dtitle, content, ddate, has_table)
            if ok:
                new_count += 1
                print(f"  + {dtitle[:40]} ({ddate})", flush=True)
            else:
                skip_count += 1
            time.sleep(0.25)
        time.sleep(0.5)

    if JSONL_PATH:
        with open(JSONL_PATH, "w", encoding="utf-8") as f:
            for row in jsonl_rows:
                f.write(json.dumps(row, ensure_ascii=False) + "\n")
        print(f"JSONL written: {JSONL_PATH} ({len(jsonl_rows)} rows)", flush=True)

    if conn is not None:
        conn.commit()
        conn.close()
    print(f"新增: {new_count}", flush=True)
    print(f"跳过: {skip_count}", flush=True)
    print(f"=== {SITE_NAME} done ===", flush=True)


if __name__ == "__main__":
    main()
