summaryrefslogtreecommitdiff
path: root/index.js
diff options
context:
space:
mode:
authorMark Powers <mark@marks.kitchen>2022-09-18 12:59:16 -0500
committerMark Powers <mark@marks.kitchen>2022-09-18 12:59:16 -0500
commit94a45e8f810aa327f9f2e094c553667ef391a043 (patch)
tree39b17e430bef40d278de5e51b90ebdd3d5a95ba2 /index.js
Initial commit
Diffstat (limited to 'index.js')
-rw-r--r--index.js210
1 files changed, 210 insertions, 0 deletions
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..f48f9ca
--- /dev/null
+++ b/index.js
@@ -0,0 +1,210 @@
+const express = require('express')
+const fetch = require('node-fetch')
+
+const config = require('./config.js')
+
+const server = express();
+
+server.use((req, res, next) => {
+ console.log(req.method, req.path)
+ next()
+})
+
+const credentials = Buffer.from(`${config.user}:${config.pass}`).toString('base64')
+const authorization = `Basic ${credentials}`
+
+var cached_sounds = []
+
+function headers(){
+ return {
+ "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
+ "Authorization": authorization,
+ }
+}
+
+function fetch_library(page, tags=""){
+ return fetch(`${config.baseURL}/library`, {
+ "credentials": "include",
+ "headers": headers(),
+ "body": `type=file,url&dir=.&tags=${tags}&keywords=&page=${page}&action=query`,
+ "method": "POST",
+ }).then(response => response.json())
+}
+
+async function fetch_total_pages(){
+ return fetch_library(1).then(data => data.total_pages)
+}
+
+async function fetch_page(page){
+ return fetch_library(page).then(data => data.items)
+}
+
+function enqueue(id){
+ fetch(`${config.baseURL}/post`, {
+ "credentials": "include",
+ "headers": headers(),
+ "body": `add_item_next=${id}`,
+ "method": "POST",
+ }).then(response => response.json())
+ .then(data => console.log(`Added ${id} to queue`));
+}
+
+async function get_all_sounds(){
+ let sounds = []
+ total_pages = await fetch_total_pages()
+ let i = 1
+ while(i <= total_pages){
+ let items = await fetch_page(i)
+ items.forEach(item => {
+ if(item.type =="File"){
+ let parts = item.path.split("-", 2)
+ let name = item.path
+ if(parts.length > 1){
+ name = parts[1]
+ }
+ let parts2 = name.split("/", 2)
+ if(parts2.length > 1){
+ name = parts2[1]
+ }
+ // NOTE: tags is index here to unpack the list it comes in...
+ // [ [tag1, tag2] ]
+ sounds.push({
+ "id": item.id,
+ "name": name,
+ "path": item.path,
+ "tags": item.tags[0],
+ "type": "file",
+ })
+ } else if (item.type == "URL") {
+ sounds.push({
+ "id": item.id,
+ "name": item.title,
+ "type": "url",
+ "tags": item.tags[0],
+ })
+ }
+ })
+ i++
+ }
+ cached_sounds = sounds
+ return sounds
+}
+
+async function organize_sounds(){
+ let sounds = {}
+ config.names.forEach(name => {
+ sounds[name] = []
+ })
+ let other = []
+ let tags = {}
+ all_sounds = await get_all_sounds()
+ all_sounds.forEach(item => {
+ if (item.tags){
+ item.tags.forEach(tag => {
+ if(!tags.hasOwnProperty(tag)){
+ tags[tag] = []
+ }
+ tags[tag].push(item)
+ })
+ }
+ let matched = false
+ Object.keys(sounds).forEach(name => {
+ if(item.type == "file"){
+ path = item.path.split("-")[0]
+ if(path.toLowerCase().indexOf(name) != -1){
+ sounds[name].push(item)
+ matched = true
+ }
+ }
+ })
+ if(!matched){
+ other.push(item)
+ }
+ })
+ sounds["other"] = other
+ return {"sounds": sounds, "tags": tags}
+}
+
+server.get('/', async (req, res) => {
+ var html = `
+ <style>
+ div.btn {
+ cursor: pointer;
+ background-color: #ddd;
+ border: 1px solid black;
+ padding: 0.25em;
+ margin: 0.25em;
+ }
+ div.tags {
+ margin-left: 2em;
+ font-style: italic;
+ }
+ div.container {
+ display: flex;
+ flex-wrap: wrap;
+ }
+ </style>
+ <script>
+ function enqueue(ids){
+ ids.forEach(id => {
+ fetch("/"+id)
+ })
+ }
+ function random(){
+ fetch("/random")
+ }
+ </script>
+ <button onclick="random()">random</button>
+ <body>
+ `
+ try {
+ let soundObj = await organize_sounds()
+ let sounds = soundObj["sounds"]
+ let tags = soundObj["tags"]
+ Object.keys(sounds).forEach(title => {
+ html += `<h1>${title.toUpperCase()}</h1><div class="container">`
+ sounds[title].forEach(item => {
+ let tags = ""
+ if(item.tags){
+ tags = `<div class="tags">${item.tags.join(", ")}</div>`
+ }
+ try {
+ html += `<div class="btn" onclick="enqueue(['${item.id}'])"><div>${item.name}</div>${tags}</div>\n`
+ } catch (error) {
+ console.log(error)
+ }
+ })
+ html += `</div>`
+ })
+ html += `<h1>Tags</h1><div class="container">`
+ Object.keys(tags).forEach(tagName => {
+ let idsForTag = tags[tagName].map( item => {return item.id})
+ let idsString = idsForTag.join("', '")
+ html += `<div class="btn" onclick="enqueue(['${idsString}'])">Add all '${tagName}'</div>\n`
+ })
+ html += `</div>`
+ html += "</body>"
+ res.send(html);
+ } catch (error){
+ console.log(error)
+ }
+})
+
+server.get('/random', async (req, res, next) => {
+ let all_sounds = cached_sounds
+ let filtered_sounds = all_sounds.filter(item => {
+ return item.type == "file" && !item.path.startsWith("uploads/")
+ })
+ let rand_sound = filtered_sounds[Math.floor(Math.random() * filtered_sounds.length)]
+ enqueue(rand_sound.id)
+ res.send("ok")
+})
+
+server.get('/:id', async (req, res, next) => {
+ enqueue(req.params.id)
+ res.send("ok")
+})
+
+
+console.log("listening...")
+server.listen(config.port)