File size: 1,869 Bytes
106f209
 
55e8541
106f209
 
a7b1cd8
106f209
 
 
3c72f08
106f209
 
8ca8853
d7363dd
 
8ca8853
43db2b4
d7363dd
 
 
43db2b4
d7363dd
 
f1b5255
 
 
 
 
 
 
 
 
 
 
 
 
106f209
c4b7c29
 
 
55e8541
c4b7c29
 
 
 
 
 
5f44f17
 
 
c4b7c29
 
998e9fa
c4b7c29
 
 
 
 
019cb1f
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)