This repository was archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
166 lines (138 loc) · 4.5 KB
/
parser.js
File metadata and controls
166 lines (138 loc) · 4.5 KB
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
const fs = require("fs");
const path = require("path");
const {
createdDate,
createCSVFile,
formatToMySQLDatetime,
} = require("./helper_functions.js");
require("dotenv").config();
//Imports above
//These are the parse functions, include some imports
const ckey_exclusion = [
"_matxh",
"Obolont",
"Cyrix01",
"gb",
"Cauldron",
"Patternseeker",
"Borntokill946",
];
const filter_junk = () => {
const directoryPath = process.env.FILE_DIR;
fs.readdir(directoryPath, (err, files) => {
if (err) {
console.error("Error reading directory:", err);
return;
}
// Filter out .txt files
const txtFiles = files.filter((file) => path.extname(file) === ".txt");
// Remove files not in txtFiles array
files.forEach((file) => {
const filePath = path.join(directoryPath, file);
// Check if the file is not in txtFiles array
if (path.extname(file) !== ".txt" && !txtFiles.includes(file)) {
fs.unlink(filePath, (err) => {
if (err) {
console.error(`Error deleting file ${filePath}:`, err);
return;
}
});
}
});
//Now onto data sorting...
console.log("Files filtered, sorting data to csv...");
sort_data(txtFiles);
});
};
const sort_data = (txtFiles) => {
if (txtFiles.length === 0) {
return console.log("No text files found, exiting...");
}
const formattedLads = []; //store our formatted boys for eventual CSVing
console.log("Please be patient, sorting may take a minute or so...");
txtFiles.forEach((file) => {
const filePath = path.join(__dirname, "showlads_dump", file);
const data = fs.readFileSync(filePath, "utf8", (err, data) => {
if (err) {
console.error("Failed to read file: ", err);
return;
}
});
//Date parsing is a bit fucky, for coolstorium text posts we go line by line and update the date as we find it. For text files, we can pull the metadata.
let curDate = null;
const textFiles = [
"showlads.txt",
"coolstorium.txt",
"questions.txt",
"general.txt",
];
const excludedFile = textFiles.includes(file);
if (!excludedFile) {
curDate = createdDate(filePath);
}
const lines = data.split("\n");
const bulletPointLines = lines.filter((line) => /^(•|\[)/.test(line));
//regex for taking our bullet point lines and reformatting into csv
const regexWithRole = /^\s*•\s*(.*?)\((.*?)\)\s*:\s*(.*)/;
const regexWithoutRole = /^\s*•\s*(.*?)\s*:\s*(.*)/;
const regexWaitress = /^\s*•\s*(.*?)\((.*?)\)\s*:\s*(.*)/; //Because randy put a /n in them for some fucking reason
const datePattern = /\[(\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{2} (?:AM|PM))\]/;
const robotPattern = /^\s*[A-Z]{3}\d{3}\s*$/;
bulletPointLines.forEach((line) => {
const matchWithRole = line.match(regexWithRole);
const matchWaitress = line.match(regexWaitress);
const matchWithoutRole = line.match(regexWithoutRole);
if (excludedFile) {
const dateMatch = line.match(datePattern);
if (dateMatch) {
const extractedDateTime = dateMatch ? dateMatch[1] : null;
curDate = formatToMySQLDatetime(extractedDateTime);
}
}
if (matchWithRole || matchWaitress) {
let [, char_name, role, ckey] = matchWithRole;
const os13RobotMatch = robotPattern.test(role.trim());
//To get os13 robots more uniform
if (os13RobotMatch) {
role = "OS13 Robot";
}
//Removes so and so's squire
if (role.includes("Squire")) {
role = "Squire";
}
if (!ckey_exclusion.includes(ckey.trim())) {
formattedLads.push(
`${char_name.trim()},${role.trim()},${ckey
.toLowerCase()
.trim()},${curDate}`
);
} else {
formattedLads.push(
`${char_name.trim()},${role.trim()},Anonymous,${curDate}`
);
}
} else if (matchWithoutRole) {
const [, char_name, ckey] = matchWithoutRole;
if (!ckey_exclusion.includes(ckey.trim())) {
formattedLads.push(
`${char_name.trim()},Unknown,${ckey
.toLowerCase()
.trim()},${curDate}`
);
} else {
formattedLads.push(
`${char_name.trim()},Unknown,Anonymous,${curDate}`
);
}
} else {
return;
}
});
});
createCSVFile(formattedLads);
};
filter_junk();
module.exports = {
filter_junk,
sort_data,
};