mirror of
https://github.com/phin05/discord-rich-presence-plex
synced 2025-02-16 13:48:26 +00:00
Update discordRichPresencePlex.py
This commit is contained in:
parent
65c703f800
commit
473ffcc799
1 changed files with 16 additions and 7 deletions
|
@ -1,4 +1,5 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import datetime
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
@ -12,7 +13,7 @@ import time
|
||||||
|
|
||||||
class plexConfig:
|
class plexConfig:
|
||||||
|
|
||||||
extraLogging = False
|
extraLogging = True
|
||||||
|
|
||||||
def __init__(self, serverName = "", username = "", password = "", token = "", listenForUser = ""):
|
def __init__(self, serverName = "", username = "", password = "", token = "", listenForUser = ""):
|
||||||
self.serverName = serverName
|
self.serverName = serverName
|
||||||
|
@ -65,7 +66,7 @@ class discordRichPresence:
|
||||||
emptyProcessFilePath = tempfile.gettempdir() + "\\discordRichPresencePlex-emptyProcess.py"
|
emptyProcessFilePath = tempfile.gettempdir() + "\\discordRichPresencePlex-emptyProcess.py"
|
||||||
if (not os.path.exists(emptyProcessFilePath)):
|
if (not os.path.exists(emptyProcessFilePath)):
|
||||||
with open(emptyProcessFilePath, "w") as emptyProcessFile:
|
with open(emptyProcessFilePath, "w") as emptyProcessFile:
|
||||||
emptyProcessFile.write("import time\n\ntry:\n\twhile (True):\n\t\ttime.sleep(60)\nexcept:\n\tpass")
|
emptyProcessFile.write("import time\n\ntry:\n\twhile (True):\n\t\ttime.sleep(3600)\nexcept:\n\tpass")
|
||||||
self.process = subprocess.Popen(["python3" if isLinux else "pythonw", emptyProcessFilePath])
|
self.process = subprocess.Popen(["python3" if isLinux else "pythonw", emptyProcessFilePath])
|
||||||
self.loop = asyncio.get_event_loop() if isLinux else asyncio.ProactorEventLoop()
|
self.loop = asyncio.get_event_loop() if isLinux else asyncio.ProactorEventLoop()
|
||||||
self.loop.run_until_complete(self.handshake())
|
self.loop.run_until_complete(self.handshake())
|
||||||
|
@ -92,6 +93,8 @@ class discordRichPresence:
|
||||||
class discordRichPresencePlex(discordRichPresence):
|
class discordRichPresencePlex(discordRichPresence):
|
||||||
|
|
||||||
productName = "Plex Media Server"
|
productName = "Plex Media Server"
|
||||||
|
plexAccount = None
|
||||||
|
plexServer = None
|
||||||
lastState = None
|
lastState = None
|
||||||
lastSessionKey = None
|
lastSessionKey = None
|
||||||
lastRatingKey = None
|
lastRatingKey = None
|
||||||
|
@ -137,10 +140,12 @@ class discordRichPresencePlex(discordRichPresence):
|
||||||
|
|
||||||
def checkConnection(self):
|
def checkConnection(self):
|
||||||
try:
|
try:
|
||||||
self.plexAccount.users()
|
self.log("Request for server sessions list to check connection: " + str(self.plexServer.sessions()), extra = True)
|
||||||
self.checkConnectionTimer = threading.Timer(self.checkConnectionTimerInterval, self.checkConnection)
|
self.checkConnectionTimer = threading.Timer(self.checkConnectionTimerInterval, self.checkConnection)
|
||||||
self.checkConnectionTimer.start()
|
self.checkConnectionTimer.start()
|
||||||
except:
|
except Exception as e:
|
||||||
|
self.plexAccount = None
|
||||||
|
self.plexServer = None
|
||||||
if (self.stopTimer):
|
if (self.stopTimer):
|
||||||
self.stopTimer.cancel()
|
self.stopTimer.cancel()
|
||||||
self.stopTimer = None
|
self.stopTimer = None
|
||||||
|
@ -149,11 +154,13 @@ class discordRichPresencePlex(discordRichPresence):
|
||||||
self.stopTimer2 = None
|
self.stopTimer2 = None
|
||||||
if (self.running):
|
if (self.running):
|
||||||
self.stop()
|
self.stop()
|
||||||
self.log("Connection to Plex lost, reconnecting")
|
self.log("Connection to Plex lost: " + str(e))
|
||||||
|
self.log("Reconnecting")
|
||||||
self.run()
|
self.run()
|
||||||
|
|
||||||
def log(self, text, colour = "", extra = False):
|
def log(self, text, colour = "", extra = False):
|
||||||
prefix = "[" + self.plexConfig.serverName + "/" + self.instanceID + "] "
|
timestamp = datetime.datetime.now().strftime("%I:%M:%S %p")
|
||||||
|
prefix = "[" + timestamp + "] [" + self.plexConfig.serverName + "/" + self.instanceID + "] "
|
||||||
lock.acquire()
|
lock.acquire()
|
||||||
if (extra):
|
if (extra):
|
||||||
if (self.plexConfig.extraLogging):
|
if (self.plexConfig.extraLogging):
|
||||||
|
@ -164,6 +171,8 @@ class discordRichPresencePlex(discordRichPresence):
|
||||||
|
|
||||||
def onPlexServerAlert(self, data):
|
def onPlexServerAlert(self, data):
|
||||||
try:
|
try:
|
||||||
|
if (not self.plexServer):
|
||||||
|
return
|
||||||
if (data["type"] == "playing" and "PlaySessionStateNotification" in data):
|
if (data["type"] == "playing" and "PlaySessionStateNotification" in data):
|
||||||
sessionData = data["PlaySessionStateNotification"][0]
|
sessionData = data["PlaySessionStateNotification"][0]
|
||||||
state = sessionData["state"]
|
state = sessionData["state"]
|
||||||
|
@ -261,7 +270,7 @@ class discordRichPresencePlex(discordRichPresence):
|
||||||
self.start()
|
self.start()
|
||||||
self.send(activity)
|
self.send(activity)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.log("Error: " + str(e))
|
self.log("onPlexServerAlert Error: " + str(e))
|
||||||
if (self.process):
|
if (self.process):
|
||||||
self.process.kill()
|
self.process.kill()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue