-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.js
executable file
·91 lines (79 loc) · 2.01 KB
/
run.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
#!/usr/bin/env node
var LastfmAPI = require('lastfmapi'),
request = require('request-promise-native');
require('dotenv').config({
path: `${__dirname}/.env`,
});
let lastfm = new LastfmAPI({
api_key: process.env.LASTFM_KEY,
secret: process.env.LASTFM_SECRET,
}),
username = process.argv[2],
currentTracks = [];
if (!username) {
console.log('No username specified');
process.exit();
}
run();
let getRecentInterval = setInterval(run, 10000);
function run() {
lastfm.user.getRecentTracks({
// limit: 1,
user: username,
}, (err, data) => {
if (err) {
console.log(err);
return;
}
let track = data.track[0],
info = `${track.artist['#text']} - ${track.name}`;
if (currentTracks.includes(info)) {
return;
}
currentTracks.unshift(info);
if (currentTracks.length > 2) {
currentTracks.pop();
}
currentTrack = info;
Promise.all(process.env.SLACK_TOKEN.split(',').map(token => {
return request.post('https://slack.com/api/users.profile.set', {
form: {
token: token,
profile: JSON.stringify({
"status_text": info,
"status_emoji": ':musical_note:',
})
}
});
}))
.then(() => {
console.log(`Now playing: ${info}`);
});
});
}
function exitHandler(options, err) {
if (err) {
console.log(err);
}
console.log('Exiting...');
Promise.all(process.env.SLACK_TOKEN.split(',').map(token => {
return request.post('https://slack.com/api/users.profile.set', {
form: {
token: process.env.SLACK_TOKEN,
profile: JSON.stringify({
"status_text": ``,
"status_emoji": '',
})
}
});
}))
.then(() => {
process.exit();
});
}
//do something when app is closing
process.on('exit', exitHandler.bind(null,{cleanup:true}));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));