Reuse server_1_watched history to avoid duplication

Keeps the server_1_watched history that way it does not need to fetch
the same results again each time it needs to sync to another server

Signed-off-by: Luis Garcia <git@luigi311.com>
This commit is contained in:
Luis Garcia
2025-03-05 10:48:00 -07:00
parent c1a26dd73b
commit c3be980eea
4 changed files with 158 additions and 12 deletions

View File

@@ -279,10 +279,14 @@ class Plex:
return LibraryData(title=library.title)
def get_watched(
self, users: list[MyPlexUser | MyPlexAccount], sync_libraries: list[str]
self,
users: list[MyPlexUser | MyPlexAccount],
sync_libraries: list[str],
users_watched: dict[str, UserData] = None,
) -> dict[str, UserData]:
try:
users_watched: dict[str, UserData] = {}
if not users_watched:
users_watched: dict[str, UserData] = {}
for user in users:
if self.admin_user == user:
@@ -307,18 +311,24 @@ class Plex:
if library.title not in sync_libraries:
continue
if user_name not in users_watched:
users_watched[user_name] = UserData()
if library.title in users_watched[user_name].libraries:
logger.info(
f"Plex: {user_name} {library.title} watched history has already been gathered, skipping"
)
continue
library_data = self.get_user_library_watched(
user_name, user_plex, library
)
if user_name not in users_watched:
users_watched[user_name] = UserData()
users_watched[user_name].libraries[library.title] = library_data
return users_watched
except Exception as e:
logger.error(f"Plex: Failed to get watched, Error: {e}")
logger.error(f"Plex: Failed to get users watched, Error: {e}")
return {}
def update_user_watched(