-
Notifications
You must be signed in to change notification settings - Fork 1
/
entryHelper.js
65 lines (59 loc) · 2.28 KB
/
entryHelper.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
import fs from 'fs';
import { JSDOM } from 'jsdom';
import { log } from './util/logger.js';
import Constants from "./Constants.js";
// -----------------------------------------------------------------------------------------------
// === HELPER FUNCTIONS ===
// Function to extract 10 result lines from the result HTML Page.
const getRowsFromTable = (entryType, tableElement, numberOfEntries) => {
if (Constants.ENTRY_TYPE_RESULT === entryType) {
return [...tableElement.firstElementChild.children].slice(1, numberOfEntries);
} else if (Constants.ENTRY_TYPE_DATESHEET === entryType) {
return [...tableElement.lastElementChild.children].slice(0, numberOfEntries);
} else
return [];
}
const extractEntriesFromPage = (entryType, htmlPage) => {
const numberOfEntries = 10;
let html = new JSDOM(htmlPage);
let tableElement = html.window.document.querySelector("table");
let rows = getRowsFromTable(entryType, tableElement, numberOfEntries);
let message = `📅 *RECENT ${numberOfEntries} ${entryType.toUpperCase()}S :*\n\n`;
rows.forEach(row => {
let dateElementMessage = "---";
if (row.children.length !== 1) {
dateElementMessage = getPossiblySpanWrappedElement(row.lastElementChild);
}
message += `*${dateElementMessage}:*\n`;
let titleElementMessage = getPossiblySpanWrappedElement(row.firstElementChild);
message += `${sanitizePdfTitle(titleElementMessage)}\n\n`;
});
return message;
};
// Get possibly span wrapped element
function getPossiblySpanWrappedElement(element) {
let elementString = element.innerHTML.trim();
return elementString.replace(/<\/?[^>]+(>|$)/g, '');
}
// Sanitize PDF Title
function sanitizePdfTitle(title) {
return title
.replace('\n\t', '')
.replace(/&/g, '&')
.replace(/ /g, ' ')
.replace('_', '-').trim();
}
// Writes the content of dataObj to the data.json file.
const writeDataFile = (dataObj) => {
try {
fs.writeFileSync(Constants.DATA_FILE_PATH, JSON.stringify(dataObj, null, 4), 'utf8');
log('=> Data written to file successfully.');
} catch (err) {
log('=> Error in writing data to file:');
log(err);
}
};
export default {
extractEntriesFromPage,
writeDataFile
};