mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-06-03 14:39:56 +08:00
fix(#2755): handle Plex None values and exceptions in item builder
This commit is contained in:
@@ -392,23 +392,7 @@ class Plex:
|
||||
return None
|
||||
try:
|
||||
item = self.__fetch_item(itemid)
|
||||
ids = self.__get_ids(item.guids)
|
||||
path = None
|
||||
if item.locations:
|
||||
path = item.locations[0]
|
||||
return schemas.MediaServerItem(
|
||||
server="plex",
|
||||
library=item.librarySectionID,
|
||||
item_id=item.key,
|
||||
item_type=item.type,
|
||||
title=item.title,
|
||||
original_title=item.originalTitle,
|
||||
year=item.year,
|
||||
tmdbid=ids['tmdb_id'],
|
||||
imdbid=ids['imdb_id'],
|
||||
tvdbid=ids['tvdb_id'],
|
||||
path=path,
|
||||
)
|
||||
return self.__build_media_server_item(item)
|
||||
except Exception as err:
|
||||
logger.error(f"获取项目详情出错:{str(err)}")
|
||||
return None
|
||||
@@ -454,6 +438,48 @@ class Plex:
|
||||
item_id = int(item_id)
|
||||
return self._plex.fetchItem(item_id)
|
||||
|
||||
def __build_media_server_item(self, item) -> Optional[schemas.MediaServerItem]:
|
||||
"""
|
||||
构造MediaServerItem
|
||||
: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
|
||||
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
|
||||
played = getattr(item, "isPlayed", None) or False
|
||||
play_count = getattr(item, "viewCount", None) or 0
|
||||
last_played_date = getattr(item, "lastViewedAt", None)
|
||||
|
||||
user_state = schemas.MediaServerItemUserState(
|
||||
played=played,
|
||||
resume=playback_position > 0,
|
||||
last_played_date=last_played_date.isoformat() if last_played_date and hasattr(last_played_date,
|
||||
'isoformat') else None,
|
||||
play_count=play_count,
|
||||
percentage=percentage,
|
||||
)
|
||||
|
||||
return schemas.MediaServerItem(
|
||||
id=item.ratingKey,
|
||||
server="plex",
|
||||
library=item.librarySectionID,
|
||||
item_id=item.key,
|
||||
item_type=item.type,
|
||||
title=item.title,
|
||||
original_title=item.originalTitle,
|
||||
year=item.year,
|
||||
tmdbid=ids.get("tmdb_id"),
|
||||
imdbid=ids.get("imdb_id"),
|
||||
tvdbid=ids.get("tvdb_id"),
|
||||
path=path,
|
||||
user_state=user_state,
|
||||
)
|
||||
|
||||
def get_items(self, parent: str, start_index: int = 0, limit: int = 100) -> Generator:
|
||||
"""
|
||||
获取媒体服务器所有媒体库列表
|
||||
@@ -473,41 +499,9 @@ class Plex:
|
||||
try:
|
||||
if not item:
|
||||
continue
|
||||
ids = self.__get_ids(item.guids)
|
||||
path = None
|
||||
if item.locations:
|
||||
path = item.locations[0]
|
||||
playback_position = item.viewOffset if hasattr(item, 'viewOffset') else 0
|
||||
duration = item.duration if hasattr(item, 'duration') else 0
|
||||
percentage = (playback_position / duration * 100) if duration > 0 else None
|
||||
played = item.isPlayed if hasattr(item, 'isPlayed') else False
|
||||
play_count = item.viewCount if hasattr(item, 'viewCount') else 0
|
||||
last_played_date = item.lastViewedAt if hasattr(item, 'lastViewedAt') else None
|
||||
user_state = schemas.MediaServerItemUserState(
|
||||
played=played,
|
||||
resume=playback_position > 0,
|
||||
last_played_date=last_played_date.isoformat() if last_played_date else None,
|
||||
play_count=play_count,
|
||||
percentage=percentage,
|
||||
)
|
||||
|
||||
yield schemas.MediaServerItem(
|
||||
id=item.ratingKey,
|
||||
server="plex",
|
||||
library=item.librarySectionID,
|
||||
item_id=item.key,
|
||||
item_type=item.type,
|
||||
title=item.title,
|
||||
original_title=item.originalTitle,
|
||||
year=item.year,
|
||||
tmdbid=ids['tmdb_id'],
|
||||
imdbid=ids['imdb_id'],
|
||||
tvdbid=ids['tvdb_id'],
|
||||
path=path,
|
||||
user_state=user_state,
|
||||
)
|
||||
yield self.__build_media_server_item(item)
|
||||
except Exception as e:
|
||||
logger.error(f"处理媒体项目时出错:{str(e)}, 跳过此项目。")
|
||||
logger.error(f"处理媒体项目时出错:{str(e)}, 跳过此项目")
|
||||
continue
|
||||
except Exception as err:
|
||||
logger.error(f"获取媒体库列表出错:{str(err)}")
|
||||
|
||||
Reference in New Issue
Block a user