-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
119 lines (107 loc) · 3.06 KB
/
app.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
require('dotenv').config();
const express = require('express');
const app = express();
const opn = require('opn');
const axios = require('axios');
const callBackUrl = 'http://localhost:3001/callback';
const clientID = process.env.CLIENT_ID;
const clientSecret = process.env.CLIENT_SECRET;
const slackToken = process.env.SLACK_TOKEN;
let userToken = '';
let spotifyAccessToken = '';
let timeRemaining = 1000;
let currentSong = '';
const getUserToken = (req) => (userToken = req.originalUrl.split('=')[1]);
process.on('SIGINT', async () => {
await setSlackStatus({
status_text: '',
status_emoji: '',
});
process.exit(1);
});
const handleFatalError = (err) => {
console.error(err.message);
console.error(err.stack);
process.exit(1);
};
const getSpotifyAuth = (userToken) => {
axios({
method: 'post',
url: 'https://accounts.spotify.com/api/token',
params: {
grant_type: 'authorization_code',
code: userToken,
redirect_uri: callBackUrl,
},
headers: {
Authorization:
'Basic ' +
Buffer.from(clientID + ':' + clientSecret).toString('base64'),
},
})
.then(function (response) {
spotifyAccessToken = response.data.access_token;
timerCheck();
})
.catch(handleFatalError);
};
const getCurrentlyPlaying = () => {
axios
.get('https://api.spotify.com/v1/me/player/currently-playing', {
headers: {
Authorization: `Bearer ${spotifyAccessToken}`,
},
})
.then(function ({ data }) {
if (!data.progress_ms) {
return handleFatalError({ message: 'No song playing!', stack: '' });
}
timeRemaining = data.item.duration_ms - data.progress_ms + 1000;
const minutes = Math.floor(timeRemaining / 60000);
const seconds = ((timeRemaining % 60000) / 1000).toFixed(0);
const newSong = data.item.artists[0].name + ' - ' + data.item.name;
if (newSong !== currentSong) {
currentSong = data.item.artists[0].name + ' - ' + data.item.name;
setSlackStatus({
status_text: `${currentSong}`,
status_emoji: ':spotify:',
});
console.log(
'Time remaining:',
`${minutes}:${seconds < 10 ? '0' : ''}${seconds}`,
'Song is: ',
currentSong
);
}
})
.catch(handleFatalError);
};
const timerCheck = () => {
getCurrentlyPlaying();
setTimeout(timerCheck, timeRemaining);
};
const setSlackStatus = (profile) =>
axios
.post(
'https://slack.com/api/users.profile.set',
{
profile,
},
{
headers: {
Authorization: `Bearer ${slackToken}`,
},
}
)
.catch((error) => console.error(error));
opn(
`https://accounts.spotify.com/authorize?client_id=${clientID}&response_type=code&redirect_uri=${callBackUrl}&scope=user-read-currently-playing%20user-read-playback-state`
);
app.get('/callback', function (req, res) {
res.send('You can now close this window 👋!');
getUserToken(req);
getSpotifyAuth(userToken);
});
app.listen(3001, function () {
console.log('server started on port 3001');
});