summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Powers <mark@marks.kitchen>2022-09-22 22:08:42 -0500
committerMark Powers <mark@marks.kitchen>2022-09-22 22:08:42 -0500
commit1e04f311e6f84c19af2cf8795fc892d8f0242f5d (patch)
tree066394d627946fc650b0f5ddccb60da4f399ab81
parent94a45e8f810aa327f9f2e094c553667ef391a043 (diff)
Add now playing
-rw-r--r--index.js60
1 files changed, 55 insertions, 5 deletions
diff --git a/index.js b/index.js
index f48f9ca..a3bd2d8 100644
--- a/index.js
+++ b/index.js
@@ -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)