Update server.js
Browse files
server.js
CHANGED
|
@@ -45,9 +45,15 @@ 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 |
-
|
| 49 |
-
const
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
const response = await axios({
|
| 53 |
url,
|
|
@@ -55,7 +61,7 @@ app.all('/track/:id', async (req, res) => {
|
|
| 55 |
responseType: 'stream'
|
| 56 |
})
|
| 57 |
|
| 58 |
-
res.setHeader('Content-Disposition', `attachment; filename="${
|
| 59 |
res.setHeader('Content-Type', 'audio/mpeg')
|
| 60 |
response.data.pipe(res)
|
| 61 |
} catch (e) {
|
|
|
|
| 45 |
try {
|
| 46 |
const { id } = req.params
|
| 47 |
if (!id) return res.json({ status: false, error: 'Track ID is not valid.' })
|
| 48 |
+
|
| 49 |
+
const search = await Spotify.search(id, 'track', 10)
|
| 50 |
+
const track = Array.isArray(search) ? search.find(v => v.id === id) : null
|
| 51 |
+
if (!track) return res.json({ status: false, error: 'not found' })
|
| 52 |
+
|
| 53 |
+
const title = track.title?.replace(/[\\/:*?"<>|]/g, '') || 'spotify_track'
|
| 54 |
+
const dl = await Spotify.download(`https://open.spotify.com/track/${id}`)
|
| 55 |
+
const url = dl?.download
|
| 56 |
+
if (!url) return res.json({ status: false, error: 'cant find download url!' })
|
| 57 |
|
| 58 |
const response = await axios({
|
| 59 |
url,
|
|
|
|
| 61 |
responseType: 'stream'
|
| 62 |
})
|
| 63 |
|
| 64 |
+
res.setHeader('Content-Disposition', `attachment; filename="${title}.mp3"`)
|
| 65 |
res.setHeader('Content-Type', 'audio/mpeg')
|
| 66 |
response.data.pipe(res)
|
| 67 |
} catch (e) {
|