Update discordRichPresencePlex.py

This commit is contained in:
Phin 2018-11-16 02:58:55 +05:30
parent bc7c4bd59a
commit 0588e38fed

View file

@ -14,6 +14,7 @@ import time
class plexConfig: class plexConfig:
extraLogging = True extraLogging = True
timeRemaining = False
def __init__(self, serverName = "", username = "", password = "", token = "", listenForUser = "", clientID = "413407336082833418"): def __init__(self, serverName = "", username = "", password = "", token = "", listenForUser = "", clientID = "413407336082833418"):
self.serverName = serverName self.serverName = serverName
@ -283,14 +284,18 @@ class discordRichPresencePlex(discordRichPresence):
if (mediaType == "movie"): if (mediaType == "movie"):
title = metadata.title + " (" + str(metadata.year) + ")" title = metadata.title + " (" + str(metadata.year) + ")"
if (state != "playing"): if (state != "playing"):
extra = str(time.strftime("%H:%M:%S", time.gmtime(viewOffset / 1000))) + "/" + str(time.strftime("%H:%M:%S", time.gmtime(metadata.duration / 1000))) extra = secondsToText(viewOffset / 1000, 2) + "/" + secondsToText(metadata.duration / 1000, 2)
else: else:
extra = str(time.strftime("%Hh%Mm", time.gmtime(metadata.duration / 1000))) extra = secondsToText(metadata.duration / 1000)
extra = extra + " · " + ", ".join([genre.tag for genre in metadata.genres[:3]]) extra = extra + " · " + ", ".join([genre.tag for genre in metadata.genres[:3]])
largeText = "Watching a Movie" largeText = "Watching a Movie"
elif (mediaType == "episode"): elif (mediaType == "episode"):
title = metadata.grandparentTitle title = metadata.grandparentTitle
extra = "S" + str(metadata.parentIndex) + " · E" + str(metadata.index) + " - " + metadata.title if (state != "playing"):
extra = secondsToText(viewOffset / 1000, 2) + "/" + secondsToText(metadata.duration / 1000, 2)
else:
extra = secondsToText(metadata.duration / 1000)
extra = extra + " · S" + str(metadata.parentIndex) + " · E" + str(metadata.index) + " - " + metadata.title
largeText = "Watching a TV Show" largeText = "Watching a TV Show"
elif (mediaType == "track"): elif (mediaType == "track"):
title = metadata.title title = metadata.title
@ -314,7 +319,10 @@ class discordRichPresencePlex(discordRichPresence):
} }
if (state == "playing"): if (state == "playing"):
currentTimestamp = int(time.time()) currentTimestamp = int(time.time())
activity["timestamps"] = {"start": currentTimestamp - (viewOffset / 1000)} # "end": currentTimestamp + ((metadata.duration - viewOffset) / 1000) if (self.plexConfig.timeRemaining):
activity["timestamps"] = {"end": currentTimestamp + ((metadata.duration - viewOffset) / 1000)}
else:
activity["timestamps"] = {"start": currentTimestamp - (viewOffset / 1000)}
if (not self.running): if (not self.running):
self.start() self.start()
if (self.running): if (self.running):
@ -355,6 +363,15 @@ def colourText(text, colour = ""):
suffix = "\033[0m" suffix = "\033[0m"
return prefix + str(text) + suffix return prefix + str(text) + suffix
def secondsToText(seconds, formatting = 1):
seconds = round(seconds)
text = {"h": seconds // 3600, "m": seconds // 60 % 60, "s": seconds % 60}
if (formatting == 1):
text = [str(v) + k for k, v in text.items() if v > 0]
return "".join(text)
else:
return ":".join(list(str(v).rjust(2, "0") for k, v in text.items()))
discordRichPresencePlexInstances = [] discordRichPresencePlexInstances = []
for config in plexConfigs: for config in plexConfigs:
discordRichPresencePlexInstances.append(discordRichPresencePlex(config)) discordRichPresencePlexInstances.append(discordRichPresencePlex(config))