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,35 +222,42 @@ 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:
if mediaType == "movie": stateStrings.append(formatSeconds(item.duration / 1000))
title = f"{item.title} ({item.year})" if mediaType == "movie":
genres: list[Genre] = item.genres[:3] title = f"{item.title} ({item.year})"
stateStrings.append(f"{', '.join(genre.tag for genre in genres)}") genres: list[Genre] = item.genres[:3]
largeText = "Watching a movie" stateStrings.append(f"{', '.join(genre.tag for genre in genres)}")
thumb = item.thumb largeText = "Watching a movie"
elif mediaType == "episode": thumb = item.thumb
title = item.grandparentTitle elif mediaType == "episode":
stateStrings.append(f"S{item.parentIndex:02}E{item.index:02}") title = item.grandparentTitle
stateStrings.append(f"S{item.parentIndex:02}E{item.index:02}")
stateStrings.append(item.title)
largeText = "Watching a TV show"
thumb = item.grandparentThumb
elif mediaType == "livetv":
title = item.grandparentTitle
if item.title != item.grandparentTitle:
stateStrings.append(item.title) stateStrings.append(item.title)
largeText = "Watching a TV show" largeText = "Watching live TV"
thumb = item.grandparentThumb thumb = item.grandparentThumb
else: elif mediaType == "track":
title = item.title
largeText = "Watching a video"
thumb = item.thumb
if state != "playing":
if config["display"]["useRemainingTime"]:
stateStrings.append(f"{formatSeconds((item.duration - viewOffset) / 1000, ':')} left")
else:
stateStrings.append(f"{formatSeconds(viewOffset / 1000, ':')} elapsed")
stateText = " · ".join(stateString for stateString in stateStrings if stateString)
else:
title = item.title title = item.title
stateText = f"{item.originalTitle or item.grandparentTitle} - {item.parentTitle} ({self.server.fetchItem(item.parentRatingKey).year})" stateStrings.append(f"{item.originalTitle or item.grandparentTitle} - {item.parentTitle} ({self.server.fetchItem(item.parentRatingKey).year})")
largeText = "Listening to music" largeText = "Listening to music"
thumb = item.thumb thumb = item.thumb
else:
title = item.title
largeText = "Watching a video"
thumb = item.thumb
if state != "playing" and mediaType != "track":
if config["display"]["useRemainingTime"]:
stateStrings.append(f"{formatSeconds((item.duration - viewOffset) / 1000, ':')} left")
else:
stateStrings.append(f"{formatSeconds(viewOffset / 1000, ':')} elapsed")
stateText = " · ".join(stateString for stateString in stateStrings if stateString)
thumbUrl = "" thumbUrl = ""
if thumb and config["display"]["posters"]["enabled"]: if thumb and config["display"]["posters"]["enabled"]:
thumbUrl = getCacheKey(thumb) thumbUrl = getCacheKey(thumb)