fix jellyfin and add new endpoint
This commit is contained in:
@@ -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() {
|
async function queryJellyfin() {
|
||||||
return await fetch(`${globalConfig.nowplaying.jellyfin.host}/Sessions`, {
|
return await fetch(`${globalConfig.nowplaying.jellyfin.host}/Sessions`, {
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": `MediaBrowser Token=${globalConfig.nowplaying.jellyfin.apiKey}`
|
"Authorization": `MediaBrowser Token=${globalConfig.nowplaying.jellyfin.apiKey}`
|
||||||
}
|
}
|
||||||
}).then(response => response.json()).then(response => {
|
}).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].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
|
if (response[x].NowPlayingItem.MediaType !== "Audio") { break } // If not playing 'audio', skip, this might change in the future
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"json": {
|
"json": {
|
||||||
"songName": response[x].NowPlayingItem.Name,
|
"songName": response[x].NowPlayingItem.Name,
|
||||||
"artistName": response[x].NowPlayingItem.Artists[0],
|
"artistName": response[x].NowPlayingItem.Artists[0],
|
||||||
"albumName": response.recenttracks.track[0].album["#text"],
|
"albumName": response[x].NowPlayingItem.Album ?? `${response[x].NowPlayingItem.Name} (Single)`,
|
||||||
"artUrl": response.recenttracks.track[0].image[3]["#text"],
|
"artUrl": `${globalConfig.nowplaying.jellyfin.hostPublic}/Items/${response[x].NowPlayingItem.Id}/Images/Primary`,
|
||||||
"link": response.recenttracks.track[0].url
|
"link": `https://www.last.fm/music/${response[x].NowPlayingItem.Artists[0].replaceAll(" ","+")}/_/${response[x].NowPlayingItem.Name.replaceAll(" ","+")}`
|
||||||
},
|
},
|
||||||
"html": `<img src="${response.recenttracks.track[0].image[3]["#text"]}" alt="Album Art" style="width: 10em;"> <div class="textlist"> <p>I'm listening to</p> <h3>${response.recenttracks.track[0].name} by ${response.recenttracks.track[0].artist["#text"]}</h3> <p>from ${response.recenttracks.track[0].album["#text"]}</p> <a href="${response.recenttracks.track[0].url}" class="noindent">View on Last.fm</a></div>`
|
"html": `<img src="${globalConfig.nowplaying.jellyfin.hostPublic}/Items/${response[x].NowPlayingItem.Id}/Images/Primary" alt="Album Art" style="width: 10em;"> <div class="textlist"> <p>I'm listening to</p> <h3>${response[x].NowPlayingItem.Name} by ${response[x].NowPlayingItem.Artists[0]}</h3> <p>from ${response[x].NowPlayingItem.Album ?? `${response[x].NowPlayingItem.Name} (Single)`}</p> <a href="https://www.last.fm/music/${response[x].NowPlayingItem.Artists[0].replaceAll(" ","+")}/_/${response[x].NowPlayingItem.Name.replaceAll(" ","+")}" class="noindent">View on Last.fm</a></div>`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return notPlayingAnythingPlaceholder
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export { queryLastfm }
|
export { queryLastfm, queryJellyfin }
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { app, globalConfig } from "../index.js"
|
import { app, globalConfig } from "../index.js"
|
||||||
import { queryLastfm } from "../liberals/libnowplaying.js"
|
import { queryLastfm, queryJellyfin } from "../liberals/libnowplaying.js"
|
||||||
|
|
||||||
var timeSinceLastLastfmQuery = Date.now()-5000
|
var timeSinceLastLastfmQuery = Date.now()-5000
|
||||||
var cachedLastfmResult = {}
|
var cachedLastfmResult = {}
|
||||||
@@ -23,4 +23,12 @@ app.get("/api/nowplaying", (rreq,rres) => {
|
|||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
app.get("/api/nowplayingbeta", (rreq,rres) => {
|
||||||
|
|
||||||
|
queryJellyfin().then(response => {
|
||||||
|
rres.send(response[rreq.query.format] ?? response.json)
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
export {app}
|
export {app}
|
||||||
Reference in New Issue
Block a user