-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbreakupchatlog.js
69 lines (62 loc) · 1.88 KB
/
breakupchatlog.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
var myProductName = "Break Up Chatlog", myVerion = "0.40c";
var request = require ("request");
var fs = require ("fs");
var urlChatlogJs = "http://friends.farm/users/davewiner/chatLog.json";
function padWithZeros (num, ctplaces) {
var s = num.toString ();
while (s.length < ctplaces) {
s = "0" + s;
}
return (s);
}
function jsonStringify (jstruct, flFixBreakage) {
if (flFixBreakage === undefined) {
flFixBreakage = false;
}
var s = JSON.stringify (jstruct, undefined, 4);
if (flFixBreakage) {
s = s.replace (/\u2028/g,'\\u2028').replace (/\u2029/g,'\\u2029');
}
return (s);
}
function readChatlog (callback) {
request (urlChatlogJs, function (err, response, jsontext) {
if (err) {
console.log ("readChatlog: err.message == " + err.message);
callback (undefined);
}
else {
callback (jsontext);
}
});
}
function startup () {
console.log ("\n" + myProductName + " v" + myVerion);
readChatlog (function (jsontext) {
try {
var theLog = JSON.parse (jsontext), lastMonth = -1, currentArray = new Array ();
function writeArray () {
if (currentArray.length > 0) {
var yearPart = new Date (currentArray [0].when).getUTCFullYear ().toString ();
var f = "months/" + yearPart + "." + padWithZeros (lastMonth, 2) + ".json";
console.log ("Writing " + currentArray.length + " item(s) to file " + f);
fs.writeFileSync (f, jsonStringify (currentArray));
}
}
for (var i = 0; i < theLog.chatLog.length; i++) {
var item = theLog.chatLog [i], when = new Date (item.when), theMonth = when.getUTCMonth () + 1;
if (theMonth != lastMonth) {
writeArray ();
lastMonth = theMonth;
currentArray = new Array ();
}
currentArray [currentArray.length] = item;
}
writeArray ();
}
catch (err) {
console.log ("startup: urlChatlogJs == " + urlChatlogJs + ", err.message == " + err.message);
}
});
}
startup ();