52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
"""爬虫工具函数"""
|
||
|
||
import logging
|
||
from datetime import datetime, timedelta
|
||
from pathlib import Path
|
||
|
||
import yaml
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
# 默认日切分小时(凌晨)
|
||
DEFAULT_CUTOFF_HOUR = 6
|
||
|
||
|
||
def _load_cutoff_hour() -> int:
|
||
"""从 system.yaml 读取日切分小时"""
|
||
config_path = Path("configs/system.yaml")
|
||
if config_path.exists():
|
||
try:
|
||
with open(config_path, encoding="utf-8") as f:
|
||
raw = yaml.safe_load(f)
|
||
return int(raw.get("schedule", {}).get("day_cutoff_hour", DEFAULT_CUTOFF_HOUR))
|
||
except Exception:
|
||
pass
|
||
return DEFAULT_CUTOFF_HOUR
|
||
|
||
|
||
def get_news_day(now: datetime | None = None) -> str:
|
||
"""获取当前新闻日(YYYYMMDD)
|
||
|
||
规则:当天 06:00 到次日 05:59 属于同一个新闻日。
|
||
例如 2026-06-19 04:00 → "20260618",07:00 → "20260619"
|
||
|
||
Args:
|
||
now: 参考时间,默认当前时间
|
||
|
||
Returns:
|
||
新闻日字符串 YYYYMMDD
|
||
"""
|
||
if now is None:
|
||
now = datetime.now()
|
||
|
||
cutoff = _load_cutoff_hour()
|
||
|
||
if now.hour < cutoff:
|
||
# 凌晨 0:00 - 5:59,属于前一天
|
||
news_date = now - timedelta(days=1)
|
||
else:
|
||
news_date = now
|
||
|
||
return news_date.strftime("%Y%m%d")
|