From 69a06bc1c353915fe78f9be857b6d5946f9a3f42 Mon Sep 17 00:00:00 2001
From: Enstrayed <48845980+Enstrayed@users.noreply.github.com>
Date: Wed, 16 Oct 2024 22:33:56 -0700
Subject: [PATCH] fix jellyfin and add new endpoint
---
liberals/libnowplaying.js | 20 +++++++++++++-------
routes/nowplaying.js | 10 +++++++++-
2 files changed, 22 insertions(+), 8 deletions(-)
diff --git a/liberals/libnowplaying.js b/liberals/libnowplaying.js
index 5ef7e92..04c7ce5 100644
--- a/liberals/libnowplaying.js
+++ b/liberals/libnowplaying.js
@@ -37,29 +37,35 @@ async function queryLastfm() {
})
}
+/**
+ * Queries Jellyfin for user set in config file and returns formatted result
+ * @returns {object} Object containing response in JSON and HTML (as string)
+ */
async function queryJellyfin() {
return await fetch(`${globalConfig.nowplaying.jellyfin.host}/Sessions`, {
headers: {
"Authorization": `MediaBrowser Token=${globalConfig.nowplaying.jellyfin.apiKey}`
}
}).then(response => response.json()).then(response => {
- for (x in response) {
+ for (let x in response) {
if (response[x].UserName !== globalConfig.nowplaying.jellyfin.target) { break } // If session does not belong to target specified in config, skip
- if (!response[x].NowPlayingItem) { break } // If the NowPlayingItem object is not present, skip (session is not playing anything)
+ if (response[x].NowPlayingItem == undefined) { break } // If the NowPlayingItem object is not present, skip (session is not playing anything)
if (response[x].NowPlayingItem.MediaType !== "Audio") { break } // If not playing 'audio', skip, this might change in the future
return {
"json": {
"songName": response[x].NowPlayingItem.Name,
"artistName": response[x].NowPlayingItem.Artists[0],
- "albumName": response.recenttracks.track[0].album["#text"],
- "artUrl": response.recenttracks.track[0].image[3]["#text"],
- "link": response.recenttracks.track[0].url
+ "albumName": response[x].NowPlayingItem.Album ?? `${response[x].NowPlayingItem.Name} (Single)`,
+ "artUrl": `${globalConfig.nowplaying.jellyfin.hostPublic}/Items/${response[x].NowPlayingItem.Id}/Images/Primary`,
+ "link": `https://www.last.fm/music/${response[x].NowPlayingItem.Artists[0].replaceAll(" ","+")}/_/${response[x].NowPlayingItem.Name.replaceAll(" ","+")}`
},
- "html": `
I'm listening to
from ${response.recenttracks.track[0].album["#text"]}
View on Last.fmI'm listening to
from ${response[x].NowPlayingItem.Album ?? `${response[x].NowPlayingItem.Name} (Single)`}
View on Last.fm