env file handling + fix error in libnowplaying
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
bun.lockb
|
bun.lockb
|
||||||
todo.txt
|
todo.txt
|
||||||
proto.js
|
proto.js
|
||||||
|
.env
|
||||||
16
index.js
16
index.js
@@ -7,20 +7,24 @@ import cookieParser from 'cookie-parser'
|
|||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
if (!process.env.DATABASE_URL) {
|
if (!process.env.DATABASE_URL) {
|
||||||
console.log("FATAL: DATABASE_URI must be set")
|
try {
|
||||||
process.exit(1)
|
process.loadEnvFile("./.env")
|
||||||
|
} catch {
|
||||||
|
console.log("FATAL: DATABASE_URL was not defined or found in env file")
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const db = postgres(process.env.DATABASE_URL)
|
const db = postgres(process.env.DATABASE_URL)
|
||||||
|
|
||||||
const globalConfig = await db`select content from config where id = ${process.env.CONFIG_OVERRIDE ?? 'production'}`.then(response => {return response[0]["content"]}).catch(error => {
|
const globalConfig = await db`select content from config where id = ${process.env.CONFIG_OVERRIDE ?? 'production'}`.then(response => { return response[0]["content"] }).catch(error => {
|
||||||
console.log(`FATAL: Error occured in downloading configuration: ${error}`)
|
console.log(`FATAL: Error occured in downloading configuration: ${error}`)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
const globalVersion = execSync(`git show --oneline -s`).toString().split(" ")[0]
|
const globalVersion = execSync(`git show --oneline -s`).toString().split(" ")[0]
|
||||||
// Returns ISO 8601 Date & 24hr time for UTC-7/PDT
|
// Returns ISO 8601 Date & 24hr time for UTC-7/PDT
|
||||||
const startTime = new Date(new Date().getTime() - 25200000).toISOString().slice(0,19).replace('T',' ')
|
const startTime = new Date(new Date().getTime() - 25200000).toISOString().slice(0, 19).replace('T', ' ')
|
||||||
|
|
||||||
export { app, fs, db, globalConfig, globalVersion }
|
export { app, fs, db, globalConfig, globalVersion }
|
||||||
|
|
||||||
@@ -28,7 +32,7 @@ app.use(json()) // Allows receiving JSON bodies
|
|||||||
// see important note: https://expressjs.com/en/api.html#express.json
|
// see important note: https://expressjs.com/en/api.html#express.json
|
||||||
app.use(cookieParser()) // Allows receiving cookies
|
app.use(cookieParser()) // Allows receiving cookies
|
||||||
|
|
||||||
process.on('SIGTERM', function() {
|
process.on('SIGTERM', function () {
|
||||||
console.log("Received SIGTERM, exiting...")
|
console.log("Received SIGTERM, exiting...")
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
})
|
})
|
||||||
@@ -41,7 +45,7 @@ fs.readdir("./routes", (err, files) => {
|
|||||||
let importedRoutes = []
|
let importedRoutes = []
|
||||||
files.forEach(file => {
|
files.forEach(file => {
|
||||||
import(`./routes/${file}`)
|
import(`./routes/${file}`)
|
||||||
importedRoutes.push(file.slice(0,-3))
|
importedRoutes.push(file.slice(0, -3))
|
||||||
})
|
})
|
||||||
process.stdout.write(` | Imported ${importedRoutes} \n`)
|
process.stdout.write(` | Imported ${importedRoutes} \n`)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ async function queryLastfm() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).catch(fetchError => {
|
}).catch(fetchError => {
|
||||||
console.log("libnowplaying.js: Fetch failed! "+fetchError)
|
console.log("libnowplaying.js: Fetch failed! " + fetchError)
|
||||||
return {}
|
return {}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -42,30 +42,34 @@ async function queryLastfm() {
|
|||||||
* @returns {object} Object containing response in JSON and HTML (as string)
|
* @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`, {
|
try {
|
||||||
headers: {
|
return await fetch(`${globalConfig.nowplaying.jellyfin.host}/Sessions`, {
|
||||||
"Authorization": `MediaBrowser Token=${globalConfig.nowplaying.jellyfin.apiKey}`
|
headers: {
|
||||||
}
|
"Authorization": `MediaBrowser Token=${globalConfig.nowplaying.jellyfin.apiKey}`
|
||||||
}).then(response => response.json()).then(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 == 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[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": `<img src="${globalConfig.nowplaying.jellyfin.hostPublic}/Items/${response[x].NowPlayingItem.Id}/Images/Primary" alt="Album Art"> <div> <span class="nowPlayingLine1">I'm listening to</span> <span class="nowPlayingLine2">${response[x].NowPlayingItem.Name} by ${response[x].NowPlayingItem.Artists[0]}</span> <span class="nowPlayingLine3">from ${response[x].NowPlayingItem.Album ?? `${response[x].NowPlayingItem.Name} (Single)`}</span> <a class="nowPlayingLine4" href="https://www.last.fm/music/${response[x].NowPlayingItem.Artists[0].replaceAll(" ","+")}/_/${response[x].NowPlayingItem.Name.replaceAll(" ","+")}">View on Last.fm</a></div>`
|
|
||||||
}
|
}
|
||||||
}
|
}).then(response => response.json()).then(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 == 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[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": `<img src="${globalConfig.nowplaying.jellyfin.hostPublic}/Items/${response[x].NowPlayingItem.Id}/Images/Primary" alt="Album Art"> <div> <span class="nowPlayingLine1">I'm listening to</span> <span class="nowPlayingLine2">${response[x].NowPlayingItem.Name} by ${response[x].NowPlayingItem.Artists[0]}</span> <span class="nowPlayingLine3">from ${response[x].NowPlayingItem.Album ?? `${response[x].NowPlayingItem.Name} (Single)`}</span> <a class="nowPlayingLine4" href="https://www.last.fm/music/${response[x].NowPlayingItem.Artists[0].replaceAll(" ", "+")}/_/${response[x].NowPlayingItem.Name.replaceAll(" ", "+")}">View on Last.fm</a></div>`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return notPlayingAnythingPlaceholder
|
||||||
|
})
|
||||||
|
} catch {
|
||||||
return notPlayingAnythingPlaceholder
|
return notPlayingAnythingPlaceholder
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { queryLastfm, queryJellyfin }
|
export { queryLastfm, queryJellyfin }
|
||||||
Reference in New Issue
Block a user