This commit is contained in:
Phin 2022-05-22 22:33:17 +05:30
parent 9f1113c07f
commit 98846bc216
4 changed files with 42 additions and 42 deletions

View file

@ -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). 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 ## Getting Started
@ -25,7 +25,7 @@ The script must be running on the same machine as your Discord client.
* `logging` * `logging`
* `debug` (boolean, default: `true`) - Outputs additional debug-helpful information to the console if enabled. * `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` * `display`
* `useRemainingTime` (boolean, default: `false`) - Displays your media's remaining time instead of elapsed time in your Rich Presence if enabled. * `useRemainingTime` (boolean, default: `false`) - Displays your media's remaining time instead of elapsed time in your Rich Presence if enabled.
* `posters` * `posters`

75
main.py
View file

@ -10,52 +10,51 @@ import sys
import time import time
import urllib.parse import urllib.parse
loadConfig() plexAlertListeners: list[PlexAlertListener] = []
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") try:
logger.info("%s - v%s", name, version) loadConfig()
loadCache() if config["logging"]["debug"]:
logger.setLevel(logging.DEBUG)
if len(config["users"]) == 0: if config["logging"]["writeToFile"]:
logger.info("No users found in the config file. Initiating authentication flow.") fileHandler = logging.FileHandler(logFilePath)
response = requests.post("https://plex.tv/api/v2/pins.json?strong=true", headers = { fileHandler.setFormatter(formatter)
"X-Plex-Product": name, logger.addHandler(fileHandler)
"X-Plex-Client-Identifier": plexClientID, os.system("clear" if isUnix else "cls")
}).json() logger.info("%s - v%s", name, version)
logger.info("Open the below URL in your web browser and sign in:") loadCache()
logger.info("https://app.plex.tv/auth#?clientID=%s&code=%s&context%%5Bdevice%%5D%%5Bproduct%%5D=%s", plexClientID, response["code"], urllib.parse.quote(name)) if len(config["users"]) == 0:
time.sleep(5) logger.info("No users found in the config file. Initiating authentication flow.")
logger.info("Checking whether authentication is successful...") response = requests.post("https://plex.tv/api/v2/pins.json?strong=true", headers = {
for _ in range(120): "X-Plex-Product": name,
authCheckResponse = requests.get(f"https://plex.tv/api/v2/pins/{response['id']}.json?code={response['code']}", headers = {
"X-Plex-Client-Identifier": plexClientID, "X-Plex-Client-Identifier": plexClientID,
}).json() }).json()
if authCheckResponse["authToken"]: logger.info("Open the below URL in your web browser and sign in:")
logger.info("Authentication successful.") logger.info("https://app.plex.tv/auth#?clientID=%s&code=%s&context%%5Bdevice%%5D%%5Bproduct%%5D=%s", plexClientID, response["code"], urllib.parse.quote(name))
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) time.sleep(5)
else: logger.info("Checking whether authentication is successful...")
logger.info("Authentication failed.") for _ in range(120):
exit() authCheckResponse = requests.get(f"https://plex.tv/api/v2/pins/{response['id']}.json?code={response['code']}", headers = {
"X-Plex-Client-Identifier": plexClientID,
plexAlertListeners: list[PlexAlertListener] = [] }).json()
try: 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"]] 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() userInput = input()
if userInput in ["exit", "quit"]: if userInput in ["exit", "quit"]:
raise KeyboardInterrupt raise KeyboardInterrupt
else: else:
while True:
time.sleep(3600) time.sleep(3600)
except KeyboardInterrupt: except KeyboardInterrupt:
for plexAlertListener in plexAlertListeners: for plexAlertListener in plexAlertListeners:

View file

@ -44,8 +44,9 @@ class PlexAlertListener(threading.Thread):
connected = False connected = False
while not connected: while not connected:
try: try:
self.logger.info("Signing into Plex")
self.account = MyPlexAccount(token = self.token) 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.listenForUser = self.serverConfig.get("listenForUser", self.account.username)
self.server = None self.server = None
for resource in self.account.resources(): for resource in self.account.resources():

View file

@ -2,7 +2,7 @@ import os
import sys import sys
name = "Discord Rich Presence for Plex" name = "Discord Rich Presence for Plex"
version = "2.2.3" version = "2.2.4"
plexClientID = "discord-rich-presence-plex" plexClientID = "discord-rich-presence-plex"
discordClientID = "413407336082833418" discordClientID = "413407336082833418"