fix(api): accept agent media type values

9 处裸构造 MediaType(x) 改为 MediaType.from_agent(x) or MediaType(x),与 search.py/site.py 既有写法一致。智能体按 API 入参规约传入 movie/tv/music 时不再抛 ValueError 返回 500,中文原生枚举值仍走原分支,无行为变化。

涉及:media.py detail、tmdb.py(3)、douban.py(2)、mediaserver.py not_exists、subscribe.py create_subscribe、transfer.py _execute_manual_transfer。
This commit is contained in:
pavel2
2026-10-01 02:48:34 +08:00
parent 736b6686c3
commit 0d62815a0f
6 changed files with 18 additions and 15 deletions

View File

@@ -70,7 +70,7 @@ async def douban_credits(
"""
根据豆瓣ID查询演员阵容,type_name: 电影/电视剧
"""
mediatype = MediaType(type_name)
mediatype = MediaType.from_agent(type_name) or MediaType(type_name)
if mediatype == MediaType.MOVIE:
return await DoubanChain().async_movie_credits(doubanid=doubanid)
elif mediatype == MediaType.TV:
@@ -93,7 +93,7 @@ async def douban_recommend(
"""
根据豆瓣ID查询推荐电影/电视剧,type_name: 电影/电视剧
"""
mediatype = MediaType(type_name)
mediatype = MediaType.from_agent(type_name) or MediaType(type_name)
if mediatype == MediaType.MOVIE:
medias = await DoubanChain().async_movie_recommend(doubanid=doubanid)
elif mediatype == MediaType.TV:

View File

@@ -697,7 +697,7 @@ async def detail(
"""
根据媒体来源和原生 ID 查询影视或音乐信息
"""
mtype = MediaType(type_name)
mtype = MediaType.from_agent(type_name) or MediaType(type_name)
normalized_source, normalized_media_id = resolve_media_identity(
media_source=media_source,
media_id=media_id,

View File

@@ -166,7 +166,7 @@ def not_exists(
"""
# 媒体信息
meta = MetaInfo(title=media_in.title)
mtype = MediaType(media_in.type) if media_in.type else None
mtype = MediaType.from_agent(media_in.type) or MediaType(media_in.type) if media_in.type else None
if mtype:
meta.type = mtype
if media_in.season is not None:

View File

@@ -251,7 +251,7 @@ async def create_subscribe(
"""
# 类型转换
if subscribe_in.type:
mtype = MediaType(subscribe_in.type)
mtype = MediaType.from_agent(subscribe_in.type) or MediaType(subscribe_in.type)
else:
mtype = None
# 非 TMDB 来源的标题可能自带季标记,入库前统一拆分。
@@ -625,7 +625,7 @@ async def popular_subscribes(
if min_sub and count < min_sub:
continue
media = MediaInfo()
media.type = MediaType(sub.get("type"))
media.type = MediaType.from_agent(sub.get("type")) or MediaType(sub.get("type"))
media.media_source = normalize_media_source(sub.get("media_source"))
media.media_id = str(sub.get("media_id")) if sub.get("media_id") is not None else None
# 处理标题

View File

@@ -116,7 +116,7 @@ async def tmdb_similar(
"""
根据TMDBID查询类似电影/电视剧,type_name: 电影/电视剧
"""
mediatype = MediaType(type_name)
mediatype = MediaType.from_agent(type_name) or MediaType(type_name)
if mediatype == MediaType.MOVIE:
medias = await TmdbChain().async_movie_similar(tmdbid=tmdbid)
elif mediatype == MediaType.TV:
@@ -143,7 +143,7 @@ async def tmdb_recommend(
"""
根据TMDBID查询推荐电影/电视剧,type_name: 电影/电视剧
"""
mediatype = MediaType(type_name)
mediatype = MediaType.from_agent(type_name) or MediaType(type_name)
if mediatype == MediaType.MOVIE:
medias = await TmdbChain().async_movie_recommend(tmdbid=tmdbid)
elif mediatype == MediaType.TV:
@@ -189,7 +189,7 @@ async def tmdb_credits(
"""
根据TMDBID查询演员阵容,type_name: 电影/电视剧
"""
mediatype = MediaType(type_name)
mediatype = MediaType.from_agent(type_name) or MediaType(type_name)
if mediatype == MediaType.MOVIE:
persons = await TmdbChain().async_movie_credits(tmdbid=tmdbid, page=page)
elif mediatype == MediaType.TV:

View File

@@ -790,12 +790,15 @@ def _execute_manual_transfer(
mtype = None
type_name = str(transer_item.type_name).strip() if transer_item.type_name else ""
if type_name and type_name.lower() not in {"自动", "auto", "none"}:
try:
mtype = MediaType(type_name)
except ValueError:
return _SchemaResponse(
success=False, message=f"不支持的媒体类型:{type_name}"
)
# 先按 Agent 使用的 movie/tv/music 取值解析,再回退到原生中文枚举值。
mtype = MediaType.from_agent(type_name)
if mtype is None:
try:
mtype = MediaType(type_name)
except ValueError:
return _SchemaResponse(
success=False, message=f"不支持的媒体类型:{type_name}"
)
def _resolve_music_type(file_item: FileItem) -> Optional[str]:
"""为未显式指定实体的旧客户端按源项类型补全音乐命名空间。"""