#!/usr/bin/env python3
import requests, json

# Test the API for the environmental section
API_URL = "https://www.jinjiang.gov.cn/ssp/search/api/v2/external"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
    "Content-Type": "application/json",
}

# Test with the parameters from the page
body = {
    "siteId": "000000008a939277018a9bf9d5960057",
    "apiName": "WCMDOCAP",
    "pageSize": 10,
    "page": 1,
    "sortField": "docreltime desc",
    "filter": {
        "docstatus": [{"eq": 10}],
        "chnlid": [{"eq": "50128"}],
    }
}

r = requests.post(API_URL, json=body, headers=headers, timeout=30)
print("Status:", r.status_code)
try:
    data = r.json()
    print("Code:", data.get("num"))
    print("Total:", data.get("data", {}).get("total"))
    results = data.get("data", {}).get("result", [])
    print("Items:", len(results))
    for item in results[:3]:
        print()
        print("  doctitle:", item.get("doctitle", "")[:60])
        print("  docpuburl:", item.get("docpuburl", ""))
        print("  pubdate:", item.get("pubdate"))
        print("  docreltime:", item.get("docreltime"))
        print("  fileno:", item.get("fileno", ""))
        print("  doccontent:", (item.get("doccontent") or "")[:100])
except Exception as e:
    print("Parse error:", e)
    print("Raw:", r.text[:500])
