#!/usr/bin/env python3
"""武宣县人民政府-通知公告"""
import re, sys, os, json, time, requests
DB_PATH = os.environ.get("DB_PATH", "/root/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",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.9",
}
SITE_NAME = "武宣县人民政府-通知公告"
GROUP = "广西来宾"
PER_PAGE = 6
MAX_PAGES = 5
BASE_URL = "http://www.wuxuan.gov.cn"
LIST_URL = "http://www.wuxuan.gov.cn/tzgg/"

def fetch(url, encoding="utf-8"):
    r = requests.get(url, headers=HEADERS, timeout=30, allow_redirects=True)
    r.encoding = encoding
    return r.text

def resolve_url(rel_url):
    if rel_url.startswith("http"):
        return rel_url
    if rel_url.startswith("./"):
        return BASE_URL + "/tzgg/" + rel_url[2:]
    if rel_url.startswith("/"):
        return BASE_URL + rel_url
    return BASE_URL + "/" + rel_url

def extract_list(html):
    """Extract (title, url, date) from list page"""
    items = []
    pattern = r'<li><span>(\d{4}-\d{1,2}-\d{1,2})</span><a\s+href="([^"]+)"[^>]*title="([^"]*)"[^>]*>'
    for m in re.finditer(pattern, html):
        date = m.group(1).strip()
        rel_url = m.group(2).strip()
        title = m.group(3).strip()
        items.append((title, rel_url, date))
    # Fallback: try matching without title attribute
    if not items:
        pattern2 = r'<li><span>(\d{4}-\d{1,2}-\d{1,2})</span><a\s+href="([^"]+)"[^>]*>([^<]+)</a>'
        for m in re.finditer(pattern2, html):
            date = m.group(1).strip()
            rel_url = m.group(2).strip()
            title = m.group(3).strip()
            items.append((title, rel_url, date))
    return items

def extract_detail(html):
    """Extract body content from detail page"""
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(html, 'html.parser')

    # Title from <h1>
    h1 = soup.find('h1')
    title = h1.get_text(strip=True) if h1 else ""

    # Date from article-inf-left
    date = ""
    inf = soup.find('div', class_='article-inf-left')
    if inf:
        text = inf.get_text(strip=True)
        dm = re.search(r'(\d{4}-\d{1,2}-\d{1,2})', text)
        if dm:
            date = dm.group(1)

    # Body from TRS_UEDITOR
    body = ""
    te = soup.find('div', class_='TRS_UEDITOR')
    if te:
        paras = [p.get_text(separator='\n', strip=True) for p in te.find_all('p') if p.get_text(strip=True)]
        body = '\n\n'.join(paras)

    return title, body, date

def save_to_db(items, site_name, group):
    import sqlite3
    conn = sqlite3.connect(DB_PATH, timeout=60)
    c = conn.cursor()
    inserted = 0
    for title, url, date, body in items:
        try:
            c.execute("""INSERT OR REPLACE INTO gov_raw (page_url, site_name, group_name, title, publish_date, summary, content, script_name) VALUES (?, ?, ?, ?, ?, ?, ?, 'crawl_wuxuan.py')""",
                (url, site_name, group, title, date, body, body))
            inserted += 1
        except Exception as e:
            print("DB error: {} - {}".format(url, e))
    conn.commit()
    conn.close()
    return inserted

def main():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('--max-pages', type=int, default=MAX_PAGES)
    args = parser.parse_args()
    max_pages = args.max_pages

    all_items = []

    # Page 1
    html = fetch(LIST_URL)
    items = extract_list(html)
    all_items.extend(items)
    print("Page 1: {} items".format(len(items)))

    # Pages 2+
    for page in range(2, max_pages + 1):
        url = "{}/index_{}.shtml".format(LIST_URL.rstrip('/'), page - 1)
        try:
            html = fetch(url)
            items = extract_list(html)
            if not items:
                print("Page {}: empty, stopping".format(page))
                break
            all_items.extend(items)
            print("Page {}: {} items".format(page, len(items)))
        except Exception as e:
            print("Page {} error: {}".format(page, e))
            break
        time.sleep(0.5)

    print("\nTotal list items: {}".format(len(all_items)))

    # Fetch details
    detail_items = []
    failed = 0
    for i, (title, rel_url, date) in enumerate(all_items):
        url = resolve_url(rel_url)
        try:
            html = fetch(url)
            d_title, body, d_date = extract_detail(html)
            if not d_date:
                d_date = date
            if not d_title:
                d_title = title
            detail_items.append((d_title, url, d_date, body))
            if (i + 1) % 10 == 0:
                print("  progress: {}/{}".format(i + 1, len(all_items)))
        except Exception as e:
            print("  FAILED: {} - {}".format(url, e))
            failed += 1
        time.sleep(0.3)

    print("\nDetails fetched: {}, failed: {}".format(len(detail_items), failed))

    # Save to DB
    inserted = save_to_db(detail_items, SITE_NAME, GROUP)
    print("Inserted: {}".format(inserted))

    # Summary
    timeout_count = 0
    error_count = failed
    abnormal_count = 0
    print("\n=== Summary ===")
    print("{} | {} | {} | {} | {}".format(SITE_NAME, inserted, timeout_count, error_count, abnormal_count))

if __name__ == "__main__":
    main()
