forked from narikei/nowplaying-on-slack
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
65 lines (56 loc) · 1.34 KB
/
main.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
const request = require('request');
const { exec } = require('child_process');
const token = process.env.SLACK_API_TOKEN;
const spotifyScript = './bin/get-playing-on-spotify';
const sendToSlack = ({ emoji, message }) => {
request({
url: 'https://slack.com/api/users.profile.set',
method: 'POST',
form: {
token,
profile: JSON.stringify({
status_text: message,
status_emoji: emoji,
}),
},
}, (error, response, body) => {
if (!error) {
return;
}
console.error(error, response, body);
});
};
const watchSpotify = () => {
let before;
setInterval(() => {
exec(spotifyScript, (e, stdout, stderr) => {
if (e) {
console.error(e);
return;
}
if (stderr) {
console.error(stderr);
return;
}
const m = JSON.parse(stdout);
if ('error' in m) {
console.error(m.error);
return;
}
if (before === JSON.stringify(m)) {
// console.log('not change');
return;
}
before = JSON.stringify(m);
const message = `${m.track} - ${m.artist}`;
const emoji = (m.state === 'playing') ? ':spotify:' : ':musical_note:';
sendToSlack({ message, emoji });
console.log(m);
});
}, 3000);
};
if (!token) {
console.log('token ga naiyo');
process.exit();
}
watchSpotify();