支持馒头站点的字幕下载

This commit is contained in:
景大侠
2025-12-29 22:43:07 +08:00
parent 41ddf77a5b
commit 7f33b0b1b8
2 changed files with 96 additions and 73 deletions
+2 -2
View File
@@ -267,9 +267,9 @@ class MTorrentSpider:
base64_str = base64.b64encode(json.dumps(params).encode('utf-8')).decode('utf-8') base64_str = base64.b64encode(json.dumps(params).encode('utf-8')).decode('utf-8')
return f"[{base64_str}]{url}" return f"[{base64_str}]{url}"
def parse_subtitle_links(self, page_url: str) -> List[str]: def get_subtitle_links(self, page_url: str) -> List[str]:
""" """
解析指定页面的字幕下载链接 获取指定页面的字幕下载链接
:param page_url: 种子详情页网址 :param page_url: 种子详情页网址
:type page_url: str :type page_url: str
+54 -31
View File
@@ -8,9 +8,13 @@ from lxml import etree
from app.chain.storage import StorageChain from app.chain.storage import StorageChain
from app.core.config import settings from app.core.config import settings
from app.core.context import Context from app.core.context import Context
from app.db.site_oper import SiteOper
from app.helper.sites import SitesHelper # noqa
from app.helper.torrent import TorrentHelper from app.helper.torrent import TorrentHelper
from app.log import logger from app.log import logger
from app.modules import _ModuleBase from app.modules import _ModuleBase
from app.modules.indexer.spider.mtorrent import MTorrentSpider
from app.schemas import TorrentInfo
from app.schemas.file import FileURI from app.schemas.file import FileURI
from app.schemas.types import ModuleType, OtherModulesType from app.schemas.types import ModuleType, OtherModulesType
from app.utils.http import RequestUtils from app.utils.http import RequestUtils
@@ -65,6 +69,52 @@ class SubtitleModule(_ModuleBase):
def test(self): def test(self):
pass pass
def _get_subtitle_links(self, torrent: TorrentInfo):
"""
获取字幕链接
"""
# API请求方式的站点需要特殊处理
if torrent.site is not None:
site = SiteOper().get(torrent.site)
if indexer := SitesHelper().get_indexer(site.domain):
if indexer.get("parser") == "mTorrent":
return MTorrentSpider(indexer).get_subtitle_links(
torrent.page_url
)
# TODO 其它采用API访问的站点
# 普通站点通过解析网站代码的方式获取
request = RequestUtils(cookies=torrent.site_cookie, ua=torrent.site_ua)
res = request.get_res(torrent.page_url)
if res and res.status_code == 200:
if not res.text:
logger.warn(f"读取页面代码失败:{torrent.page_url}")
return []
html = etree.HTML(res.text)
try:
sublink_list = []
for xpath in self._SITE_SUBTITLE_XPATH:
sublinks = html.xpath(xpath)
if sublinks:
for sublink in sublinks:
if not sublink:
continue
if not sublink.startswith("http"):
base_url = StringUtils.get_base_url(torrent.page_url)
if sublink.startswith("/"):
sublink = "%s%s" % (base_url, sublink)
else:
sublink = "%s/%s" % (base_url, sublink)
sublink_list.append(sublink)
return sublink_list
finally:
if html is not None:
del html
elif res is not None:
logger.warn(f"连接 {torrent.page_url} 失败,状态码:{res.status_code}")
else:
logger.warn(f"无法打开链接:{torrent.page_url}")
return None
def download_added(self, context: Context, download_dir: Path, torrent_content: Union[str, bytes] = None): def download_added(self, context: Context, download_dir: Path, torrent_content: Union[str, bytes] = None):
""" """
添加下载任务成功后,从站点下载字幕,保存到下载目录 添加下载任务成功后,从站点下载字幕,保存到下载目录
@@ -117,32 +167,12 @@ class SubtitleModule(_ModuleBase):
logger.error(f"下载目录不存在,无法保存字幕:{download_dir / folder_name}") logger.error(f"下载目录不存在,无法保存字幕:{download_dir / folder_name}")
return return
# 读取网站代码 # 读取网站代码
request = RequestUtils(cookies=torrent.site_cookie, ua=torrent.site_ua) sublink_list = self._get_subtitle_links(torrent)
res = request.get_res(torrent.page_url) if not sublink_list:
if res and res.status_code == 200: logger.warn(f"{torrent.page_url} 页面未找到字幕下载链接")
if not res.text:
logger.warn(f"读取页面代码失败:{torrent.page_url}")
return return
html = etree.HTML(res.text)
try:
sublink_list = []
for xpath in self._SITE_SUBTITLE_XPATH:
sublinks = html.xpath(xpath)
if sublinks:
for sublink in sublinks:
if not sublink:
continue
if not sublink.startswith("http"):
base_url = StringUtils.get_base_url(torrent.page_url)
if sublink.startswith("/"):
sublink = "%s%s" % (base_url, sublink)
else:
sublink = "%s/%s" % (base_url, sublink)
sublink_list.append(sublink)
finally:
if html is not None:
del html
# 下载所有字幕文件 # 下载所有字幕文件
request = RequestUtils(cookies=torrent.site_cookie, ua=torrent.site_ua)
for sublink in sublink_list: for sublink in sublink_list:
logger.info(f"找到字幕下载链接:{sublink},开始下载...") logger.info(f"找到字幕下载链接:{sublink},开始下载...")
# 下载 # 下载
@@ -189,11 +219,4 @@ class SubtitleModule(_ModuleBase):
else: else:
logger.error(f"下载字幕文件失败:{sublink}") logger.error(f"下载字幕文件失败:{sublink}")
continue continue
if sublink_list:
logger.info(f"{torrent.page_url} 页面字幕下载完成") logger.info(f"{torrent.page_url} 页面字幕下载完成")
else:
logger.warn(f"{torrent.page_url} 页面未找到字幕下载链接")
elif res is not None:
logger.warn(f"连接 {torrent.page_url} 失败,状态码:{res.status_code}")
else:
logger.warn(f"无法打开链接:{torrent.page_url}")