#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
新和县人民政府 - 公示公告 爬虫 (v2 - 改进版)
CMS: 阿克苏地区行政公署网 (Boshan CMS)
列表: dataList JSON 嵌入在HTML中（10页×15条=150条）
      页11+需API(session认证, 暂不可用)
详情: div.bt_content 正文容器
"""

import re
import sys
import json
import time
import subprocess
from datetime import datetime

import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin

# ── 配置 ──────────────────────────────────────────
BASE_URL = "https://www.xjxinhe.gov.cn"
LIST_URL = BASE_URL + "/xwdt/gsgg/index.html"
SITE_NAME = "新和县人民政府"
CATEGORY = "gsgg"
GROUP = "新疆阿克苏"
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.0.0 Safari/537.36",
}

session = requests.Session()
session.headers.update(HEADERS)


# ── 工具函数 ──────────────────────────────────────
def clean_text(text):
    if not text:
        return ""
    text = re.sub(r'(&middot;|&nbsp;|\s)+', ' ', text)
    return text.strip()


def clean_content_html(html_content):
    """提取正文，只处理直接子元素避免重复，表格保持HTML"""
    if not html_content:
        return ""
    soup = BeautifulSoup(html_content, 'html.parser')
    for tag in soup(['script', 'style']):
        tag.decompose()
    parts = []
    for child in soup.children:
        if not child.name:
            continue
        if child.name == 'p':
            t = child.get_text(strip=True)
            if t:
                parts.append(t)
        elif child.name == 'table':
            parts.append(str(child))
        elif child.name in ('div', 'section', 'article'):
            for p in child.find_all('p', recursive=True):
                if p.find_parent('table'):
                    continue
                t = p.get_text(strip=True)
                if t:
                    parts.append(t)
            for tbl in child.find_all('table', recursive=True):
                parts.append(str(tbl))
        elif child.name == 'img':
            src = child.get('src', '')
            alt = child.get('alt', '')
            if src:
                if src.startswith('/'):
                    src = BASE_URL + src
                parts.append(f'![{alt}]({src})')
    return '\n\n'.join(parts)


# ── 列表获取 ──────────────────────────────────────
def fetch_list():
    """从HTML中提取所有嵌入的dataList数据"""
    try:
        resp = session.get(LIST_URL, timeout=30)
        resp.encoding = "utf-8"
        if resp.status_code != 200:
            print(f"列表页 HTTP {resp.status_code}")
            return []
        html = resp.text

        # 查找dataList
        idx = html.find("dataList=")
        if idx < 0:
            print("列表页 未找到dataList")
            return []
        rest = html[idx:]
        depth = 0
        for i, c in enumerate(rest):
            if c == "[":
                depth += 1
            elif c == "]":
                depth -= 1
                if depth == 0:
                    data_str = rest[len("dataList="):i+1]
                    break

        data = json.loads(data_str)
        result = []
        for page_data in data:
            for item in page_data.get("infolist", []):
                title = clean_text(item.get("title", ""))
                url = item.get("url", "")
                date_str = item.get("daytime", "")
                if not url or not title:
                    continue
                result.append({
                    "title": title,
                    "url": url,
                    "date": date_str,
                    "source": item.get("source", ""),
                    "summary": item.get("summary", ""),
                })

        print(f"列表页: 解析到 {len(result)} 条 (嵌入{len(data)}页)")
        return result
    except Exception as e:
        print(f"列表页 异常: {e}")
        return []


# ── 详情页解析 ────────────────────────────────────
def parse_detail(html, url):
    """解析详情页，返回 (title, publish_date, source, content)"""
    soup = BeautifulSoup(html, 'html.parser')
    title = ''
    publish_date = ''
    source = ''
    content = ''

    # 1. 标题
    h1 = soup.find('h1')
    if h1:
        title = clean_text(h1.get_text())

    # 2. 发布日期
    date_match = re.search(r'发布时间[：:]\s*(\d{4}[-/]\d{1,2}[-/]\d{1,2})', html)
    if date_match:
        publish_date = date_match.group(1).replace('/', '-')
    if not publish_date:
        date_match = re.search(r'发布日期[：:]\s*(\d{4}[-/]\d{1,2}[-/]\d{1,2})', html)
        if date_match:
            publish_date = date_match.group(1).replace('/', '-')

    # 3. 来源
    src_match = re.search(r'来源[：:]\s*([^<>\n]+?)(?:</|\s*$)', html)
    if src_match:
        source = src_match.group(1).strip()

    # 4. 正文 div.bt_content
    content_div = soup.find('div', class_='bt_content')
    if not content_div:
        content_div = soup.find('div', class_='content')
    if not content_div:
        content_div = soup.find('div', class_='article-content')
    if content_div:
        content = clean_content_html(str(content_div))

    # 附件链接转为嵌入内容
    if content_div:
        for a_tag in content_div.find_all('a', href=True):
            href = a_tag['href']
            if re.search(r'\.(pdf|doc|docx|xls|xlsx|zip|rar)$', href, re.I):
                a_text = a_tag.get_text(strip=True) or href
                full_url = urljoin(BASE_URL, href) if not href.startswith('http') else href
                link_md = f"[{a_text}]({full_url})"
                # 替换到正文中
                a_tag.replace_with(soup.new_string(f' {link_md} '))
                content = clean_content_html(str(content_div))

    return title, publish_date, source, content


# ── 入库 ──────────────────────────────────────────
def insert_article(item, detail_title, pub_date, source, content):
    """写入gov_raw + FTS gov_search"""
    page_url = item['url'].replace("'", "''")
    title = (detail_title or item['title']).replace("'", "''")
    publish_date = pub_date or item.get('date', '')
    source_name = source or item.get('source', SITE_NAME)
    source_name = source_name.replace("'", "''")
    content_safe = content.replace("'", "''")
    summary = title[:200]
    site_name = SITE_NAME

    sql_raw = f"""INSERT OR IGNORE INTO gov_raw 
        (page_url, title, content, publish_date, source_url, site_name, summary, group_name, industry)
    VALUES 
        ('{page_url}', '{title}', '{content_safe}', '{publish_date}', '{page_url}', '{site_name}', '{summary}', '{GROUP}', '政府公告');
"""
    combined_sql = sql_raw + """SELECT CASE WHEN changes() > 0 THEN last_insert_rowid() ELSE 0 END;
"""
    try:
        result = subprocess.run(
            ['sqlite3', "-cmd", ".timeout 60000", DB_PATH],
            input=combined_sql,
            capture_output=True, text=True, timeout=30
        )
        if result.returncode == 0:
            out = result.stdout.strip()
            try:
                rowid = int(out.strip())
            except ValueError:
                rowid = 0
            if rowid > 0:
                fts_sql = f"""INSERT OR IGNORE INTO gov_search (rowid, title, site_name, summary)
SELECT rowid, title, site_name, summary FROM gov_raw WHERE rowid = {rowid};
"""
                subprocess.run(['sqlite3', "-cmd", ".timeout 60000", DB_PATH], input=fts_sql, capture_output=True, text=True, timeout=30)
                return True
        else:
            if 'UNIQUE constraint' not in result.stderr:
                print(f"    [DB] {result.stderr[:200]}")
    except Exception as e:
        print(f"    [DB] 错误: {e}")
    return False


# ── 主流程 ────────────────────────────────────────
def main():
    print(f"=== 新和县 - 公示公告 爬虫 ===\n")

    # 1. 获取列表
    articles = fetch_list()
    if not articles:
        print("无文章列表")
        return
    print(f"列表总计: {len(articles)} 条")

    # 2. 获取已知URL
    known_urls = set()
    try:
        result = subprocess.run(
            ['sqlite3', "-cmd", ".timeout 60000", DB_PATH, "SELECT page_url FROM gov_raw WHERE page_url LIKE '%xjxinhe.gov.cn%'"],
            capture_output=True, text=True, timeout=30
        )
        if result.returncode == 0 and result.stdout.strip():
            known_urls = set(result.stdout.strip().split('\n'))
    except Exception:
        pass
    print(f"已知URL: {len(known_urls)}")

    # 3. 遍历详情
    added = 0
    skipped = 0
    for i, a in enumerate(articles):
        url = a['url']
        if url in known_urls:
            skipped += 1
            continue

        print(f"  [{i+1}/{len(articles)}] {a['title'][:50]}...", end=' ')
        try:
            r = session.get(url, timeout=30)
            r.encoding = 'utf-8'
        except Exception as e:
            print(f"ERR {e}")
            time.sleep(1)
            continue

        d_title, d_date, d_source, content = parse_detail(r.text, url)
        if not content.strip():
            print("SKIP 无正文")
            skipped += 1
            continue

        if insert_article(a, d_title, d_date, d_source, content):
            added += 1
            print(f"OK ({len(content.strip())}字)")
        else:
            skipped += 1
            print("SKIP (重复或入库失败)")

        time.sleep(0.3)

    print(f"\n结果: 新增 {added} 条, 跳过 {skipped} 条")
    print("=== 完成 ===")


if __name__ == '__main__':
    main()
