-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc-logs-to-fake-json.js
91 lines (79 loc) · 2.72 KB
/
irc-logs-to-fake-json.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
/*
Take logs in the format given by https://archive.logbot.info/
and render JSON similar enough to the matrix logs to satisfy the renderers
*/
'use strict';
let fs = require('fs');
let path = require('path');
if (process.argv.length !== 3) {
console.error(`Usage: node ${path.basename(__filename)} logfile`);
process.exit(1);
}
let lines = fs.readFileSync(process.argv[2], 'utf8').split('\n');
let logDir;
let currentChannelName = null;
let currentFile = null;
let currentEntries = [];
for (let line of lines) {
if (line.trim() === '') {
continue;
}
let match = line.match(/^(?<ts>[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}) (?<channelName>#[A-Za-z0-9#-_]+) (?<uname>(<[^>]+>)|(-[^-]+-)|(\* [^ ]+)) (?<message>.*)/);
if (match == null) {
console.error(`could not parse line`);
console.error(line);
process.exit(1);
}
let { ts: tsString, channelName, uname, message } = match.groups;
if (currentChannelName == null) {
if (fs.existsSync(path.join('logs', 'json', 'irc-' + channelName.replace(/#/g, '')))) {
throw new Error('channel name already exists in non-historical logs');
}
logDir = path.join('logs', 'historical-json', channelName);
fs.mkdirSync(logDir, { recursive: true });
currentChannelName = channelName;
} else if (currentChannelName !== channelName) {
throw new Error('channel name is not consistent');
}
let ts = Date.parse(tsString + 'Z');
let tsDate = new Date(ts);
let year = tsDate.getUTCFullYear();
let month = ('' + (tsDate.getUTCMonth() + 1)).padStart(2, '0');
let date = ('' + tsDate.getUTCDate()).padStart(2, '0');
let fileName = `${year}-${month}-${date}.json`;
if (fileName !== currentFile) {
writeCurrentBatch();
currentFile = fileName;
currentEntries = [];
}
let isSlashMe = uname.startsWith('*');
let isIrcNotice = uname.startsWith('-');
let justUname = isSlashMe ? uname.substring(2) : uname.slice(1, -1);
let entry = {
content: {
body: message,
msgtype: isSlashMe ? 'm.emote' : 'm.text'
},
ts,
senderName: justUname,
senderId: `${justUname}@irc`,
};
if (isIrcNotice) {
entry.content.isIrcNotice = true;
}
currentEntries.push(entry);
}
writeCurrentBatch();
function writeCurrentBatch() {
if (currentFile == null) {
return;
}
// we don't just JSON.stringify because we want each event on its own line, for readability
let contents = currentEntries.length === 0 ? '[]' : '[\n' + currentEntries.map((e) => JSON.stringify(e)).join(',\n') + '\n]';
let targetFile = path.join(logDir, currentFile);
if (fs.existsSync(targetFile)) {
console.error(`${targetFile} already exists; not overwriting`);
process.exit(1);
}
fs.writeFileSync(targetFile, contents, 'utf8');
}