Skip to content

Instantly share code, notes, and snippets.

@sescobb27
Last active January 8, 2020 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sescobb27/053b3648a2dc7601e35a94cb4bf4ecee to your computer and use it in GitHub Desktop.
Save sescobb27/053b3648a2dc7601e35a94cb4bf4ecee to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<input type="file" id="input" onchange="handle_csv(this.files)" />
<button onclick="download()">Format And Download CSV</button>
</body>
<script>
const regex = /([0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}): command: ([a-z_]+): (.+)/;
const headers = [
"year",
"time",
"user",
"key",
"timestamp",
"team",
"confirmed",
"id",
"game_time",
"event_id",
"inc",
"status_id"
].join(", ");
log = null;
function handle_csv(files) {
csv = files[0];
reader = new FileReader();
reader.onload = function(e) {
// The file's text will be printed here
log = e.target.result;
};
reader.readAsText(csv);
}
function csv_string_to_objects(log) {
return log
.split("\n")
.map(function(line) {
attributes = line.match(regex);
if (attributes) {
return {
year: attributes[1],
time: attributes[2],
user: attributes[3],
metadata: JSON.parse(attributes[4])
};
}
})
.filter(entry => entry && entry["metadata"]["key"] !== "comment");
}
function generate_csv_content() {
csv_body = csv_string_to_objects(log)
.map(entry => objectToCSV(entry, ["year", "time", "user", "metadata"]))
.join("\n");
return (csv_content = headers + "\n" + csv_body);
}
function objectToCSV(object, keys) {
return keys
.map(function(key) {
if (typeof object[key] === "object") {
return objectToCSV(object[key], [
"key",
"timestamp",
"team",
"confirmed",
"id",
"game_time",
"event_id",
"inc",
"status_id"
]);
} else {
return JSON.stringify(object[key]);
}
})
.join(", ");
}
function download() {
csv_content = generate_csv_content();
link = document.createElement("a");
link.href = "data:text/csv;charset=utf-8," + encodeURI(csv_content);
link.download = "cc_log.csv";
link.target = "_blank";
document.body.appendChild(link);
link.click();
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment