#!/usr/bin/env python3
"""Test QZ API with more-button params"""
import requests, json, re

url = "https://www.qz.gov.cn/api-gateway/jpaas-publish-server/front/page/build/unit"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
    "Accept": "*/*",
    "Referer": "https://www.qz.gov.cn/col/col1229039395/index.html?number=A09",
}

# Default call
params1 = {
    "parseType": "bulidstatic",
    "webId": "3084",
    "tplSetId": "i58lqHVn2cokOgipAEjQF",
    "pageType": "column",
    "tagId": "组配分类list",
    "pageId": "1229039395",
}
r1 = requests.get(url, params=params1, headers=headers, timeout=15)
html1 = r1.json()["data"]["html"]
print("=== Default call ===")
print(f"Size: {len(html1)}")
spans = re.findall(r'<span class="fr">([^<]+)</span>', html1)
print(f"Items: {len(spans)}")
counts = re.findall(r'count="(\d+)"', html1)
print(f"count: {counts}")
pages = re.findall(r'pageNo="(\d+)"', html1)
print(f"pageNo: {pages}")

# More-button call with paramJson
search = {"xxgkId": "", "xxgkType": "", "className": ""}
paramJson = {"pageNo": 1, "pageSize": 15, "search": json.dumps(search, ensure_ascii=False)}
params2 = dict(params1)
params2["paramJson"] = json.dumps(paramJson, ensure_ascii=False)
r2 = requests.get(url, params=params2, headers=headers, timeout=15)
html2 = r2.json()["data"]["html"]
print("\n=== With paramJson ===")
print(f"Size: {len(html2)}")
spans2 = re.findall(r'<span class="fr">([^<]+)</span>', html2)
print(f"Items: {len(spans2)}")
counts2 = re.findall(r'count="(\d+)"', html2)
print(f"count: {counts2}")
pages2 = re.findall(r'pageNo="(\d+)"', html2)
print(f"pageNo: {pages2}")

# Check if scrolling works (next batch)
paramJson3 = {"pageNo": 2, "pageSize": 15, "search": json.dumps(search, ensure_ascii=False)}
params3 = dict(params1)
params3["paramJson"] = json.dumps(paramJson3, ensure_ascii=False)
r3 = requests.get(url, params=params3, headers=headers, timeout=15)
html3 = r3.json()["data"]["html"]
spans3 = re.findall(r'<span class="fr">([^<]+)</span>', html3)
print(f"\n=== pageNo=2 ===\nItems: {len(spans3)}")
