This repository has been archived by the owner on Feb 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
180 lines (140 loc) · 6.01 KB
/
server.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var _ = require('lodash'),
util = require('util'),
http = require('http'),
https = require('https'),
fs = require('fs');
var config = {};
if(process.argv[2] == '-f') { // We're using a config file
fs.exists("hipfm.json", function(exists) {
if (exists) {
console.log('Using hipfm config file.');
config = (JSON.parse(fs.readFileSync("hipfm.json", "utf8")));
fetchTracks(true, config);
} else {
console.log('The hipfm.json file does not exist. Please create the file before using the -f flag.');b
process.exit(1);
}
});
} else { // We're using the command line args
if (process.argv.length < 6) {
console.log('Usage: node server.js ' + ' [Last.fm Api Key] [Last.fm User] [HipChat Admin/Notification Key] [Room] [Display Name]');
process.exit(1);
}
config = {
"hipchat": {
"key": process.argv[4],
"room": process.argv[5]
},
"lastfm": {
"users": [
{
"displayName": process.argv[6] || 'HipFM',
"username": process.argv[3],
"key": process.argv[2]
}
]
},
"dangerZoneMessages": [
'BEWARE: Highway to the Danger Zone, Stop Right Now, Thank You Very Much, We Need Somebody with a Human Touch to SKIP.',
'BOWIE SAYS: Ch-ch-ch-ch-Changes (Turn and press the skip)',
'DEVO SAYS: Skip It, Skip It Good',
'B.J SAYS: Skip to My Lou',
'Oops You Did It Again, BRITNEY SAYS: Skip'
]
}
fetchTracks(true, config);
}
function checkTracks(config) {
var tracks = null;
var dangerzone = null;
config.lastfm.users.forEach(function(element, index, array){
var lastfmURL = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=' + config.lastfm.users[index].username + '&api_key=' + config.lastfm.users[index].key + '&format=json&limit=10';
http.get(lastfmURL, function(httpRes) {
var data = '';
httpRes.on('data', function (chunk){
data += chunk;
});
httpRes.on('end',function(){
tracks = JSON.parse(data);
complete(index);
});
}).on('error', function(e) {
util.log("ERROR::checkTracks " + e.message);
});
// danger zone
var dangerZoneUrl = 'http://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=' + config.lastfm.users[index].username + '&api_key=' + config.lastfm.users[index].key + '&period=3month&format=json&limit=10';
http.get(dangerZoneUrl, function(httpRes) {
var data = '';
httpRes.on('data', function (chunk){
data += chunk;
});
httpRes.on('end',function(){
dangerzone = JSON.parse(data);
complete(index);
});
}).on('error', function(e) {
util.log("ERROR::checkTracks " + e.message);
});
});
function complete(index) {
if (tracks !== null && dangerzone !== null) {
processData(tracks, dangerzone, index);
}
}
}
function processData(data, dangerzone, userIndex) {
if (data.recenttracks && data.recenttracks.track) {
var currentTrack = data.recenttracks.track[0];
var dangerTracks = dangerzone.toptracks.track;
if (config.lastfm.users[userIndex].lastTrack != currentTrack.name) {
var dangerTrack = _.find(dangerTracks, function(track){
if(track.mbid === currentTrack.mbid || track.name === currentTrack.name) {
return track;
}
});
var html = '';
var color = 'purple';
if (currentTrack.image[1]['#text'] !== '') {
html += '<img src="' + currentTrack.image[1]['#text'] + '" height="32"/>';
}
html += '<span> </span>';
html += '<a href="' + currentTrack.url + '">' + currentTrack.name + '</a>';
html += ' - <a href="http://www.last.fm/music/' + currentTrack.artist['#text'] + '">' + currentTrack.artist['#text'] + '</a>';
html += ', <a href="http://www.last.fm/music/' + currentTrack.artist['#text'] + '/'+ currentTrack.album['#text'] + '">' + currentTrack.album['#text'] + '</a>';
if(dangerTrack) {
var message = config.dangerZoneMessages[Math.floor(Math.random()*config.dangerZoneMessages.length)];
util.log(message);
color = 'red';
html += "<div> "+ message +"</div>";
}
sendToHipChat(html, userIndex, color);
config.lastfm.users[userIndex].lastTrack = currentTrack.name;
util.log(config.lastfm.users[userIndex].lastTrack + ' - ' + currentTrack.artist['#text'] + ' - ' + currentTrack.album['#text']);
}
}
if(userIndex==0) { // Only call the timeout callback once to prevent n*2 callbacks each time.
fetchTracks(false, config);
}
}
function sendToHipChat(message, userIndex, color) {
color = color || 'purple';
message = encodeURIComponent(message);
var url = 'https://api.hipchat.com/v1/rooms/message?format=json&auth_token=' + config.hipchat.key + '&room_id=' + config.hipchat.room + '&from=' + config.lastfm.users[userIndex].displayName + '&color='+ color +'&message_format=html&message=' + message;
https.get(url, function(httpRes) {
var data = '';
httpRes.on('data', function (chunk){
data += chunk;
});
httpRes.on('end',function(){
// util.log(data);
});
}).on('error', function(e) {
util.log("ERROR::sendToHipChat " + e.message);
});
}
function fetchTracks(instant, config) {
var millis = instant ? 0 : 30000;
setTimeout(function() {
checkTracks(config);
}, millis);
}