节假日接口改用国内无 Cloudflare 的 apihubs.cn
1. 主接口改为 api.apihubs.cn(国内节点、无 Cloudflare),按 workday/weekend 标志解析法定放假与调休补班 2. 备用接口改为节假曰 API(仅主接口失败时使用),移除 timor.tech 3. 缓存、证书回退与手动校历逻辑不变
This commit is contained in:
1 parent
c87f62baf7
commit
2d3513b246
1 file changed
+37
-26
@@ -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}")
|
||||
|
||||
|
||||
Reference in new issue
Block a user