Update server.js
Browse files
server.js
CHANGED
|
@@ -1,60 +1,65 @@
|
|
| 1 |
-
const express = require('express')
|
| 2 |
-
const axios = require('axios')
|
| 3 |
-
const Spotify = require('./lib/spotify.js')
|
| 4 |
|
| 5 |
const app = express()
|
| 6 |
app.set('json spaces', 2)
|
| 7 |
const PORT = 7860
|
| 8 |
-
|
| 9 |
-
const stringify = (value) => {
|
| 10 |
-
return JSON.stringify(value, null, 2)
|
| 11 |
-
}
|
| 12 |
-
|
| 13 |
let totalRequest = 0
|
| 14 |
|
| 15 |
app.use((req, res, next) => {
|
| 16 |
-
totalRequest
|
| 17 |
-
next()
|
| 18 |
-
})
|
| 19 |
|
| 20 |
-
app.all('/',
|
| 21 |
res.json({ uptime: new Date(process.uptime() * 1000).toUTCString().split(' ')[4], totalRequest })
|
| 22 |
})
|
| 23 |
|
| 24 |
-
app.all('/search', async(req, res) => {
|
| 25 |
try {
|
| 26 |
-
const { q, type = "track", limit = 10 } = req.query
|
| 27 |
-
if(!q) return res.json({ status: false, error: '"q" parameter is undefined!'})
|
| 28 |
-
|
| 29 |
const search = await Spotify.search(q, type, limit)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
res.json({ status: false, error: e || e.message })
|
| 34 |
}
|
| 35 |
})
|
| 36 |
|
| 37 |
-
app.all('/play', async(req, res) => {
|
| 38 |
try {
|
| 39 |
-
const { q } = req.query
|
| 40 |
-
if(!q) return res.json({ status: false, error: '"q" parameter is undefined!'})
|
| 41 |
-
|
| 42 |
const search = await Spotify.search(q, "track", 1)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
res.json({ status:
|
|
|
|
|
|
|
| 47 |
}
|
| 48 |
})
|
| 49 |
|
| 50 |
-
app.all('/track/:id', async(req, res) => {
|
| 51 |
try {
|
| 52 |
-
const { id } = req.params
|
| 53 |
-
if(!id) return res.json({ status: false, error:
|
| 54 |
-
const
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
}
|
| 59 |
})
|
| 60 |
|
|
|
|
| 1 |
+
const express = require('express')
|
| 2 |
+
const axios = require('axios')
|
| 3 |
+
const Spotify = require('./lib/spotify.js')
|
| 4 |
|
| 5 |
const app = express()
|
| 6 |
app.set('json spaces', 2)
|
| 7 |
const PORT = 7860
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
let totalRequest = 0
|
| 9 |
|
| 10 |
app.use((req, res, next) => {
|
| 11 |
+
totalRequest++
|
| 12 |
+
next()
|
| 13 |
+
})
|
| 14 |
|
| 15 |
+
app.all('/', (req, res) => {
|
| 16 |
res.json({ uptime: new Date(process.uptime() * 1000).toUTCString().split(' ')[4], totalRequest })
|
| 17 |
})
|
| 18 |
|
| 19 |
+
app.all('/search', async (req, res) => {
|
| 20 |
try {
|
| 21 |
+
const { q, type = "track", limit = 10 } = req.query
|
| 22 |
+
if (!q) return res.json({ status: false, error: '"q" parameter is undefined!' })
|
|
|
|
| 23 |
const search = await Spotify.search(q, type, limit)
|
| 24 |
+
res.json(search)
|
| 25 |
+
} catch (e) {
|
| 26 |
+
res.json({ status: false, error: e?.message || e })
|
|
|
|
| 27 |
}
|
| 28 |
})
|
| 29 |
|
| 30 |
+
app.all('/play', async (req, res) => {
|
| 31 |
try {
|
| 32 |
+
const { q } = req.query
|
| 33 |
+
if (!q) return res.json({ status: false, error: '"q" parameter is undefined!' })
|
|
|
|
| 34 |
const search = await Spotify.search(q, "track", 1)
|
| 35 |
+
const track = Array.isArray(search) ? search[0] : search
|
| 36 |
+
const id = track?.id
|
| 37 |
+
if (!id) return res.json({ status: false, error: 'No valid track found.' })
|
| 38 |
+
res.json({ status: true, result: track, download: `https://${req.hostname}/track/${id}` })
|
| 39 |
+
} catch (e) {
|
| 40 |
+
res.json({ status: false, error: e?.message || e })
|
| 41 |
}
|
| 42 |
})
|
| 43 |
|
| 44 |
+
app.all('/track/:id', async (req, res) => {
|
| 45 |
try {
|
| 46 |
+
const { id } = req.params
|
| 47 |
+
if (!id) return res.json({ status: false, error: 'Track ID is not valid.' })
|
| 48 |
+
const track = await Spotify.download(`https://open.spotify.com/track/${id}`)
|
| 49 |
+
const url = track?.download
|
| 50 |
+
if (!url) return res.json({ status: false, error: 'Failed to get download URL.' })
|
| 51 |
+
|
| 52 |
+
const response = await axios({
|
| 53 |
+
url,
|
| 54 |
+
method: 'GET',
|
| 55 |
+
responseType: 'stream'
|
| 56 |
+
})
|
| 57 |
+
|
| 58 |
+
res.setHeader('Content-Disposition', `attachment; filename="${id}.mp3"`)
|
| 59 |
+
res.setHeader('Content-Type', 'audio/mpeg')
|
| 60 |
+
response.data.pipe(res)
|
| 61 |
+
} catch (e) {
|
| 62 |
+
res.json({ status: false, error: e?.message || e })
|
| 63 |
}
|
| 64 |
})
|
| 65 |
|