fix dashboard Plex卡片加载速度

This commit is contained in:
InfinityPacer 2024-06-10 12:43:05 +08:00
parent fcbdef5e66
commit 95202af139

View File

@ -1,9 +1,9 @@
import json import json
from functools import lru_cache
from pathlib import Path from pathlib import Path
from typing import List, Optional, Dict, Tuple, Generator, Any from typing import List, Optional, Dict, Tuple, Generator, Any
from urllib.parse import quote_plus from urllib.parse import quote_plus
from cachetools import TTLCache, cached
from plexapi import media from plexapi import media
from plexapi.server import PlexServer from plexapi.server import PlexServer
@ -14,7 +14,6 @@ from app.schemas import MediaType
class Plex: class Plex:
_plex = None _plex = None
def __init__(self): def __init__(self):
@ -58,7 +57,7 @@ class Plex:
self._plex = None self._plex = None
logger.error(f"Plex服务器连接失败{str(e)}") logger.error(f"Plex服务器连接失败{str(e)}")
@lru_cache(maxsize=10) @cached(cache=TTLCache(maxsize=100, ttl=86400))
def __get_library_images(self, library_key: str, mtype: int) -> Optional[List[str]]: def __get_library_images(self, library_key: str, mtype: int) -> Optional[List[str]]:
""" """
获取媒体服务器最近添加的媒体的图片列表 获取媒体服务器最近添加的媒体的图片列表
@ -76,10 +75,11 @@ class Plex:
# 如果总数不足,接续获取下一页 # 如果总数不足,接续获取下一页
while len(poster_urls) < total_size: while len(poster_urls) < total_size:
items = self._plex.fetchItems(f"/hubs/home/recentlyAdded?type={mtype}&sectionID={library_key}", items = self._plex.fetchItems(f"/hubs/home/recentlyAdded?type={mtype}&sectionID={library_key}",
container_size=total_size, container_start=container_start,
container_start=container_start) container_size=8,
maxresults=8)
for item in items: for item in items:
if item.type == 'episode': if item.type == "episode":
# 如果是剧集的单集,则去找上级的图片 # 如果是剧集的单集,则去找上级的图片
if item.parentThumb is not None: if item.parentThumb is not None:
poster_urls[item.parentThumb] = None poster_urls[item.parentThumb] = None
@ -631,8 +631,12 @@ class Plex:
return [] return []
# 媒体库白名单 # 媒体库白名单
allow_library = ",".join([lib.id for lib in self.get_librarys()]) allow_library = ",".join([lib.id for lib in self.get_librarys()])
params = {'contentDirectoryID': allow_library} params = {"contentDirectoryID": allow_library}
items = self._plex.fetchItems("/hubs/continueWatching/items", container_start=0, container_size=num, params=params) items = self._plex.fetchItems("/hubs/continueWatching/items",
container_start=0,
container_size=num,
maxresults=num,
params=params)
ret_resume = [] ret_resume = []
for item in items: for item in items:
item_type = MediaType.MOVIE.value if item.TYPE == "movie" else MediaType.TV.value item_type = MediaType.MOVIE.value if item.TYPE == "movie" else MediaType.TV.value