const express = require('express'); const axios = require('axios'); const Spotify = require('./lib/spotify.js'); const app = express() app.set('json spaces', 2) const PORT = 7860 const stringify = (value) => { return JSON.stringify(value, null, 2) } let totalRequest = 0 app.use((req, res, next) => { totalRequest++; next(); }); app.all('/', async(req, res) => { res.json({ uptime: new Date(process.uptime() * 1000).toUTCString().split(' ')[4], totalRequest }) }) app.all('/search', async(req, res) => { try { const { q, type = "track", limit = 10 } = req.query || req.body if(!q) return res.json({ status: false, error: '"q" parameter is undefined!'}) const search = await Spotify.search(q, type, limit) //if(!Array.isArray(search)) return res.json({ status: false, error: 'Result is not array.'}) res.json(search); } catch(e) { res.json({ status: false, error: e || e.message }) } }) app.all('/play', async(req, res) => { try { const { q } = req.query || req.body if(!q) return res.json({ status: false, error: '"q" parameter is undefined!'}) const search = await Spotify.search(q, "track", 1) //if(!Array.isArray(search)) return res.json({ status: false, error: 'Result is not array.'}) res.json({ status: true, result: Array.isArray(search) ? search[0] : search, download: `https://${req.hostname}/track/${search[0]?.id}` }); } catch(e) { res.json({ status: false, error: e || e.message }) } }) app.all('/track/:id', async(req, res) => { try { const { id } = req.params || res.params if(!id) return res.json({ status: false, error: "Track ID is not valid." }) const spotify = await Spotify.download(`https://open.spotify.com/track/${id}`) res.redirect(spotify?.download) } catch(e) { res.json({ status: false, error: e || e.message }) } }) app.listen(PORT)