-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
executable file
·143 lines (118 loc) · 3.84 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env node
var fs = require('fs'),
path = require('path'),
log = require('verbalize'),
colors = require('colors'),
nconf = require('nconf'),
mkdirp = require('mkdirp'),
nowplaying = require('nowplaying'),
request = require('request');
var itunes = require('./lib/itunes'),
spotify = require('./lib/spotify'),
apps = [itunes, spotify],
defaultConfigFilePath;
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
// SETUP
// Fetch default configuration path from process environment
nconf.env(['HOME', 'NODE_ENV']);
defaultConfigFilePath = path.normalize(path.join(nconf.get('HOME'), '.config', 'slack-audio', 'config.json'));
mkdirp.sync(path.dirname(defaultConfigFilePath));
// Add conf file as 1st hierarchy for nconf
nconf.use('file', { file: defaultConfigFilePath });
console.log(('Using slack-audio configuration: ' + defaultConfigFilePath).yellow);
// Add argv as 2nd hierarchy for nconf
nconf.argv({
'user': {
alias: 'u',
describe: 'User to report as poster in Slack channel.',
default: (nconf.get('slack:user') || 'Unknown Maxrelaxer')
},
'channel': {
alias: 'c',
describe: 'Slack channel to post song playing changes to.',
default: (nconf.get('slack:channel') || '#relaxbot')
},
'bot': {
alias: 'b',
describe: 'Name of the bot that posts to Slack channel.',
default: (nconf.get('slack:bot') || 'Jams Bot')
},
'url': {
alias: 'w',
describe: 'Webhooks URL found on the Slack custom integrations page.',
demand: (!nconf.get('slack:url')),
default: (nconf.get('slack:url') || null)
},
'save': {
alias: 's',
describe: 'Bool on whether to save argv options to config file (default: false)',
default: false
}
});
// Allow argv to override existing prefs
nconf.set('slack:user', nconf.get('user'));
nconf.set('slack:channel', nconf.get('channel'));
nconf.set('slack:bot', nconf.get('bot'));
if (nconf.get('url') !== null) nconf.set('slack:url', nconf.get('url'));
// Save only if -s flag passed
if (nconf.get('save')) saveConfig();
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
// App
var currentTrack,
message,
payload;
payload = {
'username': 'MusicBot',
'text': null
};
// Listen to nowplaying events
nowplaying.on("playing", onPlaying);
nowplaying.on("paused", onPaused);
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
function getFormattedMessage(data) {
for (var i in apps) {
if (apps[i].name === data.source) {
return apps[i].getMessage(nconf.get('slack:user'), data);
}
}
return null;
}
function onPlaying(data) {
message = getFormattedMessage(data);
console.log("PLAYING!".green);
console.log("[message to post]".green);
console.log(message.yellow);
payload['text'] = message;
if (message && message != currentTrack) {
request.post({
url: nconf.get('slack:url'),
json: true,
body: payload
}, function(err, resp, body) {
if (err) console.error(err);
if (resp && body) {
console.log(("Slack Says: " + body).green);
currentTrack = message;
}
})
}
}
function onPaused(data) {
console.log("PAUSED!".yellow, data);
}
function saveConfig() {
nconf.save(function (err) {
if (err) {
console.error('Error saving slack-audio conf file.'.red);
throw err;
}
console.log('Saved nconf file successfully!'.green);
fs.readFile(defaultConfigFilePath, function (err, data) {
if (err) throw err;
console.log('[Current Config]'.grey);
console.dir(JSON.parse(data.toString()));
});
});
}
// Verbalize `runner`
// log.runner = 'nowplaying-slack';