From ed5dec1b0f8b75b5db4466854a51e378f6aec735 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Tue, 5 Sep 2023 12:39:01 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E7=A7=8D=E5=AD=90=E5=88=B7=E6=96=B0?= =?UTF-8?q?=E9=A2=91=E7=8E=87=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/torrents.py | 11 +++++++++++ app/utils/timer.py | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/app/chain/torrents.py b/app/chain/torrents.py index e96173d8..11885a02 100644 --- a/app/chain/torrents.py +++ b/app/chain/torrents.py @@ -1,3 +1,4 @@ +from datetime import datetime from typing import Dict, List, Union from requests import Session @@ -12,6 +13,7 @@ from app.log import logger from app.schemas import Notification from app.schemas.types import SystemConfigKey, MessageChannel from app.utils.string import StringUtils +from app.utils.timer import TimerUtils class TorrentsChain(ChainBase): @@ -20,6 +22,7 @@ class TorrentsChain(ChainBase): """ _cache_file = "__torrents_cache__" + _last_refresh_time = None def __init__(self, db: Session = None): super().__init__(db) @@ -47,6 +50,14 @@ class TorrentsChain(ChainBase): """ 刷新站点最新资源 """ + # 控制刷新频率不能小于10分钟 + if self._last_refresh_time and TimerUtils.diff_minutes(self._last_refresh_time) < 10: + logger.warn(f'种子刷新频率过快,跳过本次刷新') + return self.get_torrents() + + # 记录刷新时间 + self._last_refresh_time = datetime.now() + # 读取缓存 torrents_cache = self.get_torrents() diff --git a/app/utils/timer.py b/app/utils/timer.py index 95b2fe5c..49819e76 100644 --- a/app/utils/timer.py +++ b/app/utils/timer.py @@ -64,3 +64,13 @@ class TimerUtils: time_difference_string += f"{minutes}分钟" return time_difference_string + + @staticmethod + def diff_minutes(input_datetime: datetime) -> int: + """ + 计算当前时间与输入时间的分钟差 + """ + if not input_datetime: + return 0 + time_difference = datetime.datetime.now() - input_datetime + return int(time_difference.total_seconds() / 60)