From 8108dd8a398a17acefb82f432b19cd2d09b46607 Mon Sep 17 00:00:00 2001 From: RemiRigal Date: Tue, 19 Jul 2022 14:43:24 +0200 Subject: [PATCH] Add cache for instance users (#52) --- plex_auto_languages/plex_server.py | 20 ++++++++++++++------ plex_auto_languages/plex_server_cache.py | 15 ++++++++++++++- tests/test_plex_server.py | 10 ++++++++++ tests/test_plex_server_cache.py | 8 ++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/plex_auto_languages/plex_server.py b/plex_auto_languages/plex_server.py index 1f0d0b9..c7e8261 100644 --- a/plex_auto_languages/plex_server.py +++ b/plex_auto_languages/plex_server.py @@ -151,13 +151,21 @@ def start_alert_listener(self, error_callback: Callable): self._alert_listener.start() def get_instance_users(self): + users = self.cache.get_instance_users() + if users is not None: + return users users = [] - for user in self._plex.myPlexAccount().users(): - server_identifiers = [share.machineIdentifier for share in user.servers] - if self.unique_id in server_identifiers: - user.name = user.title - users.append(user) - return users + try: + for user in self._plex.myPlexAccount().users(): + server_identifiers = [share.machineIdentifier for share in user.servers] + if self.unique_id in server_identifiers: + user.name = user.title + users.append(user) + self.cache.set_instance_users(users) + return users + except BadRequest: + logger.warning("Unable to retrieve the users of the account, falling back to cache") + return self.cache.get_instance_users(check_validity=False) def get_all_user_ids(self): return [self.user_id] + [user.id for user in self.get_instance_users()] diff --git a/plex_auto_languages/plex_server_cache.py b/plex_auto_languages/plex_server_cache.py index 1ba2120..a82eebb 100644 --- a/plex_auto_languages/plex_server_cache.py +++ b/plex_auto_languages/plex_server_cache.py @@ -1,8 +1,9 @@ from __future__ import annotations import os import json -from datetime import datetime +import copy from typing import TYPE_CHECKING +from datetime import datetime, timedelta from dateutil.parser import isoparse from plex_auto_languages.utils.logger import get_logger @@ -30,6 +31,9 @@ def __init__(self, plex: PlexServer): self.newly_added = {} # episode_id: added_at self.newly_updated = {} # episode_id: updated_at self.recent_activities = {} # (user_id, item_id): timestamp + # Users cache + self._instance_users = [] + self._instance_users_valid_until = datetime.fromtimestamp(0) # Library cache self.episode_parts = {} # Initialization @@ -75,6 +79,15 @@ def refresh_library_cache(self): self._is_refreshing = False return added, updated + def get_instance_users(self, check_validity=True): + if check_validity and datetime.now() > self._instance_users_valid_until: + return None + return copy.deepcopy(self._instance_users) + + def set_instance_users(self, instance_users): + self._instance_users = copy.deepcopy(instance_users) + self._instance_users_valid_until = datetime.now() + timedelta(hours=12) + def _get_cache_file_path(self): data_dir = self._plex.config.get("data_dir") cache_dir = os.path.join(data_dir, "cache") diff --git a/tests/test_plex_server.py b/tests/test_plex_server.py index ab3fce3..e9e0249 100644 --- a/tests/test_plex_server.py +++ b/tests/test_plex_server.py @@ -4,6 +4,7 @@ from datetime import datetime from unittest.mock import patch from plexapi.video import Episode, Show +from plexapi.exceptions import BadRequest from plexapi.server import PlexServer as BasePlexServer from plex_auto_languages.track_changes import TrackChanges, NewOrUpdatedTrackChanges @@ -98,6 +99,15 @@ def test_get_logged_user(plex): assert user is None +def test_get_instance_users(plex): + with patch.object(BasePlexServer, "myPlexAccount", side_effect=BadRequest()): + assert plex.get_instance_users() == [] + assert len(plex.get_instance_users()) == 1 + assert plex.cache._instance_users_valid_until > datetime.fromtimestamp(0) + with patch.object(BasePlexServer, "myPlexAccount", side_effect=BadRequest()): + assert len(plex.get_instance_users()) == 1 + + def test_get_user_by_id(plex): user = plex.get_user_by_id(plex.user_id) assert user.id == plex.user_id diff --git a/tests/test_plex_server_cache.py b/tests/test_plex_server_cache.py index 6c4b454..0f25766 100644 --- a/tests/test_plex_server_cache.py +++ b/tests/test_plex_server_cache.py @@ -48,6 +48,14 @@ def test_load_save(): assert old_episode_parts == cache.episode_parts +def test_instance_users(plex): + assert plex.cache.get_instance_users() is None + assert plex.cache.get_instance_users(check_validity=False) == [] + + plex.cache.set_instance_users(["user1", "user2"]) + assert len(plex.cache.get_instance_users()) == 2 + + def test_refresh(plex): keys = list(plex.cache.episode_parts.keys()) assert len(keys) > 1