-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.js
87 lines (67 loc) · 2.29 KB
/
monitor.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
/*
Node.js Icecast listener count monitor.
Built by James Baber for Backwell School Radio.
Requirements:
najax: https://github.com/najaxjs/najax
fs: Included with Node.js
*/
// Get required libraries for the script
najax = require("najax");
fs = require("fs");
// Introductory text
console.log("Monitoring the listener count for the schoolradio icecast server");
// Icecast server URL
var serverWebURL = "http://localhost"; // Change me (I'm the address of the Icecast servers WEB control panel)
// Time constant configuration
var waitTime = 10; // Change me (I'm the time between logs in seconds)
var interval = 1000 * waitTime;
console.log("Seconds between logs: " + waitTime);
// Save location
var saveLoc = "listenerLog.csv"; // Change me (I'm the directory to put the csv with all the data)
function getDateTime() {
// Get the date and time into a nice format for our csv
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var day = date.getDate();
day = (day < 10 ? "0" : "") + day;
return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
}
function parseData(strToParse, strStart, strFinish)
{
// Find listener count in string from ajax call to icecast web interface
var str = strToParse.match(strStart + "(.*?)" + strFinish);
if (str != null) {
return(str[1]);
}
}
function writeLog(data) {
// Write string to file and handle errors
fs.appendFile(saveLoc, data, function(err) {
if(err) {
console.log("File writing failed! Please make sure that the file isn't open on another machine.");
}
});
}
function logData(data) {
// Create the output string
var time = getDateTime();
var output = "\r\n" + time + "," + data
// Log it with the date
writeLog(output);
}
setInterval(function() {
// Perform an ajax call to the icecast web interface
najax(serverWebURL, function(html) {
html = someText = html.replace(/(\r\n|\n|\r)/gm,"");
var listeners = parseData(html, '<td>Current Listeners:</td><td class="streamdata">', "</td></tr>");
logData(listeners);
});
}, interval);