From ed536fdc8176d811073817405c134ed09f786b7b Mon Sep 17 00:00:00 2001 From: Luigi311 Date: Sat, 11 Jun 2022 13:29:30 -0600 Subject: [PATCH] Fix plex login via username --- .env.sample | 31 ++++++++++++++++++------------- src/plex.py | 30 +++++++++++++++++++----------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/.env.sample b/.env.sample index 30ec78f..c2e54fc 100644 --- a/.env.sample +++ b/.env.sample @@ -1,33 +1,38 @@ -# Do not mark any shows/movies as played and instead just output to log if they would of been marked. +## Do not mark any shows/movies as played and instead just output to log if they would of been marked. DRYRUN = "True" -# Additional logging information +## Additional logging information DEBUG = "True" -# How often to run the script in seconds +## How often to run the script in seconds SLEEP_DURATION = "3600" -# Log file where all output will be written to +## Log file where all output will be written to LOGFILE = "log.log" -# Map usernames between plex and jellyfin in the event that they are different, order does not matter +## Map usernames between plex and jellyfin in the event that they are different, order does not matter #USER_MAPPING = { "testuser2": "testuser3" } -# Map libraries between plex and jellyfin in the even that they are different, order does not matter +## Map libraries between plex and jellyfin in the even that they are different, order does not matter #LIBRARY_MAPPING = { "Shows": "TV Shows" } -# URL of the plex server, use hostname or IP address if the hostname is not resolving correctly + +## Recommended to use token as it is faster to connect as it is direct to the server instead of going through the plex servers +## URL of the plex server, use hostname or IP address if the hostname is not resolving correctly PLEX_BASEURL = "http://localhost:32400" -# Plex token https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/ +## Plex token https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/ PLEX_TOKEN = "SuperSecretToken" -# If not using plex token then use username and password of the server admin +## If not using plex token then use username and password of the server admin along with the servername #PLEX_USERNAME = "" #PLEX_PASSWORD = "" +#PLEX_SERVERNAME = "Plex Server" -# Jellyfin server URL, use hostname or IP address if the hostname is not resolving correctly + +## Jellyfin server URL, use hostname or IP address if the hostname is not resolving correctly JELLYFIN_BASEURL = "http://localhost:8096" -# Jellyfin api token, created manually by logging in to the jellyfin server admin dashboard and creating an api key +## Jellyfin api token, created manually by logging in to the jellyfin server admin dashboard and creating an api key JELLYFIN_TOKEN = "SuperSecretToken" -# Blacklisting/Whitelisting libraries, library types such as Movies, TV Shows, and users. Mappings apply so if the mapping for the user or library exist then both will be excluded. + +## Blacklisting/Whitelisting libraries, library types such as Movies/TV Shows, and users. Mappings apply so if the mapping for the user or library exist then both will be excluded. #BLACKLIST_LIBRARY = "" #WHITELIST_LIBRARY = "" #BLACKLIST_LIBRARY_TYPE = "" #WHITELIST_LIBRARY_TYPE = "" #BLACKLIST_USERS = "" -WHITELIST_USERS = "testuser1,testuser2" \ No newline at end of file +WHITELIST_USERS = "testuser1,testuser2" diff --git a/src/plex.py b/src/plex.py index d91b906..9ab9aa7 100644 --- a/src/plex.py +++ b/src/plex.py @@ -11,6 +11,7 @@ plex_baseurl = os.getenv("PLEX_BASEURL") plex_token = os.getenv("PLEX_TOKEN") username = os.getenv("PLEX_USERNAME") password = os.getenv("PLEX_PASSWORD") +servername = os.getenv("PLEX_SERVERNAME") # class plex accept base url and token and username and password but default with none class Plex: @@ -19,25 +20,32 @@ class Plex: self.token = plex_token self.username = username self.password = password + self.servername = servername self.plex = self.plex_login() self.admin_user = self.plex.myPlexAccount() self.users = self.get_plex_users() def plex_login(self): - if self.baseurl: - if self.token: - # Login via token - plex = PlexServer(self.baseurl, self.token) - elif self.username and self.password: + try: + if self.baseurl and self.token: + # Login via token + plex = PlexServer(self.baseurl, self.token) + elif self.username and self.password and self.servername: # Login via plex account account = MyPlexAccount(self.username, self.password) - plex = account.resource(self.baseurl).connect() + plex = account.resource(self.servername).connect() else: - raise Exception("No plex credentials provided") - else: - raise Exception("No plex baseurl provided") - - return plex + raise Exception("No complete plex credentials provided") + + return plex + except Exception as e: + if self.username or self.password: + msg = f"Failed to login via plex account {self.username}" + logger(f"Plex: Failed to login, {msg}, Error: {e}", 2) + else: + logger(f"Plex: Failed to login, Error: {e}", 2) + return None + def get_plex_users(self): users = self.plex.myPlexAccount().users()