refactor(string): split utilities by responsibility

This commit is contained in:
jxxghp
2026-08-15 08:03:55 +08:00
parent a2117bafc8
commit 96118e73e7
99 changed files with 1608 additions and 1424 deletions
+6 -5
View File
@@ -25,7 +25,8 @@ from app.schemas.tmdb import TmdbEpisode
from app.schemas.transfer import TransferInfo
from app.schemas.types import MUSIC_ENTITY_ALBUM, SystemConfigKey
from app.foundation.singleton import Singleton, SingletonClass
from app.domain.string import StringUtils
from app.foundation import size as size_tools
from app.foundation.crypto import HashUtils
class TemplateContextBuilder:
@@ -346,7 +347,7 @@ class TemplateContextBuilder:
return
if torrentinfo.size:
if str(torrentinfo.size).replace(".", "").isdigit():
size = StringUtils.str_filesize(torrentinfo.size)
size = size_tools.format_compact_size(torrentinfo.size)
else:
size = torrentinfo.size
else:
@@ -391,7 +392,7 @@ class TemplateContextBuilder:
ctx = {
"transfer_type": transferinfo.transfer_type,
"file_count": transferinfo.file_count,
"total_size": StringUtils.str_filesize(transferinfo.total_size),
"total_size": size_tools.format_compact_size(transferinfo.total_size),
"err_msg": transferinfo.message,
}
context.update(ctx)
@@ -472,9 +473,9 @@ class TemplateHelper(metaclass=SingletonClass):
"""
if isinstance(cuntent, dict):
base_str = cuntent.get("title", '') + cuntent.get("text", '')
return StringUtils.md5_hash(json.dumps(base_str, sort_keys=True, ensure_ascii=False))
return HashUtils.md5(json.dumps(base_str, sort_keys=True, ensure_ascii=False))
return StringUtils.md5_hash(cuntent)
return HashUtils.md5(cuntent)
def get_cache_context(self, cuntent: Union[str, dict]) -> Optional[dict]:
"""
+2 -2
View File
@@ -10,7 +10,7 @@ from app.application.security.twofactor import TwoFactorAuth
from app.runtime.log import logger
from app.adapters.network.http import RequestUtils
from app.domain.site import SiteUtils
from app.domain.string import StringUtils
from app.foundation import url as url_tools
class CookieHelper:
@@ -355,4 +355,4 @@ class CookieHelper:
return ""
if imageurl.startswith("/"):
imageurl = imageurl[1:]
return "%s/%s" % (StringUtils.get_base_url(siteurl), imageurl)
return "%s/%s" % (url_tools.base_url(siteurl), imageurl)
-24
View File
@@ -1,25 +1 @@
"""站点目录、认证与索引资源的应用能力包。"""
from importlib.machinery import EXTENSION_SUFFIXES
from pathlib import Path
def _include_legacy_resource_directory(
package_paths: list[str], package_dir: Path
) -> None:
"""canonical 扩展缺失时允许读取旧 Docker 更新器写入的资源目录。"""
extension_names = tuple(f"sites{suffix}" for suffix in EXTENSION_SUFFIXES)
if any((package_dir / name).is_file() for name in extension_names):
return
legacy_dir = package_dir.parent.parent / "helper"
if (
legacy_dir.is_dir()
and any((legacy_dir / name).is_file() for name in extension_names)
and str(legacy_dir) not in package_paths
):
# 旧镜像内固化的 mp_update.sh 无法随源码热更新,只在过渡场景扩展包搜索路径。
package_paths.append(str(legacy_dir))
_include_legacy_resource_directory(__path__, Path(__file__).resolve().parent)
+14 -12
View File
@@ -19,7 +19,9 @@ from app.runtime.log import logger
from app.schemas.types import MediaType, SystemConfigKey
from app.adapters.network.http import RequestUtils
from app.schemas.media import resolve_media_identity
from app.domain.string import StringUtils
from app.domain import torrent as torrent_rules
from app.foundation import text as text_tools
from app.foundation.crypto import HashUtils
_SIZE_UNIT = 1024 * 1024
@@ -85,7 +87,7 @@ class TorrentHelper:
if url.startswith("magnet:"):
return None, url, "", [], f"磁力链接"
# 构建 torrent 种子文件的缓存路径
cache_path = Path(StringUtils.md5_hash(url)).with_suffix(".torrent")
cache_path = Path(HashUtils.md5(url)).with_suffix(".torrent")
# 缓存处理器
cache_backend = FileCache()
# 读取缓存的种子文件
@@ -249,7 +251,7 @@ class TorrentHelper:
return "", []
# 检查是否为磁力链接
if StringUtils.is_magnet_link(torrent_content):
if torrent_rules.is_magnet_link(torrent_content):
return "", []
try:
@@ -423,15 +425,15 @@ class TorrentHelper:
return True
# 要匹配的媒体标题、原标题
media_titles = {
StringUtils.clear_upper(mediainfo.title),
StringUtils.clear_upper(mediainfo.original_title)
text_tools.normalize_upper(mediainfo.title),
text_tools.normalize_upper(mediainfo.original_title)
} - {""}
# 要匹配的媒体别名、译名
media_names = {StringUtils.clear_upper(name) for name in mediainfo.names if name}
media_names = {text_tools.normalize_upper(name) for name in mediainfo.names if name}
# 识别的种子中英文名
meta_names = {
StringUtils.clear_upper(torrent_meta.cn_name),
StringUtils.clear_upper(torrent_meta.en_name)
text_tools.normalize_upper(torrent_meta.cn_name),
text_tools.normalize_upper(torrent_meta.en_name)
} - {""}
# 比对种子识别类型
if torrent_meta.type == MediaType.TV and mediainfo.type != MediaType.TV:
@@ -470,19 +472,19 @@ class TorrentHelper:
# 标题拆分
if torrent_meta.org_string:
# 只拆分出标题中的非英文单词进行匹配,英文单词容易误匹配(带空格的多个单词组合除外)
titles = [StringUtils.clear_upper(t) for t in re.split(
titles = [text_tools.normalize_upper(t) for t in re.split(
r'[\s/【】.\[\]\-]+',
torrent_meta.org_string
) if not StringUtils.is_english_word(t)]
) if not text_tools.is_english_word(t)]
# 在标题中判断是否存在标题、原语种标题
if media_titles.intersection(titles):
logger.info(f'{mediainfo.title} 通过标题匹配到资源:{torrent.site_name} - {torrent.title}')
return True
# 在副标题中(非英文单词)判断是否存在标题、原语种标题、别名、译名
if torrent.description:
subtitles = {StringUtils.clear_upper(t) for t in re.split(
subtitles = {text_tools.normalize_upper(t) for t in re.split(
r'[\s/【】|]+',
torrent.description) if not StringUtils.is_english_word(t)}
torrent.description) if not text_tools.is_english_word(t)}
if media_titles.intersection(subtitles) or media_names.intersection(subtitles):
logger.info(f'{mediainfo.title} 通过副标题匹配到资源:{torrent.site_name} - {torrent.title}'
f'副标题:{torrent.description}')