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 -4
View File
@@ -14,7 +14,9 @@ from app.runtime.log import logger
from app.schemas.types import MediaType
from app.adapters.system import rust as rust_accel
from app.adapters.network.http import RequestUtils, AsyncRequestUtils
from app.domain.string import StringUtils
from app.foundation import size as size_tools
from app.foundation import temporal as time_tools
from app.foundation import url as url_tools
from app.foundation.url import UrlUtils
@@ -479,7 +481,7 @@ class SiteSpider:
if download_link:
if not download_link.startswith("http") \
and not download_link.startswith("magnet"):
_scheme, _domain = StringUtils.get_url_netloc(self.domain)
_scheme, _domain = url_tools.split_netloc(self.domain)
if _domain in download_link:
if download_link.startswith("/"):
self.torrents_info['enclosure'] = f"{_scheme}:{download_link}"
@@ -535,7 +537,7 @@ class SiteSpider:
size_val = item.replace("\n", "").strip()
size_val = self.__filter_text(size_val,
selector.get('filters'))
self.torrents_info['size'] = StringUtils.num_filesize(size_val)
self.torrents_info['size'] = size_tools.parse_size(size_val)
else:
self.torrents_info['size'] = 0
@@ -600,7 +602,7 @@ class SiteSpider:
else:
datetime.datetime.strptime(str(self.torrents_info['pubdate']), '%Y-%m-%d %H:%M:%S')
except (ValueError, TypeError):
self.torrents_info['pubdate'] = StringUtils.unify_datetime_str(str(self.torrents_info['pubdate']))
self.torrents_info['pubdate'] = time_tools.normalize_datetime(str(self.torrents_info['pubdate']))
if self.__is_invalid_pubdate_text(self.torrents_info.get('pubdate')):
self.torrents_info.pop('pubdate', None)
+4 -3
View File
@@ -6,7 +6,8 @@ from app.db.oper.systemconfig import SystemConfigOper
from app.runtime.log import logger
from app.schemas import MediaType
from app.adapters.network.http import RequestUtils, AsyncRequestUtils
from app.domain.string import StringUtils
from app.domain import site as site_rules
from app.foundation import temporal as time_tools
class HaiDanSpider:
@@ -63,7 +64,7 @@ class HaiDanSpider:
if indexer:
self._indexerid = indexer.get('id')
self._url = indexer.get('domain')
self._domain = StringUtils.get_url_domain(self._url)
self._domain = site_rules.extract_domain(self._url)
self._searchurl = self._searchurl % self._url
self._name = indexer.get('name')
if indexer.get('proxy'):
@@ -132,7 +133,7 @@ class HaiDanSpider:
'title': item.get('name'),
'description': item.get('small_descr'),
'enclosure': item.get('url'),
'pubdate': StringUtils.format_timestamp(item.get('added')),
'pubdate': time_tools.format_timestamp(item.get('added')),
'size': int(item.get('size') or '0'),
'seeders': int(item.get('seeders') or '0'),
'peers': int(item.get("leechers") or '0'),
+2 -2
View File
@@ -5,7 +5,7 @@ from app.db.oper.systemconfig import SystemConfigOper
from app.runtime.log import logger
from app.schemas import MediaType
from app.adapters.network.http import RequestUtils, AsyncRequestUtils
from app.domain.string import StringUtils
from app.domain import site as site_rules
class HddolbySpider:
@@ -71,7 +71,7 @@ class HddolbySpider:
if indexer:
self._indexerid = indexer.get('id')
self._domain = indexer.get('domain')
self._domain_host = StringUtils.get_url_domain(self._domain)
self._domain_host = site_rules.extract_domain(self._domain)
self._name = indexer.get('name')
if indexer.get('proxy'):
self._proxy = settings.PROXY
+7 -6
View File
@@ -9,7 +9,8 @@ from app.db.oper.systemconfig import SystemConfigOper
from app.runtime.log import logger
from app.schemas import MediaType
from app.adapters.network.http import RequestUtils, AsyncRequestUtils
from app.domain.string import StringUtils
from app.domain import site as site_rules
from app.foundation import temporal as time_tools
class MTorrentSpider:
@@ -68,7 +69,7 @@ class MTorrentSpider:
if indexer:
self._indexerid = indexer.get('id')
self._url = indexer.get('domain')
self._domain = StringUtils.get_url_domain(self._url)
self._domain = site_rules.extract_domain(self._url)
self._searchurl = self._searchurl % self._domain
self._name = indexer.get('name')
if indexer.get('proxy'):
@@ -137,7 +138,7 @@ class MTorrentSpider:
'title': result.get('name'),
'description': result.get('smallDescr'),
'enclosure': self.__get_download_url(result.get('id')),
'pubdate': StringUtils.format_timestamp(result.get('createdDate')),
'pubdate': time_tools.format_timestamp(result.get('createdDate')),
'size': int(result.get('size') or '0'),
'seeders': int(status.get("seeders") or '0'),
'peers': int(status.get("leechers") or '0'),
@@ -150,18 +151,18 @@ class MTorrentSpider:
'category': category
}
if discount_end_time := status.get('discountEndTime'):
torrent['freedate'] = StringUtils.format_timestamp(discount_end_time)
torrent['freedate'] = time_tools.format_timestamp(discount_end_time)
# 解析全站促销时的规则(当前馒头只有下载促销)
if promotion_rule := status.get("promotionRule"):
discount = promotion_rule.get("discount", "NORMAL")
torrent["downloadvolumefactor"] = self.__get_downloadvolumefactor(discount)
if end_time := promotion_rule.get("endTime"):
torrent["freedate"] = StringUtils.format_timestamp(end_time)
torrent["freedate"] = time_tools.format_timestamp(end_time)
if mall_single_free := status.get("mallSingleFree"):
if mall_single_free.get("status") == "ONGOING":
torrent["downloadvolumefactor"] = self.__get_downloadvolumefactor("FREE")
if end_date := mall_single_free.get("endDate"):
torrent["freedate"] = StringUtils.format_timestamp(end_date)
torrent["freedate"] = time_tools.format_timestamp(end_date)
torrents.append(torrent)
return torrents
+5 -4
View File
@@ -7,7 +7,8 @@ from app.db.oper.systemconfig import SystemConfigOper
from app.runtime.log import logger
from app.schemas import MediaType
from app.adapters.network.http import RequestUtils, AsyncRequestUtils
from app.domain.string import StringUtils
from app.domain import site as site_rules
from app.foundation import temporal as time_tools
class RousiSpider:
@@ -52,7 +53,7 @@ class RousiSpider:
if indexer:
self._indexerid = indexer.get('id')
self._url = indexer.get('domain')
self._domain = StringUtils.get_url_domain(self._url)
self._domain = site_rules.extract_domain(self._url)
self._searchurl = self._searchurl % self._domain
self._downloadurl = self._downloadurl % (self._domain, "%s")
self._name = indexer.get('name')
@@ -199,13 +200,13 @@ class RousiSpider:
uploadvolumefactor = float(promotion.get('up_multiplier', 1.0))
# 促销到期时间,格式化为 YYYY-MM-DD HH:MM:SS
if promotion.get('until'):
freedate = StringUtils.unify_datetime_str(promotion.get('until'))
freedate = time_tools.normalize_datetime(promotion.get('until'))
torrent = {
'title': result.get('title'),
'description': result.get('subtitle'),
'enclosure': self.__get_download_url(result.get('id')),
'pubdate': StringUtils.unify_datetime_str(result.get('created_at')),
'pubdate': time_tools.normalize_datetime(result.get('created_at')),
'size': int(result.get('size') or 0),
'seeders': int(result.get('seeders') or 0),
'peers': int(result.get('leechers') or 0),
+3 -3
View File
@@ -7,7 +7,7 @@ from app.runtime.config import settings
from app.runtime.log import logger
from app.schemas import MediaType
from app.adapters.network.http import AsyncRequestUtils, RequestUtils
from app.domain.string import StringUtils
from app.foundation import temporal as time_tools
class SunnyPTSpider:
@@ -272,14 +272,14 @@ class SunnyPTSpider:
if promotion_active else 1.0
upload_factor = float(promotion.get("up_multiplier", 1.0)) \
if promotion_active else 1.0
freedate = StringUtils.unify_datetime_str(promotion.get("until")) \
freedate = time_tools.normalize_datetime(promotion.get("until")) \
if promotion_active and promotion.get("until") else None
torrent_id = result.get("id")
torrents.append({
"title": result.get("title"),
"description": result.get("subtitle"),
"enclosure": self._build_download_url(torrent_id),
"pubdate": StringUtils.unify_datetime_str(result.get("created_at")),
"pubdate": time_tools.normalize_datetime(result.get("created_at")),
"size": int(result.get("size") or 0),
"seeders": int(result.get("seeders") or 0),
"peers": int(result.get("leechers") or 0),
+2 -2
View File
@@ -6,7 +6,7 @@ from app.runtime.config import settings
from app.runtime.log import logger
from app.adapters.network.http import RequestUtils, AsyncRequestUtils
from app.foundation.singleton import SingletonClass
from app.domain.string import StringUtils
from app.foundation import temporal as time_tools
class TNodeSpider(metaclass=SingletonClass):
@@ -98,7 +98,7 @@ class TNodeSpider(metaclass=SingletonClass):
'title': result.get('title'),
'description': result.get('subtitle'),
'enclosure': self._downloadurl % (self._domain, result.get('id')),
'pubdate': StringUtils.format_timestamp(result.get('upload_time')),
'pubdate': time_tools.format_timestamp(result.get('upload_time')),
'size': result.get('size'),
'seeders': result.get('seeding'),
'peers': result.get('leeching'),
+5 -4
View File
@@ -5,7 +5,8 @@ from app.runtime.config import settings
from app.runtime.log import logger
from app.schemas import MediaType
from app.adapters.network.http import RequestUtils, AsyncRequestUtils
from app.domain.string import StringUtils
from app.foundation import temporal as time_tools
from app.foundation import text as text_tools
class TorrentLeech:
@@ -76,7 +77,7 @@ class TorrentLeech:
'enclosure': self._downloadurl % (self._indexer.get('domain'),
result.get('fid'),
result.get('filename')),
'pubdate': StringUtils.format_timestamp(result.get('addedTimestamp')),
'pubdate': time_tools.format_timestamp(result.get('addedTimestamp')),
'size': result.get('size'),
'seeders': result.get('seeders'),
'peers': result.get('leechers'),
@@ -104,7 +105,7 @@ class TorrentLeech:
"""
搜索种子
"""
if StringUtils.is_chinese(keyword):
if text_tools.contains_chinese(keyword):
# 不支持中文
return True, []
@@ -141,7 +142,7 @@ class TorrentLeech:
"""
异步搜索种子
"""
if StringUtils.is_chinese(keyword):
if text_tools.contains_chinese(keyword):
# 不支持中文
return True, []
+3 -3
View File
@@ -6,7 +6,7 @@ from app.runtime.config import settings
from app.runtime.log import logger
from app.schemas import MediaType
from app.adapters.network.http import AsyncRequestUtils, RequestUtils
from app.domain.string import StringUtils
from app.foundation import temporal as time_tools
class YemaSpider:
@@ -162,14 +162,14 @@ class YemaSpider:
"title": result.get("showName"),
"description": result.get("shortDesc"),
"enclosure": self._build_download_url(torrent_id),
"pubdate": StringUtils.unify_datetime_str(result.get("listingTime")),
"pubdate": time_tools.normalize_datetime(result.get("listingTime")),
"size": result.get("fileSize"),
"seeders": result.get("seedNum"),
"peers": result.get("leechNum"),
"grabs": result.get("completedNum"),
"downloadvolumefactor": self._download_factor(result.get("downloadPromotion")),
"uploadvolumefactor": self._upload_factor(result.get("uploadPromotion")),
"freedate": StringUtils.unify_datetime_str(result.get("downloadPromotionEndTime")),
"freedate": time_tools.normalize_datetime(result.get("downloadPromotionEndTime")),
"page_url": f"{self._site_url}/#/torrent/detail/{torrent_id}/",
"labels": labels,
"hit_and_run": bool(result.get("hrPunishEnable")),