diff options
-rw-r--r-- | index.js | 60 |
1 files changed, 55 insertions, 5 deletions
@@ -28,7 +28,13 @@ function fetch_library(page, tags=""){ "headers": headers(), "body": `type=file,url&dir=.&tags=${tags}&keywords=&page=${page}&action=query`, "method": "POST", - }).then(response => response.json()) + }).then(response => { + try { + return response.json() + } catch (error) { + return {} + } + }) } async function fetch_total_pages(){ @@ -45,10 +51,30 @@ function enqueue(id){ "headers": headers(), "body": `add_item_next=${id}`, "method": "POST", - }).then(response => response.json()) + }).then(response => { + try { + return response.json() + } catch (error) { + return {} + } + }) .then(data => console.log(`Added ${id} to queue`)); } +function playlist(){ + return fetch(`${config.baseURL}/playlist`, { + "credentials": "include", + "headers": headers(), + "method": "GET", + }).then(response => { + try { + return response.json() + } catch (error) { + return {} + } + }) +} + async function get_all_sounds(){ let sounds = [] total_pages = await fetch_total_pages() @@ -56,6 +82,7 @@ async function get_all_sounds(){ while(i <= total_pages){ let items = await fetch_page(i) items.forEach(item => { + let tags = item.tags.map( i => {return i[0]} ) if(item.type =="File"){ let parts = item.path.split("-", 2) let name = item.path @@ -72,7 +99,7 @@ async function get_all_sounds(){ "id": item.id, "name": name, "path": item.path, - "tags": item.tags[0], + "tags": tags, "type": "file", }) } else if (item.type == "URL") { @@ -80,7 +107,7 @@ async function get_all_sounds(){ "id": item.id, "name": item.title, "type": "url", - "tags": item.tags[0], + "tags": tags, }) } }) @@ -153,7 +180,26 @@ server.get('/', async (req, res) => { function random(){ fetch("/random") } + function playlist(){ + var intervalId = setInterval(function() { + fetch("/playlist").then(res => res.json()).then(res => { + let el = document.getElementById("playlist") + el.innerHTML = '' + res.items.forEach(song => { + var li = document.createElement("li"); + let text = song.title + " (" + (song.duration/60).toFixed(2) + " minutes)" + li.appendChild(document.createTextNode(text)) + el.appendChild(li) + }) + }) + }, 5000); + } + window.addEventListener('load', event => playlist()); </script> + <div> + <h1>Now playling</h1> + <ol id="playlist"><ol> + </div> <button onclick="random()">random</button> <body> ` @@ -200,11 +246,15 @@ server.get('/random', async (req, res, next) => { res.send("ok") }) +server.get('/playlist', async (req, res, next) => { + let playlist_items = await playlist() + res.send(playlist_items) +}) + server.get('/:id', async (req, res, next) => { enqueue(req.params.id) res.send("ok") }) - console.log("listening...") server.listen(config.port) |