From 61281cca0243e80d9b68bf82f7e9f73a8f1d549b Mon Sep 17 00:00:00 2001 From: jxxghp Date: Mon, 1 Jan 2024 10:22:18 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=85=8D=E8=B4=B9=E5=89=A9?= =?UTF-8?q?=E4=BD=99=E6=97=B6=E9=97=B4=20&&=20HR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/download.py | 2 ++ app/core/context.py | 2 ++ app/modules/indexer/spider.py | 33 +++++++++++++++++++++++++++++---- app/schemas/context.py | 2 ++ app/utils/string.py | 24 ++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 4 deletions(-) diff --git a/app/chain/download.py b/app/chain/download.py index 179809d7..4cc7fe9e 100644 --- a/app/chain/download.py +++ b/app/chain/download.py @@ -55,6 +55,8 @@ class DownloadChain(ChainBase): msg_text = f"{msg_text}\n种子:{torrent.title}" if torrent.pubdate: msg_text = f"{msg_text}\n发布时间:{torrent.pubdate}" + if torrent.freedate: + msg_text = f"{msg_text}\n免费时间:{StringUtils.diff_time_str(torrent.freedate)}" if torrent.seeders: msg_text = f"{msg_text}\n做种数:{torrent.seeders}" if torrent.uploadvolumefactor and torrent.downloadvolumefactor: diff --git a/app/core/context.py b/app/core/context.py index bfc2054d..ce55213d 100644 --- a/app/core/context.py +++ b/app/core/context.py @@ -44,6 +44,8 @@ class TorrentInfo: pubdate: str = None # 已过时间 date_elapsed: str = None + # 免费截止时间 + freedate: str = None # 上传因子 uploadvolumefactor: float = None # 下载因子 diff --git a/app/modules/indexer/spider.py b/app/modules/indexer/spider.py index 8c7e8f2b..dbbb4057 100644 --- a/app/modules/indexer/spider.py +++ b/app/modules/indexer/spider.py @@ -12,9 +12,9 @@ from ruamel.yaml import CommentedMap from app.core.config import settings from app.helper.browser import PlaywrightHelper from app.log import logger +from app.schemas.types import MediaType from app.utils.http import RequestUtils from app.utils.string import StringUtils -from app.schemas.types import MediaType class TorrentSpider: @@ -547,6 +547,29 @@ class TorrentSpider: else: self.torrents_info['labels'] = [] + def __get_free_date(self, torrent): + # free date + if 'free_date' not in self.fields: + return + selector = self.fields.get('free_date', {}) + free_date = torrent(selector.get('selector', '')).clone() + self.__remove(free_date, selector) + items = self.__attribute_or_text(free_date, selector) + self.torrents_info['freedate'] = self.__index(items, selector) + self.torrents_info['freedate'] = self.__filter_text(self.torrents_info.get('freedate'), + selector.get('filters')) + + def __get_hit_and_run(self, torrent): + # hitandrun + if 'hr' not in self.fields: + return + selector = self.fields.get('hr', {}) + hit_and_run = torrent(selector.get('selector', '')) + if hit_and_run: + self.torrents_info['hit_and_run'] = True + else: + self.torrents_info['hit_and_run'] = False + def get_info(self, torrent) -> dict: """ 解析单条种子数据 @@ -566,13 +589,15 @@ class TorrentSpider: self.__get_uploadvolumefactor(torrent) self.__get_pubdate(torrent) self.__get_date_elapsed(torrent) + self.__get_free_date(torrent) self.__get_labels(torrent) + self.__get_hit_and_run(torrent) except Exception as err: logger.error("%s 搜索出现错误:%s" % (self.indexername, str(err))) return self.torrents_info @staticmethod - def __filter_text(text, filters): + def __filter_text(text: str, filters: list): """ 对文件进行处理 """ @@ -613,7 +638,7 @@ class TorrentSpider: item.remove(v) @staticmethod - def __attribute_or_text(item, selector): + def __attribute_or_text(item, selector: dict): if not selector: return item if not item: @@ -625,7 +650,7 @@ class TorrentSpider: return items @staticmethod - def __index(items, selector): + def __index(items: list, selector: dict): if not items: return None if selector: diff --git a/app/schemas/context.py b/app/schemas/context.py index d0a54266..5bc65975 100644 --- a/app/schemas/context.py +++ b/app/schemas/context.py @@ -196,6 +196,8 @@ class TorrentInfo(BaseModel): pubdate: Optional[str] = None # 已过时间 date_elapsed: Optional[str] = None + # 免费截止时间 + freedate: Optional[str] = None # 上传因子 uploadvolumefactor: Optional[float] = None # 下载因子 diff --git a/app/utils/string.py b/app/utils/string.py index c2a75067..3bde6014 100644 --- a/app/utils/string.py +++ b/app/utils/string.py @@ -711,3 +711,27 @@ class StringUtils: return -1 else: return 0 + + @staticmethod + def diff_time_str(time_str: str): + """ + 输入YYYY-MM-DD HH24:MI:SS 格式的时间字符串,返回距离现在的剩余时间:xx天xx小时xx分钟 + """ + if not time_str: + return '' + try: + time_obj = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S') + except ValueError: + return '' + now = datetime.datetime.now() + diff = time_obj - now + diff_seconds = diff.seconds + diff_days = diff.days + diff_hours = diff_seconds // 3600 + diff_minutes = (diff_seconds % 3600) // 60 + if diff_days > 0: + return f'{diff_days}天{diff_hours}小时{diff_minutes}分钟' + elif diff_hours > 0: + return f'{diff_hours}小时{diff_minutes}分钟' + else: + return f'{diff_minutes}分钟'