-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
99 lines (75 loc) · 2.56 KB
/
utils.js
File metadata and controls
99 lines (75 loc) · 2.56 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require('dotenv').config();
const axios = require('axios').default;
const moment = require('moment');
const qs = require('qs');
const { createClient } = require('redis');
const utils = {
token: {
access_token: null,
token_type: 'bearer',
expires_in: 0,
expiration_date: moment().subtract(1, 'day').utc()
},
getAuthToken: async function () {
if (moment(this.expiration_date).isBefore(moment())) {
console.log('token pas expiré ?');
return this.token;
}
const credential = Buffer.from(process.env.SPOTIFY_CLIENT_ID + ':' + process.env.SPOTIFY_CLIENT_SECRET).toString('base64');
try {
const options = {
method: 'POST',
url: 'https://accounts.spotify.com/api/token',
headers: {
Accept: 'application/json',
Authorization: `Basic ${credential}`
},
data: qs.stringify({
grant_type: 'refresh_token',
refresh_token: process.env.SPOTIFY_REFRESH_TOKEN
})
};
const response = await axios.request(options);
this.token = response.data;
this.token.expiration_date = moment().add(this.token.expires_in, 'seconds');
return this.token;
} catch (e) {
console.error(e);
return false;
}
},
async getPlayback() {
const token = await this.getAuthToken();
const response = await axios.get('https://api.spotify.com/v1/me/player', {
headers: {
'Authorization': `Bearer ${token.access_token}`,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
if (response.status === 204) {
return false;
}
return response.data;
},
async getRedisClient() {
const client = createClient();
client.on('error', console.error);
await client.connect();
return client;
},
async getFirstInQueue() {
const client = await utils.getRedisClient();
const first = await client.lIndex('queue', 0);
return JSON.parse(first);
},
async popQueue() {
const client = await utils.getRedisClient();
await client.blPop('queue', 0);
},
async addToQueue(item) {
const client = await utils.getRedisClient();
await client.rPush('queue', JSON.stringify(item));
}
};
module.exports = utils;