From 2d3513b246177a474317ec67871fb7cf9a66a985 Mon Sep 17 00:00:00 2001 From: Canglan Date: Sun, 25 Oct 2026 16:49:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8A=82=E5=81=87=E6=97=A5=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=94=B9=E7=94=A8=E5=9B=BD=E5=86=85=E6=97=A0=20Cloudflare=20?= =?UTF-8?q?=E7=9A=84=20apihubs.cn?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 主接口改为 api.apihubs.cn(国内节点、无 Cloudflare),按 workday/weekend 标志解析法定放假与调休补班 2. 备用接口改为节假曰 API(仅主接口失败时使用),移除 timor.tech 3. 缓存、证书回退与手动校历逻辑不变 --- backend/services/holiday_service.py | 63 +++++++++++++++++------------ 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/backend/services/holiday_service.py b/backend/services/holiday_service.py index f0a9ee6..f655ea7 100644 --- a/backend/services/holiday_service.py +++ b/backend/services/holiday_service.py @@ -24,10 +24,10 @@ from utils.logger import get_logger logger = get_logger(__name__) -# 国内节假日接口(节假曰 API):{"YYYY-MM-DD": {"name": "...", "isOffDay": true/false}} -HOLIDAY_API_DOMESTIC = 'https://api.jiejiariapi.com/v1/holidays/{year}' -# 备用接口(timor.tech) -HOLIDAY_API_FALLBACK = 'https://timor.tech/api/holiday/year/{year}' +# 国内节假日接口(apihubs.cn,国内节点、无 Cloudflare) +HOLIDAY_API_DOMESTIC = 'https://api.apihubs.cn/holiday/get?year={year}&size=400' +# 备用接口(节假曰 API),仅在主接口不可用时使用 +HOLIDAY_API_FALLBACK = 'https://api.jiejiariapi.com/v1/holidays/{year}' CACHE_TTL = 12 * 3600 FAIL_CACHE_TTL = 600 REQUEST_TIMEOUT = 6 @@ -52,10 +52,39 @@ def _urlopen_json(url: str) -> Any: def _fetch_domestic(year: int) -> Dict[str, Any]: - """国内接口:节假曰 API(放假 + 调休补班)""" + """国内接口(apihubs.cn):按 workday/weekend 标志解析放假与调休补班""" payload = _urlopen_json(HOLIDAY_API_DOMESTIC.format(year=year)) + items = (payload.get('data') or {}).get('list') or [] + if not items: + raise ValueError("国内接口未返回数据") + + holidays: Dict[str, str] = {} + workdays: Dict[str, str] = {} + for item in items: + try: + date_int = int(item.get('date') or 0) + except (TypeError, ValueError): + continue + if date_int < 20000101: + continue + s = str(date_int) + day = f'{s[0:4]}-{s[4:6]}-{s[6:8]}' + # workday: 1=上班, 2=休息;weekend: 1=周六/日, 2=工作日 + if item.get('workday') == 2 and item.get('weekend') == 2: + holidays[day] = '法定节假日' + elif item.get('workday') == 1 and item.get('weekend') == 1: + workdays[day] = '调休补班' + + if not holidays: + raise ValueError("国内接口未解析到节假日数据") + return {'holidays': holidays, 'workdays': workdays, 'api_ok': True} + + +def _fetch_fallback(year: int) -> Dict[str, Any]: + """备用接口(节假曰 API):放假 + 调休补班""" + payload = _urlopen_json(HOLIDAY_API_FALLBACK.format(year=year)) if not isinstance(payload, dict) or not payload: - raise ValueError("国内接口返回数据为空") + raise ValueError("备用接口返回数据为空") holidays: Dict[str, str] = {} workdays: Dict[str, str] = {} @@ -70,25 +99,7 @@ def _fetch_domestic(year: int) -> Dict[str, Any]: workdays[day] = name if not holidays: - raise ValueError("国内接口未返回节假日数据") - return {'holidays': holidays, 'workdays': workdays, 'api_ok': True} - - -def _fetch_timor(year: int) -> Dict[str, Any]: - """备用接口:timor.tech""" - payload = _urlopen_json(HOLIDAY_API_FALLBACK.format(year=year)) - if payload.get('code') != 0: - raise ValueError(f"备用接口返回错误码: {payload.get('code')}") - - holidays: Dict[str, str] = {} - workdays: Dict[str, str] = {} - for day, info in (payload.get('holiday') or {}).items(): - d = str(info.get('date') or f'{year}-{day}')[:10] - if info.get('holiday'): - holidays[d] = info.get('name') or '节假日' - else: - workdays[d] = info.get('name') or '调休补班' - + raise ValueError("备用接口未返回节假日数据") return {'holidays': holidays, 'workdays': workdays, 'api_ok': True} @@ -100,7 +111,7 @@ def _fetch_year_sync(year: int) -> Dict[str, Any]: logger.warning(f"国内节假日接口获取失败({year}),尝试备用接口: {e}") try: - return _fetch_timor(year) + return _fetch_fallback(year) except Exception as e: logger.warning(f"备用节假日接口获取失败({year}): {e}")