文件预览

service.py

查看 1688 Shop Operate 技能包中的文件内容。

文件内容

scripts/capabilities/get_transaction_ranking/service.py

#!/usr/bin/env python3
"""行业交易排名数据查询服务"""

from _http import api_post
from _errors import ServiceError

VALID_DATE_TYPES = {"RECENT_7", "RECENT_30"}
VALID_DEVICES = {"ALL", "PC", "WIRELESS"}

def get_transaction_ranking(date_type: str = "RECENT_7", device: str = "ALL") -> dict:
    """获取行业交易排名数据

    Args:
        date_type: 日期类型,仅支持 RECENT_7/RECENT_30(不支持 RECENT_1)
        device:    设备类型 ALL/PC/WIRELESS

    Returns:
        API 响应 data 字段,包含行业排名、百分位、标杆数据
    """
    if date_type not in VALID_DATE_TYPES:
        raise ValueError(
            f"date_type 必须为 {', '.join(sorted(VALID_DATE_TYPES))} 之一(不支持 RECENT_1),"
            f"当前值: {date_type}"
        )
    if device not in VALID_DEVICES:
        raise ValueError(f"device 必须为 {', '.join(sorted(VALID_DEVICES))} 之一,当前值: {device}")

    data = api_post("/api/get_transaction_ranking/1.0.0", {
        "date_type": date_type,
        "device": device,
    })

    if not isinstance(data, dict):
        raise ServiceError("格式异常,请稍后重试")

    return data