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 => { try { return response.json() } catch (error) { return {} } }) } 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 => { 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() let i = 1 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 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": tags, "type": "file", }) } else if (item.type == "URL") { sounds.push({ "id": item.id, "name": item.title, "type": "url", "tags": tags, }) } }) 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 = `