Added support for live TV

This commit is contained in:
Phin 2024-02-12 17:00:32 +05:30
parent 1a7b7aad58
commit 7aab6fa6d4
2 changed files with 39 additions and 28 deletions

View file

@ -45,7 +45,7 @@ The config file is stored in a directory named `data`.
* `buttons` (list) - [Information](#buttons) * `buttons` (list) - [Information](#buttons)
* `label` (string) - The label to be displayed on the button. * `label` (string) - The label to be displayed on the button.
* `url` (string) - A web address or a [dynamic URL placeholder](#dynamic-button-urls). * `url` (string) - A web address or a [dynamic URL placeholder](#dynamic-button-urls).
* `mediaTypes` (list, optional) - If set, the button is displayed only for the specified media types. Valid media types are `movie`, `episode`, `track` and `clip`. * `mediaTypes` (list, optional) - If set, the button is displayed only for the specified media types. Valid media types are `movie`, `episode`, `live_episode`, `track` and `clip`.
* `users` (list) * `users` (list)
* `token` (string) - An access token associated with your Plex account. ([X-Plex-Token](https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/), [Authenticating with Plex](https://forums.plex.tv/t/authenticating-with-plex/609370)) * `token` (string) - An access token associated with your Plex account. ([X-Plex-Token](https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/), [Authenticating with Plex](https://forums.plex.tv/t/authenticating-with-plex/609370))
* `servers` (list) * `servers` (list)

View file

@ -34,7 +34,7 @@ def getAuthToken(id: str, code: str) -> Optional[str]:
}).json() }).json()
return response["authToken"] return response["authToken"]
validMediaTypes = ["movie", "episode", "track", "clip"] validMediaTypes = ["movie", "episode", "live_episode", "track", "clip"]
buttonTypeGuidTypeMap = { buttonTypeGuidTypeMap = {
"imdb": "imdb", "imdb": "imdb",
"tmdb": "tmdb", "tmdb": "tmdb",
@ -157,7 +157,11 @@ class PlexAlertListener(threading.Thread):
ratingKey = int(stateNotification["ratingKey"]) ratingKey = int(stateNotification["ratingKey"])
assert self.server assert self.server
item: PlexPartialObject = self.server.fetchItem(ratingKey) item: PlexPartialObject = self.server.fetchItem(ratingKey)
mediaType: str = item.type mediaType: str
if item.key and item.key.startswith("/livetv"):
mediaType = "live_episode"
else:
mediaType = item.type
if mediaType not in validMediaTypes: if mediaType not in validMediaTypes:
self.logger.debug("Unsupported media type '%s', ignoring", mediaType) self.logger.debug("Unsupported media type '%s', ignoring", mediaType)
return return
@ -218,8 +222,9 @@ class PlexAlertListener(threading.Thread):
self.lastState, self.lastSessionKey, self.lastRatingKey = state, sessionKey, ratingKey self.lastState, self.lastSessionKey, self.lastRatingKey = state, sessionKey, ratingKey
title: str title: str
thumb: str thumb: str
if mediaType in ["movie", "episode", "clip"]: stateStrings: list[str] = []
stateStrings: list[str] = [] if config["display"]["hideTotalTime"] else [formatSeconds(item.duration / 1000)] if not config["display"]["hideTotalTime"] and item.duration:
stateStrings.append(formatSeconds(item.duration / 1000))
if mediaType == "movie": if mediaType == "movie":
title = f"{item.title} ({item.year})" title = f"{item.title} ({item.year})"
genres: list[Genre] = item.genres[:3] genres: list[Genre] = item.genres[:3]
@ -232,21 +237,27 @@ class PlexAlertListener(threading.Thread):
stateStrings.append(item.title) stateStrings.append(item.title)
largeText = "Watching a TV show" largeText = "Watching a TV show"
thumb = item.grandparentThumb thumb = item.grandparentThumb
elif mediaType == "livetv":
title = item.grandparentTitle
if item.title != item.grandparentTitle:
stateStrings.append(item.title)
largeText = "Watching live TV"
thumb = item.grandparentThumb
elif mediaType == "track":
title = item.title
stateStrings.append(f"{item.originalTitle or item.grandparentTitle} - {item.parentTitle} ({self.server.fetchItem(item.parentRatingKey).year})")
largeText = "Listening to music"
thumb = item.thumb
else: else:
title = item.title title = item.title
largeText = "Watching a video" largeText = "Watching a video"
thumb = item.thumb thumb = item.thumb
if state != "playing": if state != "playing" and mediaType != "track":
if config["display"]["useRemainingTime"]: if config["display"]["useRemainingTime"]:
stateStrings.append(f"{formatSeconds((item.duration - viewOffset) / 1000, ':')} left") stateStrings.append(f"{formatSeconds((item.duration - viewOffset) / 1000, ':')} left")
else: else:
stateStrings.append(f"{formatSeconds(viewOffset / 1000, ':')} elapsed") stateStrings.append(f"{formatSeconds(viewOffset / 1000, ':')} elapsed")
stateText = " · ".join(stateString for stateString in stateStrings if stateString) stateText = " · ".join(stateString for stateString in stateStrings if stateString)
else:
title = item.title
stateText = f"{item.originalTitle or item.grandparentTitle} - {item.parentTitle} ({self.server.fetchItem(item.parentRatingKey).year})"
largeText = "Listening to music"
thumb = item.thumb
thumbUrl = "" thumbUrl = ""
if thumb and config["display"]["posters"]["enabled"]: if thumb and config["display"]["posters"]["enabled"]:
thumbUrl = getCacheKey(thumb) thumbUrl = getCacheKey(thumb)