mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-06-12 19:21:05 +08:00
feat: add Rust acceleration for core parsing
This commit is contained in:
@@ -11,8 +11,10 @@ from requests import Session
|
||||
from app.core.config import settings
|
||||
from app.helper.cloudflare import under_challenge
|
||||
from app.log import logger
|
||||
from app.utils import rust_accel
|
||||
from app.utils.http import RequestUtils
|
||||
from app.utils.site import SiteUtils
|
||||
from app.utils.string import StringUtils
|
||||
|
||||
|
||||
# 站点框架
|
||||
@@ -154,6 +156,16 @@ class SiteParserBase(metaclass=ABCMeta):
|
||||
"""
|
||||
return self.schema
|
||||
|
||||
@staticmethod
|
||||
def num_filesize(text) -> int:
|
||||
"""
|
||||
将站点页面中的文件大小文本转换为字节,优先使用 Rust 快路径。
|
||||
"""
|
||||
rust_value = rust_accel.parse_filesize(text)
|
||||
if rust_value is not None:
|
||||
return rust_value
|
||||
return StringUtils.num_filesize(text)
|
||||
|
||||
def parse(self):
|
||||
"""
|
||||
解析站点信息
|
||||
|
||||
@@ -93,10 +93,10 @@ class NexusPhpSiteUserInfo(SiteParserBase):
|
||||
html_text = self._prepare_html_text(html_text)
|
||||
upload_match = re.search(r"[^总]上[传傳]量?[::_<>/a-zA-Z-=\"'\s#;]+([\d,.\s]+[KMGTPI]*B)", html_text,
|
||||
re.IGNORECASE)
|
||||
self.upload = StringUtils.num_filesize(upload_match.group(1).strip()) if upload_match else 0
|
||||
self.upload = self.num_filesize(upload_match.group(1).strip()) if upload_match else 0
|
||||
download_match = re.search(r"[^总子影力]下[载載]量?[::_<>/a-zA-Z-=\"'\s#;]+([\d,.\s]+[KMGTPI]*B)", html_text,
|
||||
re.IGNORECASE)
|
||||
self.download = StringUtils.num_filesize(download_match.group(1).strip()) if download_match else 0
|
||||
self.download = self.num_filesize(download_match.group(1).strip()) if download_match else 0
|
||||
ratio_match = re.search(r"分享率[::_<>/a-zA-Z-=\"'\s#;]+([\d,.\s]+)", html_text)
|
||||
# 计算分享率
|
||||
calc_ratio = 0.0 if self.download <= 0.0 else round(self.upload / self.download, 3)
|
||||
@@ -209,7 +209,7 @@ class NexusPhpSiteUserInfo(SiteParserBase):
|
||||
page_seeding = len(seeding_sizes)
|
||||
|
||||
for i in range(0, len(seeding_sizes)):
|
||||
size = StringUtils.num_filesize(seeding_sizes[i].xpath("string(.)").strip())
|
||||
size = self.num_filesize(seeding_sizes[i].xpath("string(.)").strip())
|
||||
seeders = StringUtils.str_int(seeding_seeders[i])
|
||||
|
||||
page_seeding_size += size
|
||||
@@ -273,7 +273,7 @@ class NexusPhpSiteUserInfo(SiteParserBase):
|
||||
tmp_seeding_size = 0
|
||||
tmp_seeding_info = []
|
||||
for i in range(0, len(seeding_sizes)):
|
||||
size = StringUtils.num_filesize(seeding_sizes[i].xpath("string(.)").strip())
|
||||
size = self.num_filesize(seeding_sizes[i].xpath("string(.)").strip())
|
||||
seeders = StringUtils.str_int(seeding_seeders[i])
|
||||
|
||||
tmp_seeding_size += size
|
||||
@@ -292,7 +292,7 @@ class NexusPhpSiteUserInfo(SiteParserBase):
|
||||
seeding_size_match = re.search(r"总做种体积:\s+([\d,.\s]+[KMGTPI]*B)", seeding_sizes[0], re.IGNORECASE)
|
||||
tmp_seeding = StringUtils.str_int(seeding_match.group(1)) if (
|
||||
seeding_match and seeding_match.group(1)) else 0
|
||||
tmp_seeding_size = StringUtils.num_filesize(
|
||||
tmp_seeding_size = self.num_filesize(
|
||||
seeding_size_match.group(1).strip()) if seeding_size_match else 0
|
||||
if not self.seeding_size:
|
||||
self.seeding_size = tmp_seeding_size
|
||||
|
||||
@@ -75,7 +75,7 @@ class ZhixingSiteUserInfo(SiteParserBase):
|
||||
s = s.strip()
|
||||
if re.match(r'^\d+(\.\d+)?$', s):
|
||||
s += ' B'
|
||||
return StringUtils.num_filesize(s) if s else 0
|
||||
return self.num_filesize(s) if s else 0
|
||||
|
||||
self.upload = num_filesize_safe(info_dict.get('上传流量')) if '上传流量' in info_dict else 0
|
||||
self.download = num_filesize_safe(info_dict.get('下载流量')) if '下载流量' in info_dict else 0
|
||||
@@ -108,7 +108,7 @@ class ZhixingSiteUserInfo(SiteParserBase):
|
||||
if size_td:
|
||||
size_text = size_td.find('a').text if size_td.find('a') else size_td.text.strip()
|
||||
page_seeding += 1
|
||||
page_seeding_size += StringUtils.num_filesize(size_text)
|
||||
page_seeding_size += self.num_filesize(size_text)
|
||||
return page_seeding, page_seeding_size
|
||||
|
||||
def _parse_message_unread_links(self, html_text: str, msg_links: list) -> Optional[str]:
|
||||
@@ -164,7 +164,7 @@ class ZhixingSiteUserInfo(SiteParserBase):
|
||||
s = s.strip()
|
||||
if re.match(r'^\d+(\.\d+)?$', s):
|
||||
s += ' B'
|
||||
return StringUtils.num_filesize(s) if s else 0
|
||||
return self.num_filesize(s) if s else 0
|
||||
self.seeding = int(self._basic_info.get('当前保种数量', 0))
|
||||
self.seeding_size = num_filesize_safe(self._basic_info.get('当前保种容量', ''))
|
||||
|
||||
@@ -181,4 +181,4 @@ class ZhixingSiteUserInfo(SiteParserBase):
|
||||
self.message_unread = str(self.message_unread or 0)
|
||||
|
||||
self.seeding = str(self.seeding or 0)
|
||||
self.seeding_size = str(self.seeding_size or 0)
|
||||
self.seeding_size = str(self.seeding_size or 0)
|
||||
|
||||
Reference in New Issue
Block a user