Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (48 loc) · 1.5 KB
/
Copy pathindex.js
File metadata and controls
52 lines (48 loc) · 1.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
const fs = require('fs')
const htmlparser = require('htmlparser2')
const inFile = './MyActivity.html';
const outFile = './activity.csv';
console.log('Loading Activity');
const html = fs.readFileSync(inFile)
const out = fs.createWriteStream(outFile)
out.write("query,date,hour,year,day of week\n");
console.log('Parsing Activity');
const searchMatcher = /Searched for/i;
var newEntry = null;
var saveNextText = false;
var parser = new htmlparser.Parser({
onopentag: function(name, attribs){
if(attribs.class && attribs.class.indexOf('outer-cell') >= 0){
if (newEntry && newEntry.search && newEntry.date) {
saveNextText = false;
out.write(`"${newEntry.query}","${newEntry.date}",${newEntry.hour},${newEntry.year},${newEntry.dayOfWeek}\n`);
}
newEntry = {};
}
},
ontext: function(text){
if (saveNextText && text.trim().length > 0) {
newEntry.query = text;
saveNextText = false;
}
if (text.match(searchMatcher)) {
newEntry.search = true;
saveNextText = true;
}
//Try and parse a date (if this works, the it's the date)
let date = new Date(text);
if (date instanceof Date && isFinite(date)) {
newEntry.date = date.toISOString();
newEntry.hour = date.getHours();
newEntry.year = date.getFullYear();
newEntry.dayOfWeek = date.getDay();
newEntry.month = date.getMonth();
}
},
onend: function(){
out.end();
console.log('Completed!');
}
}, {decodeEntities: true});
parser.write(html);
parser.end();