main plex_account needed to read watchlist
MODIFY
plextraktsync/commands/plex_login.py
plextraktsync/commands/plex_login.py
@@ -202,6 +202,7 @@ def login(username: str, password: str):
managed_user = choose_managed_user(account)
if managed_user:
user = managed_user
+ CONFIG["PLEX_OWNER_TOKEN"] = token
token = account.user(managed_user).get_token(plex.machineIdentifier)
CONFIG["PLEX_USERNAME"] = user

MODIFY
plextraktsync/config.py
plextraktsync/config.py
@@ -107,6 +107,7 @@ class Config(dict):
"PLEX_FALLBACKURL", # legacy, used before 0.18.21
"PLEX_LOCALURL",
"PLEX_TOKEN",
+ "PLEX_OWNER_TOKEN",
"PLEX_USERNAME",
"TRAKT_USERNAME",
]

MODIFY
plextraktsync/plex_api.py
plextraktsync/plex_api.py
from plexapi.media import AudioStream, MediaPart, SubtitleStream, VideoStream
from plexapi.server import PlexServer, SystemAccount, SystemDevice
from plexapi.video import Episode, Movie, Show
+from plexapi.myplex import MyPlexAccount
from trakt.utils import timestamp
from plextraktsync.decorators.cached_property import cached_property
@@ -476,6 +477,7 @@ class PlexApi:
def __init__(self, plex: PlexServer):
self.plex = plex
+ self.account = self._plex_account()
@cached_property
def plex_base_url(self):
@@ -622,3 +624,57 @@ def has_sessions(self):
@nocache
def get_sessions(self):
return self.plex.sessions()
+
+ @nocache
+ def _plex_account(self):
+ CONFIG = factory.config()
+ plex_owner_token = CONFIG.get("PLEX_OWNER_TOKEN")
+ plex_username = CONFIG.get("PLEX_USERNAME")
+ if plex_owner_token:
+ try:
+ plex_owner_account = MyPlexAccount(token=plex_owner_token)
+ return plex_owner_account.switchHomeUser(plex_username)
+ except BadRequest as e:
+ logger.error(f"Error during {plex_username} account access: {e}")
+ else:
+ try:
+ return self.plex.myPlexAccount()
+ except BadRequest as e:
+ logger.error(f"Error during {plex_username} account access: {e}")
+ return None
+
+ @nocache
+ def watchlist(self):
+ if self.account:
+ try:
+ return self.account.watchlist()
+ except BadRequest as e:
+ logger.error(f"Error during {self.account.username} watchlist access: {e}")
+ return None
+
+ @nocache
+ def add_to_watchlist(self, item):
+ try:
+ self.account.addToWatchlist(item)
+ except BadRequest as e:
+ logger.error(f"Error when adding {item.title} to Plex watchlist: {e}")
+
+ @nocache
+ def remove_from_watchlist(self, item):
+ try:
+ self.account.removeFromWatchlist(item)
+ except BadRequest as e:
+ logger.error(f"Error when removing {item.title} from Plex watchlist: {e}")
+
+ @retry()
+ def search_online(self, title: str, media_type: str):
+ if not self.account:
+ return None
+ try:
+ result = self.account.searchDiscover(title, libtype=media_type)
+ except (BadRequest, Unauthorized) as e:
+ logger.error(f"{title}: Searching Plex Discover error: {e}")
+ return None
+ except NotFound:
+ return None
+ return map(PlexLibraryItem, result)

main plex_account needed to read watchlist