From 99a06dcba05b387ceffbc7eabf69413ff6586322 Mon Sep 17 00:00:00 2001 From: thsrite Date: Tue, 12 Sep 2023 10:09:17 +0800 Subject: [PATCH] =?UTF-8?q?fix=20rss=E8=BF=87=E6=9C=9F=EF=BC=8C=E5=B0=9D?= =?UTF-8?q?=E8=AF=95=E4=BF=9D=E7=95=99=E5=8E=9F=E9=85=8D=E7=BD=AE=E7=94=9F?= =?UTF-8?q?=E6=88=90=E6=96=B0=E7=9A=84rss=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/torrents.py | 44 ++++++++++++++++++++++++++++++++++++++++++- app/helper/rss.py | 11 +++++++++-- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/app/chain/torrents.py b/app/chain/torrents.py index 22f95ac7..ceee69d6 100644 --- a/app/chain/torrents.py +++ b/app/chain/torrents.py @@ -1,3 +1,4 @@ +import re from typing import Dict, List, Union from cachetools import cached, TTLCache @@ -7,12 +8,13 @@ from app.core.config import settings from app.core.context import TorrentInfo, Context, MediaInfo from app.core.metainfo import MetaInfo from app.db import SessionFactory +from app.db.site_oper import SiteOper from app.db.systemconfig_oper import SystemConfigOper from app.helper.rss import RssHelper from app.helper.sites import SitesHelper from app.log import logger from app.schemas import Notification -from app.schemas.types import SystemConfigKey, MessageChannel +from app.schemas.types import SystemConfigKey, MessageChannel, NotificationType from app.utils.singleton import Singleton from app.utils.string import StringUtils @@ -29,6 +31,7 @@ class TorrentsChain(ChainBase, metaclass=Singleton): self._db = SessionFactory() super().__init__(self._db) self.siteshelper = SitesHelper() + self.siteoper = SiteOper(self._db) self.rsshelper = RssHelper() self.systemconfig = SystemConfigOper() @@ -85,6 +88,10 @@ class TorrentsChain(ChainBase, metaclass=Singleton): logger.error(f'站点 {domain} 未配置RSS地址!') return [] rss_items = self.rsshelper.parse(site.get("rss"), True if site.get("proxy") else False) + if rss_items is None: + # rss过期,尝试保留原配置生成新的rss + self.__renew_rss_url(domain=domain, site=site) + return [] if not rss_items: logger.error(f'站点 {domain} 未获取到RSS数据!') return [] @@ -187,3 +194,38 @@ class TorrentsChain(ChainBase, metaclass=Singleton): self.save_cache(torrents_cache, self._rss_file) # 返回 return torrents_cache + + def __renew_rss_url(self, domain: str, site: dict): + """ + 保留原配置生成新的rss地址 + """ + try: + # RSS链接过期 + logger.error(f"站点 {domain} RSS链接已过期,正在尝试自动获取!") + # 自动生成rss地址 + rss_url, errmsg = self.rsshelper.get_rss_link( + url=site.get("url"), + cookie=site.get("cookie"), + ua=site.get("ua") or settings.USER_AGENT, + proxy=True if site.get("proxy") else False + ) + if rss_url: + # 获取新的日期的passkey + match = re.search(r'passkey=([a-zA-Z0-9]+)', rss_url) + if match: + new_passkey = match.group(1) + # 获取过期rss除去passkey部分 + old_rss = re.sub(r'&passkey=.*', '&passkey=', site.get("rss")) + new_rss = old_rss + new_passkey + logger.info(f"更新站点 {domain} RSS地址 ...") + self.siteoper.update_rss(domain=domain, rss=new_rss) + else: + # 发送消息 + self.post_message( + Notification(mtype=NotificationType.SiteMessage, title=f"站点 {domain} RSS链接已过期")) + else: + self.post_message( + Notification(mtype=NotificationType.SiteMessage, title=f"站点 {domain} RSS链接已过期")) + except Exception as e: + print(str(e)) + self.post_message(Notification(mtype=NotificationType.SiteMessage, title=f"站点 {domain} RSS链接已过期")) diff --git a/app/helper/rss.py b/app/helper/rss.py index 7285edcb..fa5d6882 100644 --- a/app/helper/rss.py +++ b/app/helper/rss.py @@ -1,5 +1,5 @@ import xml.dom.minidom -from typing import List, Tuple +from typing import List, Tuple, Optional from urllib.parse import urljoin from lxml import etree @@ -221,7 +221,7 @@ class RssHelper: } @staticmethod - def parse(url, proxy: bool = False) -> List[dict]: + def parse(url, proxy: bool = False) -> Optional[List[dict], None]: """ 解析RSS订阅URL,获取RSS中的种子信息 :param url: RSS地址 @@ -288,6 +288,13 @@ class RssHelper: continue except Exception as e2: print(str(e2)) + # RSS过期 观众RSS 链接已过期,您需要获得一个新的! pthome RSS Link has expired, You need to get a new one! + _rss_expired_msg = [ + "RSS 链接已过期, 您需要获得一个新的!", + "RSS Link has expired, You need to get a new one!" + ] + if ret_xml in _rss_expired_msg: + return None return ret_array def get_rss_link(self, url: str, cookie: str, ua: str, proxy: bool = False) -> Tuple[str, str]: