-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
122 lines (115 loc) · 3.82 KB
/
helpers.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
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
const fs = require('fs');
// a function to log stuff onto the console.
const log = (message, style) => {
output = "";
switch (style) {
case ('box'):
let horizontals = ''
let mLength = message.length;
for (let i = 0; i < (mLength + 2); i++) {
horizontals += '═';
}
output += '\n╔' + horizontals + '╗\n' + '║ ' + message + ' ║' + '\n╚' + horizontals + '╝';
break;
case 'm':
output += '\n- ' + message;
break;
default:
output = ' ' + message;
}
console.log(output);
};
// a function to format the output of diverse scrapes and formats onto the format required by the output feed
const formatJson = (json, what) => {
log('Formatting json for ' + what.id);
const findItems = (route) => {
var branches = route.split('>');
let obj = json;
for (let i = 0; i < branches.length; i++) {
obj = obj[branches[i].trim()];
}
return obj;
};
let sourceArray = findItems(what.itemsPath);
let output = { items: [] };
let sourceItem, outputItem;
for (let i = 0; i < sourceArray.length; i++) {
sourceItem = sourceArray[i]
outputItem = {};
for (let key in what.items) {
outputItem[key] = what.items[key](sourceItem);
}
output.items.push(outputItem);
}
return output;
};
// https get with promises
const getContent = (url) => {
// return new pending promise
return new Promise((resolve, reject) => {
// select http or https module, depending on reqested url
const lib = url.startsWith('https') ? require('https') : require('http');
const request = lib.get(url, (response) => {
log('Retrieveing content from ' + url);
// handle http errors
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error('Failed to load page, status code: ' + response.statusCode));
}
// temporary data holder
const body = [];
// on every content chunk, push it to the data array
response.on('data', (chunk) => body.push(chunk));
// we are done, resolve promise with those joined chunks
response.on('end', () => resolve(body.join('')));
});
// handle connection errors of the request
request.on('error', (err) => reject(err))
});
};
// read files asynchronously and return data in a promise
const readFileAsync = function(filename) {
return new Promise(function(resolve, reject) {
fs.readFile(filename, function(err, data){
if (err)
reject(err);
else
resolve(data);
});
});
};
// writes files asynchronously and returns written value in promise
const writeFileAsync = function(filename,contents){
return new Promise(function(resolve, reject) {
fs.writeFile(filename, contents, function (err) {
if (err) reject(err);
resolve(true);
});
});
};
// create directories asynchronously
const makeDirSync = function(path){
if (!fs.existsSync(path)) fs.mkdirSync(path);
return true;
}
// name says it all, comverts xml onto json
const xmlToJson = function (xml) {
return new Promise((resolve, reject) => {
let parseString = require('xml2js').parseString;
let output = parseString(xml, function (err, result) {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
};
module.exports = {
log: log,
getContent: getContent,
xmlToJson: xmlToJson,
formatJson: formatJson,
readFileAsync: readFileAsync,
writeFileAsync: writeFileAsync,
makeDirSync: makeDirSync
}