#!/usr/bin/env python3
"""Find xiangfuqu UCAP API details"""
import requests, re, json, sys
sys.stdout.reconfigure(encoding='utf-8')

headers = {'User-Agent': 'Mozilla/5.0', 'Referer': 'https://www.xiangfuqu.gov.cn/kfsxfqwz/c00511/pc/list.html'}
base = 'https://www.xiangfuqu.gov.cn'

# Try UCAP API with proper headers
api_url = f'{base}/ucap/website/article/list'
payload = {
    'page': 1,
    'pageSize': 20,
    'channelId': '939929095397969920',
    'websiteId': '1873534524856287232'
}
r = requests.post(api_url, json=payload, headers={**headers, 'Content-Type': 'application/json', 'Accept': 'application/json'}, timeout=15)
print(f'POST /ucap/website/article/list: HTTP {r.status_code}')
if r.status_code == 200:
    print(r.text[:500])
else:
    print(r.text[:300])

# Try GET with query params
r2 = requests.get(f'{base}/ucap/website/article/list', params=payload, headers=headers, timeout=15)
print(f'\nGET /ucap/website/article/list: HTTP {r2.status_code}')
if r2.status_code == 200:
    print(r2.text[:500])

# Check the content page for a specific article
# Look at the page HTML to find a sample article URL
sample_url = f'{base}/kfsxfqwz/c00511/pc/content/content_2013787546566324224.html'
r3 = requests.get(sample_url, headers=headers, timeout=15)
print(f'\nSample content page: HTTP {r3.status_code}')
if r3.status_code == 200:
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(r3.text, 'html.parser')
    print(f'  Title: {soup.title.get_text(strip=True)[:60] if soup.title else "N/A"}')
    print(f'  Size: {len(r3.text)}')
