feat: 添加中国节假日感知定时功能
- 新增 chinese_holidays 表,通过 timor.tech API 同步节假日数据 - 修正 holiday 字段解读:holiday=true → 休息日,holiday=false → 调休工作日 - 工作日 8:00-22:00 每小时爬取,周末/节假日/夜间自动跳过 - 新增 /api/v1/holidays/sync 和 /api/v1/holidays/today 接口 - 企微菜单新增「同步节假日」按钮,支持手动触发同步
This commit is contained in:
+21
-8
@@ -1,30 +1,43 @@
|
||||
import logging
|
||||
from datetime import datetime, time
|
||||
from datetime import datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||
from apscheduler.triggers.cron import CronTrigger
|
||||
|
||||
from app.api.deps import get_crawl_service
|
||||
from app.api.deps import get_db, get_crawl_service
|
||||
from app.config import settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
scheduler = AsyncIOScheduler()
|
||||
|
||||
NIGHT_START = time(22, 0)
|
||||
NIGHT_END = time(6, 0)
|
||||
NIGHT_START = 22 # 22:00
|
||||
NIGHT_END = 8 # 08:00
|
||||
TZ = ZoneInfo("Asia/Shanghai")
|
||||
|
||||
|
||||
def _is_night_time() -> bool:
|
||||
"""22:00 ~ 次日 06:00 夜间时段"""
|
||||
current = datetime.now(TZ).time()
|
||||
"""22:00 ~ 次日 08:00 夜间时段"""
|
||||
current = datetime.now(TZ).hour
|
||||
return current >= NIGHT_START or current < NIGHT_END
|
||||
|
||||
|
||||
async def scheduled_crawl():
|
||||
async def _should_skip() -> bool:
|
||||
"""检查是否应该跳过爬取"""
|
||||
if _is_night_time():
|
||||
logger.info("夜间时段 (22:00-06:00),跳过爬取")
|
||||
logger.info("夜间时段 (22:00-08:00),跳过爬取")
|
||||
return True
|
||||
|
||||
from app.services.holiday_service import is_workday
|
||||
async for db in get_db():
|
||||
if not await is_workday(db):
|
||||
logger.info("非工作日,跳过爬取")
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
async def scheduled_crawl():
|
||||
if await _should_skip():
|
||||
return
|
||||
|
||||
logger.info("开始定时爬取任务")
|
||||
|
||||
Reference in New Issue
Block a user