refactor: streamline media recognition by removing MetaInfoPath usage

This commit is contained in:
jxxghp
2026-05-10 09:26:13 +08:00
parent 4d132c424a
commit 79eb128196
6 changed files with 45 additions and 48 deletions
+1 -2
View File
@@ -49,8 +49,7 @@ class RecognizeMediaTool(MoviePilotTool):
try: try:
media_chain = MediaChain() media_chain = MediaChain()
context = None
# 根据提供的参数选择识别方式 # 根据提供的参数选择识别方式
if path: if path:
# 文件路径识别 # 文件路径识别
+11 -15
View File
@@ -8,7 +8,6 @@ from pydantic import BaseModel, Field
from app.agent.tools.base import MoviePilotTool from app.agent.tools.base import MoviePilotTool
from app.chain.media import MediaChain from app.chain.media import MediaChain
from app.core.metainfo import MetaInfoPath
from app.log import logger from app.log import logger
from app.schemas import FileItem from app.schemas import FileItem
@@ -80,8 +79,7 @@ class ScrapeMetadataTool(MoviePilotTool):
# 检查本地存储路径是否存在 # 检查本地存储路径是否存在
if storage == "local": if storage == "local":
scrape_path = Path(path) if not Path(path).exists():
if not scrape_path.exists():
return json.dumps( return json.dumps(
{"success": False, "message": f"刮削路径不存在: {path}"}, {"success": False, "message": f"刮削路径不存在: {path}"},
ensure_ascii=False, ensure_ascii=False,
@@ -89,14 +87,12 @@ class ScrapeMetadataTool(MoviePilotTool):
# 识别媒体信息 # 识别媒体信息
media_chain = MediaChain() media_chain = MediaChain()
scrape_path = Path(path) context = await media_chain.async_recognize_by_path(
meta = MetaInfoPath(scrape_path) path,
mediainfo = await media_chain.async_recognize_by_meta(
meta,
obtain_images=True, obtain_images=True,
) )
if not mediainfo: if not context or not context.media_info:
return json.dumps( return json.dumps(
{ {
"success": False, "success": False,
@@ -111,8 +107,8 @@ class ScrapeMetadataTool(MoviePilotTool):
"storage", "storage",
media_chain.scrape_metadata, media_chain.scrape_metadata,
fileitem=fileitem, fileitem=fileitem,
meta=meta, meta=context.meta_info,
mediainfo=mediainfo, mediainfo=context.media_info,
overwrite=overwrite, overwrite=overwrite,
) )
@@ -122,11 +118,11 @@ class ScrapeMetadataTool(MoviePilotTool):
"message": f"{path} 刮削完成", "message": f"{path} 刮削完成",
"path": path, "path": path,
"media_info": { "media_info": {
"title": mediainfo.title, "title": context.media_info.title,
"year": mediainfo.year, "year": context.media_info.year,
"type": mediainfo.type.value if mediainfo.type else None, "type": context.media_info.type.value if context.media_info.type else None,
"tmdb_id": mediainfo.tmdb_id, "tmdb_id": context.media_info.tmdb_id,
"season": mediainfo.season, "season": context.media_info.season,
}, },
}, },
ensure_ascii=False, ensure_ascii=False,
+10 -7
View File
@@ -9,7 +9,7 @@ from app.chain.tmdb import TmdbChain
from app.core.config import settings from app.core.config import settings
from app.core.context import Context from app.core.context import Context
from app.core.event import eventmanager from app.core.event import eventmanager
from app.core.metainfo import MetaInfo, MetaInfoPath from app.core.metainfo import MetaInfo
from app.core.security import verify_token, verify_apitoken from app.core.security import verify_token, verify_apitoken
from app.db.models import User from app.db.models import User
from app.db.user_oper import get_current_active_user, get_current_active_superuser from app.db.user_oper import get_current_active_user, get_current_active_superuser
@@ -121,16 +121,19 @@ def scrape(fileitem: schemas.FileItem,
return schemas.Response(success=False, message="刮削路径无效") return schemas.Response(success=False, message="刮削路径无效")
chain = MediaChain() chain = MediaChain()
# 识别媒体信息 # 识别媒体信息
scrape_path = Path(fileitem.path) context = chain.recognize_by_path(fileitem.path, obtain_images=True)
meta = MetaInfoPath(scrape_path) if not context or not context.media_info:
mediainfo = chain.recognize_by_meta(meta, obtain_images=True)
if not mediainfo:
return schemas.Response(success=False, message="刮削失败,无法识别媒体信息") return schemas.Response(success=False, message="刮削失败,无法识别媒体信息")
if storage == "local": if storage == "local":
if not scrape_path.exists(): if not Path(fileitem.path).exists():
return schemas.Response(success=False, message="刮削路径不存在") return schemas.Response(success=False, message="刮削路径不存在")
# 手动刮削 (暂时使用同步版本,可以后续优化为异步) # 手动刮削 (暂时使用同步版本,可以后续优化为异步)
chain.scrape_metadata(fileitem=fileitem, meta=meta, mediainfo=mediainfo, overwrite=True) chain.scrape_metadata(
fileitem=fileitem,
meta=context.meta_info,
mediainfo=context.media_info,
overwrite=True
)
return schemas.Response(success=True, message=f"{fileitem.path} 刮削完成") return schemas.Response(success=True, message=f"{fileitem.path} 刮削完成")
+7 -6
View File
@@ -10,7 +10,6 @@ from app.chain.media import MediaChain
from app.chain.storage import StorageChain from app.chain.storage import StorageChain
from app.chain.transfer import TransferChain from app.chain.transfer import TransferChain
from app.core.config import settings from app.core.config import settings
from app.core.metainfo import MetaInfoPath
from app.core.security import verify_token from app.core.security import verify_token
from app.db.models import User from app.db.models import User
from app.db.user_oper import get_current_active_superuser, get_current_active_superuser_async from app.db.user_oper import get_current_active_superuser, get_current_active_superuser_async
@@ -199,15 +198,17 @@ def rename(fileitem: schemas.FileItem,
if f".{sub_file.extension.lower()}" not in media_exts: if f".{sub_file.extension.lower()}" not in media_exts:
continue continue
sub_path = Path(f"{fileitem.path}{sub_file.name}") sub_path = Path(f"{fileitem.path}{sub_file.name}")
meta = MetaInfoPath(sub_path) context = MediaChain().recognize_by_path(
mediainfo = MediaChain().recognize_by_meta( sub_path,
meta,
obtain_images=False, obtain_images=False,
) )
if not mediainfo: if not context or not context.media_info:
progress.end() progress.end()
return schemas.Response(success=False, message=f"{sub_path.name} 未识别到媒体信息") return schemas.Response(success=False, message=f"{sub_path.name} 未识别到媒体信息")
new_path = transferchain.recommend_name(meta=meta, mediainfo=mediainfo) new_path = transferchain.recommend_name(
meta=context.meta_info,
mediainfo=context.media_info
)
if not new_path: if not new_path:
progress.end() progress.end()
return schemas.Response(success=False, message=f"{sub_path.name} 未识别到新名称") return schemas.Response(success=False, message=f"{sub_path.name} 未识别到新名称")
+5 -7
View File
@@ -9,7 +9,6 @@ from app.chain.media import MediaChain
from app.chain.storage import StorageChain from app.chain.storage import StorageChain
from app.chain.transfer import TransferChain from app.chain.transfer import TransferChain
from app.core.config import settings, global_vars from app.core.config import settings, global_vars
from app.core.metainfo import MetaInfoPath
from app.core.security import verify_token, verify_apitoken from app.core.security import verify_token, verify_apitoken
from app.db import get_db from app.db import get_db
from app.db.models import User from app.db.models import User
@@ -30,19 +29,18 @@ def query_name(path: str, filetype: str,
:param filetype: 文件类型 :param filetype: 文件类型
:param _: Token校验 :param _: Token校验
""" """
meta = MetaInfoPath(Path(path)) context = MediaChain().recognize_by_path(
mediainfo = MediaChain().recognize_by_meta( path,
meta,
obtain_images=False, obtain_images=False,
) )
if not mediainfo: if not context or not context.media_info:
return schemas.Response(success=False, message="未识别到媒体信息") return schemas.Response(success=False, message="未识别到媒体信息")
new_path = TransferChain().recommend_name(meta=meta, mediainfo=mediainfo) new_path = TransferChain().recommend_name(meta=context.meta_info, mediainfo=context.media_info)
if not new_path: if not new_path:
return schemas.Response(success=False, message="未识别到新名称") return schemas.Response(success=False, message="未识别到新名称")
if filetype == "dir": if filetype == "dir":
media_path = DirectoryHelper.get_media_root_path( media_path = DirectoryHelper.get_media_root_path(
rename_format=settings.RENAME_FORMAT(mediainfo.type), rename_format=settings.RENAME_FORMAT(context.media_info.type),
rename_path=Path(new_path), rename_path=Path(new_path),
) )
if media_path: if media_path:
+11 -11
View File
@@ -1,12 +1,9 @@
from pathlib import Path
from app.workflow.actions import BaseAction
from app.core.config import global_vars
from app.schemas import ActionParams, ActionContext
from app.chain.media import MediaChain from app.chain.media import MediaChain
from app.chain.storage import StorageChain from app.chain.storage import StorageChain
from app.core.metainfo import MetaInfoPath from app.core.config import global_vars
from app.log import logger from app.log import logger
from app.schemas import ActionParams, ActionContext
from app.workflow.actions import BaseAction
class ScrapeFileParams(ActionParams): class ScrapeFileParams(ActionParams):
@@ -63,17 +60,20 @@ class ScrapeFileAction(BaseAction):
if self.check_cache(workflow_id, cache_key): if self.check_cache(workflow_id, cache_key):
logger.info(f"{fileitem.path} 已刮削过,跳过") logger.info(f"{fileitem.path} 已刮削过,跳过")
continue continue
meta = MetaInfoPath(Path(fileitem.path))
mediachain = MediaChain() mediachain = MediaChain()
mediainfo = mediachain.recognize_by_meta( context = mediachain.recognize_by_path(
meta, fileitem.path,
obtain_images=True, obtain_images=True,
) )
if not mediainfo: if not context or not context.media_info:
_failed_count += 1 _failed_count += 1
logger.info(f"{fileitem.path} 未识别到媒体信息,无法刮削") logger.info(f"{fileitem.path} 未识别到媒体信息,无法刮削")
continue continue
mediachain.scrape_metadata(fileitem=fileitem, meta=meta, mediainfo=mediainfo) mediachain.scrape_metadata(
fileitem=fileitem,
meta=context.meta_info,
mediainfo=context.media_info
)
self._scraped_files.append(fileitem) self._scraped_files.append(fileitem)
# 保存缓存 # 保存缓存
self.save_cache(workflow_id, cache_key) self.save_cache(workflow_id, cache_key)