fast72 commited on
Commit
30c8700
·
verified ·
1 Parent(s): d021009

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +40 -35
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('/', async(req, res) => {
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 || req.body
27
- if(!q) return res.json({ status: false, error: '"q" parameter is undefined!'})
28
-
29
  const search = await Spotify.search(q, type, limit)
30
- //if(!Array.isArray(search)) return res.json({ status: false, error: 'Result is not array.'})
31
- res.json(search);
32
- } catch(e) {
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 || req.body
40
- if(!q) return res.json({ status: false, error: '"q" parameter is undefined!'})
41
-
42
  const search = await Spotify.search(q, "track", 1)
43
- //if(!Array.isArray(search)) return res.json({ status: false, error: 'Result is not array.'})
44
- res.json({ status: true, result: Array.isArray(search) ? search[0] : search, download: `https://${req.hostname}/track/${search[0]?.id}` });
45
- } catch(e) {
46
- res.json({ status: false, error: e || e.message })
 
 
47
  }
48
  })
49
 
50
- app.all('/track/:id', async(req, res) => {
51
  try {
52
- const { id } = req.params || res.params
53
- if(!id) return res.json({ status: false, error: "Track ID is not valid." })
54
- const spotify = await Spotify.download(`https://open.spotify.com/track/${id}`)
55
- res.redirect(spotify?.download)
56
- } catch(e) {
57
- res.json({ status: false, error: e || e.message })
 
 
 
 
 
 
 
 
 
 
 
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