Merge pull request #2116 from InfinityPacer/main

This commit is contained in:
jxxghp 2024-05-19 12:08:03 +08:00 committed by GitHub
commit d20951e7a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -378,26 +378,34 @@ class Plex:
@staticmethod @staticmethod
def __get_ids(guids: List[Any]) -> dict: def __get_ids(guids: List[Any]) -> dict:
def parse_tmdb_id(value: str) -> (bool, int):
"""尝试将TMDB ID字符串转换为整数。如果成功返回(True, int),失败则返回(False, None)。"""
try:
int_value = int(value)
return True, int_value
except ValueError:
return False, None
guid_mapping = { guid_mapping = {
"imdb://": "imdb_id", "imdb://": "imdb_id",
"tmdb://": "tmdb_id", "tmdb://": "tmdb_id",
"tvdb://": "tvdb_id" "tvdb://": "tvdb_id"
} }
ids = {} ids = {varname: None for varname in guid_mapping.values()}
for prefix, varname in guid_mapping.items():
ids[varname] = None
for guid in guids: for guid in guids:
guid_id = guid['id'] if isinstance(guid, dict) else guid.id
for prefix, varname in guid_mapping.items(): for prefix, varname in guid_mapping.items():
if isinstance(guid, dict): if guid_id.startswith(prefix):
if guid['id'].startswith(prefix): clean_id = guid_id[len(prefix):]
# 找到匹配的ID if varname == "tmdb_id":
ids[varname] = guid['id'][len(prefix):] # tmdb_id为intPlex可能存在脏数据特别处理tmdb_id
break success, parsed_id = parse_tmdb_id(clean_id)
else: if success:
if guid.id.startswith(prefix): ids[varname] = parsed_id
# 找到匹配的ID else:
ids[varname] = guid.id[len(prefix):] ids[varname] = clean_id
break break
return ids return ids
def get_items(self, parent: str) -> Generator: def get_items(self, parent: str) -> Generator:
@ -412,25 +420,29 @@ class Plex:
section = self._plex.library.sectionByID(int(parent)) section = self._plex.library.sectionByID(int(parent))
if section: if section:
for item in section.all(): for item in section.all():
if not item: try:
if not item:
continue
ids = self.__get_ids(item.guids)
path = None
if item.locations:
path = item.locations[0]
yield 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,
)
except Exception as e:
logger.error(f"处理媒体项目时出错:{str(e)}, 跳过此项目。")
continue continue
ids = self.__get_ids(item.guids)
path = None
if item.locations:
path = item.locations[0]
yield 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,
)
except Exception as err: except Exception as err:
logger.error(f"获取媒体库列表出错:{str(err)}") logger.error(f"获取媒体库列表出错:{str(err)}")
yield None yield None