From 98846bc216de1de513dab72bf47f9bc768f98423 Mon Sep 17 00:00:00 2001 From: Phin <59180111+phin05@users.noreply.github.com> Date: Sun, 22 May 2022 22:33:17 +0530 Subject: [PATCH] Tweaks --- README.md | 4 +- main.py | 75 +++++++++++++++++------------------ services/PlexAlertListener.py | 3 +- store/constants.py | 2 +- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 6297290..cd3ba9e 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A Python script that displays your [Plex](https://www.plex.tv) status on [Discord](https://discord.com) using [Rich Presence](https://discord.com/developers/docs/rich-presence/how-to). -Current Version: 2.2.3 +Current Version: 2.2.4 ## Getting Started @@ -25,7 +25,7 @@ The script must be running on the same machine as your Discord client. * `logging` * `debug` (boolean, default: `true`) - Outputs additional debug-helpful information to the console if enabled. - * `writeToFile` (boolean, default: `false`) - Writes everything outputted to console to a `console.log` file if enabled. + * `writeToFile` (boolean, default: `false`) - Writes everything outputted to the console to a `console.log` file if enabled. * `display` * `useRemainingTime` (boolean, default: `false`) - Displays your media's remaining time instead of elapsed time in your Rich Presence if enabled. * `posters` diff --git a/main.py b/main.py index 1017999..ce08bad 100644 --- a/main.py +++ b/main.py @@ -10,52 +10,51 @@ import sys import time import urllib.parse -loadConfig() -if config["logging"]["debug"]: - logger.setLevel(logging.DEBUG) -if config["logging"]["writeToFile"]: - fileHandler = logging.FileHandler(logFilePath) - fileHandler.setFormatter(formatter) - logger.addHandler(fileHandler) +plexAlertListeners: list[PlexAlertListener] = [] -os.system("clear" if isUnix else "cls") -logger.info("%s - v%s", name, version) -loadCache() - -if len(config["users"]) == 0: - logger.info("No users found in the config file. Initiating authentication flow.") - response = requests.post("https://plex.tv/api/v2/pins.json?strong=true", headers = { - "X-Plex-Product": name, - "X-Plex-Client-Identifier": plexClientID, - }).json() - logger.info("Open the below URL in your web browser and sign in:") - logger.info("https://app.plex.tv/auth#?clientID=%s&code=%s&context%%5Bdevice%%5D%%5Bproduct%%5D=%s", plexClientID, response["code"], urllib.parse.quote(name)) - time.sleep(5) - logger.info("Checking whether authentication is successful...") - for _ in range(120): - authCheckResponse = requests.get(f"https://plex.tv/api/v2/pins/{response['id']}.json?code={response['code']}", headers = { +try: + loadConfig() + if config["logging"]["debug"]: + logger.setLevel(logging.DEBUG) + if config["logging"]["writeToFile"]: + fileHandler = logging.FileHandler(logFilePath) + fileHandler.setFormatter(formatter) + logger.addHandler(fileHandler) + os.system("clear" if isUnix else "cls") + logger.info("%s - v%s", name, version) + loadCache() + if len(config["users"]) == 0: + logger.info("No users found in the config file. Initiating authentication flow.") + response = requests.post("https://plex.tv/api/v2/pins.json?strong=true", headers = { + "X-Plex-Product": name, "X-Plex-Client-Identifier": plexClientID, }).json() - if authCheckResponse["authToken"]: - logger.info("Authentication successful.") - serverName = input("Enter the name of the Plex Media Server you wish to connect to: ") - config["users"].append({ "token": authCheckResponse["authToken"], "servers": [{ "name": serverName }] }) - saveConfig() - break + logger.info("Open the below URL in your web browser and sign in:") + logger.info("https://app.plex.tv/auth#?clientID=%s&code=%s&context%%5Bdevice%%5D%%5Bproduct%%5D=%s", plexClientID, response["code"], urllib.parse.quote(name)) time.sleep(5) - else: - logger.info("Authentication failed.") - exit() - -plexAlertListeners: list[PlexAlertListener] = [] -try: + logger.info("Checking whether authentication is successful...") + for _ in range(120): + authCheckResponse = requests.get(f"https://plex.tv/api/v2/pins/{response['id']}.json?code={response['code']}", headers = { + "X-Plex-Client-Identifier": plexClientID, + }).json() + if authCheckResponse["authToken"]: + logger.info("Authentication successful.") + serverName = input("Enter the name of the Plex Media Server you wish to connect to: ") + config["users"].append({ "token": authCheckResponse["authToken"], "servers": [{ "name": serverName }] }) + saveConfig() + break + time.sleep(5) + else: + logger.info("Authentication failed.") + exit() plexAlertListeners = [PlexAlertListener(user["token"], server) for user in config["users"] for server in user["servers"]] - while True: - if sys.stdin: + if sys.stdin: + while True: userInput = input() if userInput in ["exit", "quit"]: raise KeyboardInterrupt - else: + else: + while True: time.sleep(3600) except KeyboardInterrupt: for plexAlertListener in plexAlertListeners: diff --git a/services/PlexAlertListener.py b/services/PlexAlertListener.py index 56526e6..f494192 100644 --- a/services/PlexAlertListener.py +++ b/services/PlexAlertListener.py @@ -44,8 +44,9 @@ class PlexAlertListener(threading.Thread): connected = False while not connected: try: + self.logger.info("Signing into Plex") self.account = MyPlexAccount(token = self.token) - self.logger.info("Signed in as Plex User \"%s\"", self.account.username) + self.logger.info("Signed in as Plex user \"%s\"", self.account.username) self.listenForUser = self.serverConfig.get("listenForUser", self.account.username) self.server = None for resource in self.account.resources(): diff --git a/store/constants.py b/store/constants.py index 92a3763..21b30fa 100644 --- a/store/constants.py +++ b/store/constants.py @@ -2,7 +2,7 @@ import os import sys name = "Discord Rich Presence for Plex" -version = "2.2.3" +version = "2.2.4" plexClientID = "discord-rich-presence-plex" discordClientID = "413407336082833418"