Skip to content

Commit 499fbb3

Browse files
committed
feat: 引入 Alist2StrmMode 枚举以简化模式管理
1 parent bb5596f commit 499fbb3

2 files changed

Lines changed: 27 additions & 14 deletions

File tree

app/modules/alist2strm/alist2strm.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from app.utils import RequestUtils
1111
from app.extensions import VIDEO_EXTS, SUBTITLE_EXTS, IMAGE_EXTS, NFO_EXTS
1212
from app.modules.alist import AlistClient, AlistPath
13-
13+
from app.modules.alist2strm.mode import Alist2StrmMode
1414

1515
class Alist2Strm:
1616
def __init__(
@@ -58,7 +58,7 @@ def __init__(
5858
"""
5959

6060
self.client = AlistClient(url, username, password, token)
61-
self.mode = mode
61+
self.mode = Alist2StrmMode.from_str(mode)
6262

6363
self.source_dir = source_dir
6464
self.target_dir = Path(target_dir)
@@ -159,13 +159,8 @@ def filter(path: AlistPath) -> bool:
159159

160160
return True
161161

162-
if self.mode not in ["AlistURL", "RawURL", "AlistPath"]:
163-
logger.warning(
164-
f"Alist2Strm 的模式 {self.mode} 不存在,已设置为默认模式 AlistURL"
165-
)
166-
self.mode = "AlistURL"
167162

168-
if self.mode == "RawURL":
163+
if self.mode == Alist2StrmMode.RawURL:
169164
is_detail = True
170165
else:
171166
is_detail = False
@@ -194,7 +189,7 @@ def filter(path: AlistPath) -> bool:
194189
logger.info(f"最大文件: {largest_file.full_path}")
195190

196191
# 重新获取详细信息以确保有 raw_url
197-
if self.mode == "RawURL" and not largest_file.raw_url:
192+
if self.mode == Alist2StrmMode.RawURL and not largest_file.raw_url:
198193
logger.debug(f"重新获取 BDMV 文件详细信息: {largest_file.full_path}")
199194
try:
200195
updated_path = await self.client.async_api_fs_get(largest_file.full_path)
@@ -233,14 +228,12 @@ async def __file_processer(self, path: AlistPath) -> None:
233228
logger.debug(f"__file_processer: 处理文件 {path.full_path} -> 本地路径 {local_path} | 模式 {self.mode}")
234229

235230
# 统一的 URL 生成逻辑,BDMV 文件与普通文件使用相同的逻辑
236-
if self.mode == "AlistURL":
231+
if self.mode == Alist2StrmMode.AlistURL:
237232
content = path.download_url
238-
elif self.mode == "RawURL":
233+
elif self.mode == Alist2StrmMode.RawURL:
239234
content = path.raw_url
240-
elif self.mode == "AlistPath":
235+
elif self.mode == Alist2StrmMode.AlistPath:
241236
content = path.full_path
242-
else:
243-
raise ValueError(f"AlistStrm 未知的模式 {self.mode}")
244237

245238
logger.debug(f"__file_processer: 初始 content = {content}")
246239

app/modules/alist2strm/mode.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from enum import Enum
2+
3+
class Alist2StrmMode(Enum):
4+
"""
5+
模块 alist2strm 的运行模式
6+
"""
7+
AlistURL = "AlistURL"
8+
RawURL = "RawURL"
9+
AlistPath = "AlistPath"
10+
11+
@classmethod
12+
def from_str(cls, mode_str: str) -> "Alist2StrmMode":
13+
"""
14+
从字符串转换为 AList2StrmMode 枚举
15+
如果字符串不匹配任何枚举值,则返回 AlistURL 模式
16+
:param mode_str: 模式字符串
17+
:return: Alist2StrmMode 枚举值
18+
例如,"alisturl" 将返回 Alist2StrmMode.AlistURL
19+
"""
20+
return cls[mode_str.upper()] if mode_str.upper() in cls.__members__ else cls.AlistURL

0 commit comments

Comments
 (0)