-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
232 lines (185 loc) · 5.05 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
var fs = require('fs');
var _ = require('underscore');
var byline = require('byline');
var Slack = require('node-slack');
var request = require('request');
var requestify = require('requestify');
var memwatch = require('memwatch-next');
var config = require('./config');
// xmltojson
var parseString = require('xml2js').parseString;
console.log(config);
// Slack api
var options = "";
var slack = new Slack(config.slack.hook_url, options);
memwatch.on('leak', function(info) {
console.log(info);
});
// A couple mixins
_.mixin({
compactObject : function(o) {
var clone = _.clone(o);
_.each(clone, function(v, k) {
if(!v) {
delete clone[k];
}
});
return clone;
},
hex2a: function(hexx) {
//converts hex to string
var hex = hexx.toString();//force conversion
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
});
var shairportParser = (function(_, slack) {
var current = {
artist: "",
title: "",
album: "",
artwork: ""
};
var next = _.clone(current);
return {
parseBufferStream: function(items) {
return _.chain(items)
.map(function(val, key) {
// console.log(val.code[0]);
return {
type: _(val).find(function(v, k) { return k === 'type'}),
code: _(val).find(function(v, k) { return k === 'code'}),
data: _(val).find(function(val, key) {return key === 'data'})
}
}).map(function(val, key) {
if(val.data) {
// console.log(val.type);
var type = _.hex2a(val.type[0]);
var code = _.hex2a(val.code[0]);
var data = new Buffer(val.data[0]._, 'base64');
// console.log(type.toString('utf8'));
// console.log(data.toString('utf8'));
return {
type: type.toString('utf8'),
data: data.toString('utf8'),
code: code.toString('utf8')
}
}else {
var type = _.hex2a(val.type[0]);
var code = _.hex2a(val.code[0]);
return {
type:type,
code: code
}
}
}).value();
},
createShairObj: function(obj, cb) {
// asal = album
// asar = artist
// minm = song name
next = {};
_(obj).each(function(val) {
switch(val.code) {
case "asal":
_(next).extend({album: val.data});
break;
case "asar":
_(next).extend({artist: val.data});
break;
case "minm":
_(next).extend({title: val.data});
break;
}
});
if(current.title !== next.title && !_.isEmpty(next.title)) {
current = next;
var artist = encodeURIComponent(current.artist);
var song = encodeURIComponent(current.title);
var url = 'https://itunes.apple.com/search?term=' + song + "%20" +artist;
request(url, function(error, response, body) {
if (!error && response.statusCode == 200) {
var itunesObj = JSON.parse(body).results;
var artwork = _(itunesObj).first();
if(!_.isEmpty(artwork)) {
current.artwork = artwork.artworkUrl100;
current.link = artwork.trackViewUrl;
}
}
if(typeof cb === "function") {
cb(current);
}
});
}
},
sendToSlack: function(songObj) {
var textOut;
if(songObj.album) {
textOut = "*" + songObj.artist +"*\n_" + songObj.album + "_";
}else {
textOut = "*" + songObj.artist +"*";
}
var options = {
attachments: [
{
"title" : "“" + songObj.title + "”",
"title_link" : songObj.link,
"text" : textOut,
"fallback": "",
"thumb_url": songObj.artwork,
"mrkdwn_in": ["text"]
}
]
};
options.attachments[0] = _.compactObject(options.attachments[0]);
var boston = slack.send(options);
},
sendToFirebase: function(songObj) {
var options = {
host: config.firebase.host,
method: "PUT"
};
var fireSong = {
"album": "",
"artist": "",
"artwork": "",
"comment": "",
"genre": "",
"title": ""
};
requestify.put(options.host, _(fireSong).extend(songObj));
}
}
})(_, slack);
// Read from the FIFO
var s = byline(fs.createReadStream('/tmp/shairport-sync-metadata', { encoding: 'utf8' }));
var buffer = "";
s.on('data', function(data, err){
buffer += data;
});
setInterval(function() {
// every interval we are going to check on the buffer to see if its collected any info for us
// There might be a better way to do this, like maybe a delimiter of some sort
// but I haven't had success with it.
var xml = "<root>" + buffer + "</root>";
parseString(xml, function(err, results) {
if(!err && buffer) {
buffer = "";
var items = results.root.item;
// parse the buffer xml -> json object
var result = shairportParser.parseBufferStream(items);
// create a readable object, include artwork from itunes if we can
shairportParser.createShairObj(result, function(data) {
console.log(data);
if(data.title && data.artist) {
shairportParser.sendToSlack(data);
shairportParser.sendToFirebase(data);
}
});
}else {
buffer = "";
}
});
},6000);