mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-11-30 16:05:05 +08:00
fix(plex): sync music items with optional metadata (#6814)
This commit is contained in:
@@ -37,7 +37,7 @@ class MediaServerChain(ChainBase):
|
||||
MediaType.MUSIC.value.casefold(),
|
||||
"music",
|
||||
"musicalbum",
|
||||
"album",
|
||||
"artist", "album", "track",
|
||||
"audio",
|
||||
"song",
|
||||
}:
|
||||
|
||||
@@ -655,14 +655,15 @@ class Plex:
|
||||
|
||||
def __build_media_server_item(self, item) -> Optional[_SchemaMediaServerItem]:
|
||||
"""
|
||||
构造MediaServerItem
|
||||
按 Plex 条目实际具备的字段构造媒体信息,兼容艺人、专辑和曲目
|
||||
:param item: Plex媒体项目
|
||||
:return: MediaServerItem
|
||||
"""
|
||||
if not item:
|
||||
return None
|
||||
ids = self.__get_ids(item.guids)
|
||||
path = item.locations[0] if item.locations else None
|
||||
locations = getattr(item, "locations", None)
|
||||
path = locations[0] if locations else None
|
||||
playback_position = getattr(item, "viewOffset", None) or 0
|
||||
duration = getattr(item, "duration", None) or 0
|
||||
percentage = (playback_position / duration * 100) if duration > 0 else None
|
||||
@@ -685,8 +686,8 @@ class Plex:
|
||||
item_id=item.key,
|
||||
item_type=item.type,
|
||||
title=item.title,
|
||||
original_title=item.originalTitle,
|
||||
year=item.year,
|
||||
original_title=getattr(item, "originalTitle", None),
|
||||
year=getattr(item, "year", None),
|
||||
media_source=MediaServerIdentityHelper.from_provider_ids(ids)[0],
|
||||
media_id=MediaServerIdentityHelper.from_provider_ids(ids)[1],
|
||||
path=path,
|
||||
|
||||
@@ -73,6 +73,9 @@ def test_media_count_reuses_existing_server_statistics():
|
||||
("音乐", "音乐"),
|
||||
("MusicAlbum", "音乐"),
|
||||
("Audio", "音乐"),
|
||||
("artist", "音乐"),
|
||||
("album", "音乐"),
|
||||
("track", "音乐"),
|
||||
("Movie", "电影"),
|
||||
],
|
||||
)
|
||||
|
||||
@@ -3,9 +3,10 @@ from unittest.mock import Mock, call, patch
|
||||
from xml.etree.ElementTree import fromstring
|
||||
|
||||
import pytest
|
||||
from plexapi.library import MovieSection
|
||||
|
||||
# 注册真实 Plex 电影类型,供列表 XML 解析测试使用。
|
||||
# 注册真实 Plex 音乐和电影类型,供列表 XML 解析测试使用。
|
||||
from plexapi.audio import Album, Artist, Track # noqa: F401 # pylint: disable=unused-import
|
||||
from plexapi.library import MovieSection, MusicSection
|
||||
from plexapi.video import Movie # noqa: F401 # pylint: disable=unused-import
|
||||
|
||||
from app.modules.plex import plex as plex_module
|
||||
@@ -213,3 +214,38 @@ def test_plex_sync_uses_list_metadata_without_reloading_each_movie():
|
||||
headers={"X-Plex-Container-Start": "0", "X-Plex-Container-Size": "50"},
|
||||
params={"excludeElements": plex_module.PLEX_SYNC_EXCLUDE_ELEMENTS, "skipRefresh": 1},
|
||||
)
|
||||
|
||||
|
||||
def test_plex_sync_converts_music_objects_without_detail_reload():
|
||||
"""音乐库中的艺人、专辑和曲目应按各自可用字段转换,不触发详情重载。"""
|
||||
server = Mock()
|
||||
server.query.return_value = fromstring(
|
||||
'<MediaContainer size="3" totalSize="3" librarySectionID="8">'
|
||||
'<Directory type="artist" key="/library/metadata/1" ratingKey="1" title="Artist">'
|
||||
'<Location path="/music/Artist"/></Directory>'
|
||||
'<Directory type="album" key="/library/metadata/2" ratingKey="2" '
|
||||
'title="Album" year="2024"/>'
|
||||
'<Track type="track" key="/library/metadata/3" ratingKey="3" '
|
||||
'title="Song" originalTitle="Artist" year="2024">'
|
||||
'<Media><Part file="/music/Artist/Album/Song.flac"/></Media>'
|
||||
'</Track></MediaContainer>'
|
||||
)
|
||||
section = MusicSection(
|
||||
server, fromstring('<Directory key="8" type="artist" title="Music"/>'),
|
||||
"/library/sections",
|
||||
)
|
||||
client = _make_client(section)
|
||||
del client._Plex__build_media_server_item
|
||||
|
||||
result = list(client.get_items("8"))
|
||||
|
||||
assert [(item.item_type, item.original_title, item.year, item.path) for item in result] == [
|
||||
("artist", None, None, "/music/Artist"),
|
||||
("album", None, 2024, None),
|
||||
("track", "Artist", 2024, "/music/Artist/Album/Song.flac"),
|
||||
]
|
||||
server.query.assert_called_once_with(
|
||||
"/library/sections/8/all?includeGuids=1&type=8",
|
||||
headers={"X-Plex-Container-Start": "0", "X-Plex-Container-Size": "50"},
|
||||
params={"excludeElements": plex_module.PLEX_SYNC_EXCLUDE_ELEMENTS, "skipRefresh": 1},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user