- type: custom-api title: Media Server History frameless: true cache: 5m options: media-server: '${MEDIA_SERVER_TYPE}' base-url: ${JELLYFIN_URL} api-key: ${JELLYFIN_TOKEN} user-name: ${JELLYFIN_USER} media-types: '${MEDIA_SERVER_TYPES}' history-length: '${MEDIA_SERVER_HISTORY_LENGTH}' small-column: true compact: true show-thumbnail: true thumbnail-aspect-ratio: 'default' show-user: false time-absolute: false time-format: 'Jan 02 15:04' template: | {{ $mediaServer := .Options.StringOr "media-server" "" }} {{ $baseURL := .Options.StringOr "base-url" "" }} {{ $apiKey := .Options.StringOr "api-key" "" }} {{ $userName := .Options.StringOr "user-name" "" }} {{ define "errorMsg" }}
ERROR

{{ . }}

{{ end }} {{ if or (eq $mediaServer "") (eq $baseURL "") (eq $apiKey "") (and (eq $mediaServer "jellyfin") (eq $userName "")) }} {{ template "errorMsg" "Some required options are not set" }} {{ else }} {{ $historyLength := .Options.StringOr "history-length" "10" }} {{ $mediaTypes := .Options.StringOr "media-types" "" }} {{ if eq $mediaServer "tautulli" }} {{ $mediaTypes = .Options.StringOr "media-types" "movie,episode,track" }} {{ else if or (eq $mediaServer "jellyfin") (eq $mediaServer "emby") }} {{ $mediaTypes = .Options.StringOr "media-types" "Movie,Episode,Audio" }} {{ end }} {{ $isSmallColumn := .Options.BoolOr "small-column" false }} {{ $isCompact := .Options.BoolOr "compact" true }} {{ $showThumbnail := .Options.BoolOr "show-thumbnail" false }} {{ $thumbAspectRatio := .Options.StringOr "thumbnail-aspect-ratio" "" }} {{ $showUser := .Options.BoolOr "show-user" true }} {{ $timeAbsolute := .Options.BoolOr "time-absolute" false }} {{ $timeFormat := .Options.StringOr "time-format" "Jan 02 15:04" }} {{ $userID := "" }} {{ $historyRequestURL := "" }} {{ $usersRequestURL := "" }} {{ $historyCall := "" }} {{ $usersCall := "" }} {{ $history := "" }} {{ $users := "" }} {{ if eq $mediaServer "plex" }} {{ $historyRequestURL = concat $baseURL "/status/sessions/history/all" }} {{ $historyCall = newRequest $historyRequestURL | withParameter "limit" $historyLength | withParameter "sort" "viewedAt:desc" | withHeader "Accept" "application/json" | withHeader "X-Plex-Token" $apiKey | getResponse }} {{ if $historyCall.JSON.Exists "MediaContainer" }} {{ $history = $historyCall.JSON.Array "MediaContainer.Metadata" }} {{ else }} {{ template "errorMsg" (concat "Could not fetch " $mediaServer " API.") }} {{ end }} {{ $usersRequestURL = concat $baseURL "/accounts" }} {{ $usersCall = newRequest $usersRequestURL | withHeader "Accept" "application/json" | withHeader "X-Plex-Token" $apiKey | getResponse }} {{ $users = $usersCall.JSON.Array "MediaContainer.Account" }} {{ else if eq $mediaServer "tautulli" }} {{ $historyRequestURL = concat $baseURL "/api/v2" }} {{ $historyCall = newRequest $historyRequestURL | withParameter "apikey" $apiKey | withParameter "cmd" "get_history" | withParameter "length" $historyLength | withParameter "media_type" $mediaTypes | withHeader "Accept" "application/json" | getResponse }} {{ if eq $historyCall.Response.StatusCode 200 }} {{ $history = $historyCall.JSON.Array "response.data.data" }} {{ else }} {{ template "errorMsg" (concat "Could not fetch " $mediaServer " API.") }} {{ end }} {{ else if or (eq $mediaServer "jellyfin") (eq $mediaServer "emby") }} {{ $usersRequestURL = concat $baseURL "/Users" }} {{ $usersCall = newRequest $usersRequestURL | withParameter "api_key" $apiKey | withHeader "Accept" "application/json" | getResponse }} {{ $usersList := $usersCall.JSON.Array "" }} {{ range $i, $user := $usersList }} {{ if eq ($user.String "Name") $userName }} {{ $userID = $user.String "Id" }} {{ break }} {{ end }} {{ end }} {{ if eq $userID "" }} {{ template "errorMsg" (concat "User '" $userName "' not found.") }} {{ end }} {{ $historyRequestURL = concat $baseURL "/Users/" $userID "/Items" }} {{ $historyCall = newRequest $historyRequestURL | withParameter "api_key" $apiKey | withParameter "Limit" $historyLength | withParameter "IncludeItemTypes" $mediaTypes | withParameter "Recursive" "true" | withParameter "isPlayed" "true" | withParameter "sortBy" "DatePlayed" | withParameter "sortOrder" "Descending" | withParameter "Fields" "UserDataLastPlayedDate" | withHeader "Accept" "application/json" | getResponse }} {{ $history = $historyCall.JSON.Array "Items" }} {{ end }} {{ if and (eq $historyCall.Response.StatusCode 200) (eq (len $history) 0) }}

Nothing has been played. Start streaming something!

{{ else }} {{ end }} {{ end }}