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).
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`

75
main.py
View file

@ -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:

View file

@ -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():

View file

@ -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"