sparkychild

Modlog Conversion Script

Dec 15th, 2017
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. const fs = require('fs');
  4.  
  5. function toId(s) {
  6.     return "" + (s && typeof s === "string" ? s : "").toLowerCase().replace(/[^a-z0-9]+/g, "");
  7. }
  8.  
  9. function convertLine(line) {
  10.     if (!line) return "";
  11.    
  12.     // first we save and remove the timestamp and the roomname
  13.     let prefix = line.match(/\[.+?\] \(.+?\) /i)[0];
  14.     line = line.replace(prefix, '');
  15.  
  16.     // apply the drastic replaces to mod actions first - these are the most important
  17.     if (/^.{1,19}(?:\W+)? was (muted|unmuted|warned|banned|unbanned|locked|globally banned|unlocked|globally unbanned)(?: from .+?)? by .{1,19}(?:\W+)?(?: for (?:7|60) minutes)?\./i.test(line)) {
  18.         line = line
  19.             .replace(/^(.{1,19}?)(?:\W+)? was muted (by .{1,19}?)(?:\W+)? for 7 minutes\.( \(.+\)$)?/i, 'MUTE: [$1] $2$3')
  20.             .replace(/^(.{1,19}?)(?:\W+)? was muted (by .{1,19}?)(?:\W+)? for 60 minutes\.( \(.+\)$)?/i, 'HOURMUTE: [$1] $2$3')
  21.             .replace(/^(.{1,19}?)(?:\W+)? was unmuted (by .+)(?:\W+)?\.$/i, 'UNMUTE: [$1] $2')
  22.             .replace(/^(.{1,19}?)(?:\W+)? was warned (by .{1,19}?)(?:\W+)?(?:\.$|\.( \(.+\)$))/i, 'WARN: [$1] $2$3')
  23.             .replace(/^(.{1,19}?)(?:\W+)? was banned from .+? (by .{1,19}?)(?:\W+)?\.( \(.+\))?( \(.+\))$/i, 'ROOMBAN: [$1]$4 $2$3')
  24.             .replace(/^(.{1,19}?)(?:\W+)? was unbanned from .+? (by .{1,19}?)(?:\W+)?\.$/i, 'ROOMUNBAN: [$1] $2')
  25.             .replace(/^(.{1,19}?)(?:\W+)? was locked from talking (by .{1,19}?)(?:\W+)?\.( \(.+\))?( \(.+\))$/i, 'LOCK: [$1]$4 $2$3')
  26.             .replace(/^(.{1,19}?)(?:\W+)? was globally banned (by .{1,19}?)(?:\W+)?\.( \(.+\))?( \(.+\))$/i, 'BAN: [$1]$4 $2$3')
  27.             .replace(/^(.{1,19}?)(?:\W+)? was globally unbanned (by .{1,19}?)(?:\W+)?\.$/i, 'UNBAN: [$1] $2')
  28.             .replace(/^(.{1,19}?)(?:\W+)? was unlocked from (by .{1,19}?)(?:\W+)?\.$/i, 'UNLOCK: [$1] $2')
  29.  
  30.            
  31.             // finally convert most things to userids!
  32.             .replace(/\[.+\](?= (?:\(.+?\) )?by )/i, match => `[${toId(match)}]`)
  33.             .replace(/by .{1,19}(?=$| \(.+?\)$)/i, match => `by ${toId(match.slice(2))}`);
  34.            
  35.             return prefix + line;
  36.     }
  37.     // generic grammatical patterns that can be automatically picked up
  38.     line = line.replace(/^.{1,19}(?:\W+)?(?= would be)/g, match => `[${toId(match)}]`) // identify subject - conditional (would be muted, would be banned, etc)
  39.         .replace(/by .{1,19}(?:(?![\.\)]+)\W+)?(?=\.(?:\))?(?:\(.+\))?$| but)/g, match => `by [${toId(match.slice(2))}]`) // identify the object of each line and convert to userid.
  40.         .replace(/^\(?.{1,19}(?:\W+)?(?= [hw]as (?:.+?ed|set|reset)\b)/g, match => (match.startsWith('(') ? '(' : '') + `[${toId(match)}]`) // identify the subject of each line
  41.  
  42.         // specific cases
  43.         .replace(/^.{1,19}(?:\W+)?(?= (?:globally )? declared)/i, match => `[${toId(match)}]`) // (global declares)
  44.         .replace(/^\(?.{1,19}(?:\W+)?(?= (?!(has|was)\b)(?:created|changed the|set modchat to|started (a|the)|notes))/i, match => (match.startsWith('(') ? '(' : '') + `[${toId(match)}]`);
  45.  
  46.     return prefix + line;
  47. }
  48.  
  49. function convertLogs(file) {
  50.     const filename = 'logs/modlog/' + file;
  51.     const data = fs.readFileSync(filename).toString().split('\n')
  52.         .map(line => convertLine(line));
  53.     fs.writeFile(filename, data.join('\n'), () => {});
  54. }
  55.  
  56. function convertGlobalModlog() {
  57.     const file = 'logs/modlog/modlog_global.txt';
  58.     let data = fs.readFileSync(file).toString().split('\n');
  59.     data = data.map(line => line.replace(/by .{1,19}(?:\W+)?(?=: |$)/, match => `by ${toId(match.slice(2))}`));
  60.     fs.writeFile(file, data.join('\n'), () => {});
  61. }
  62.  
  63. console.time('modlog conversion');
  64.  
  65. // convert non global modlog files first!
  66. fs.readdir('logs/modlog', (err, dir) => {
  67.     for (let file of dir) {
  68.         if (file === 'modlog_global.txt' || file === 'README.md') continue;
  69.         convertLogs(file);
  70.     }
  71. });
  72. convertGlobalModlog();
  73. console.timeEnd('modlog conversion');
Advertisement
Add Comment
Please, Sign In to add comment