-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
155 lines (144 loc) · 3.84 KB
/
app.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
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
const express = require('express');
const axios = require('axios');
const cheerio = require('cheerio');
const NodeCache = require('node-cache');
const app = express();
const cache = new NodeCache({ stdTTL: 60 * 60, checkperiod: 120 });
let link = 'https://www.ratehub.ca/banks/bank-mortgage-rates'
let parseHtml = (response) => {
return cheerio.load(response.data);
}
let scrape = (html) => {
let parsedData = []
html('.rh-table > tbody > tr').each(function (i, element) {
let row = {
'provider': html(element).find('span .provider-name').text().trim(),
'rates': [
{
'type': '5-years-variable',
'rate': parseFloat(html(element).find('td:nth-child(2) > a > span > span').text().trim()) || 0,
'comment': html(element).find('td:nth-child(2) > a > span > div').text().trim()
},
{
'type': '3-years-fixed',
'rate': parseFloat(html(element).find('td:nth-child(3)').text().trim()) || 0
},
{
'type': '5-years-fixed',
'rate': parseFloat(html(element).find('td:nth-child(4)').text().trim()) || 0
},
{
'type': '10-years-fixed',
'rate': parseFloat(html(element).find('td:nth-child(5)').text().trim()) || 0
}
]
}
parsedData.push(row);
});
return parsedData;
}
let filter = (parsedData, name) => {
if (name === undefined) {
return parsedData.filter(n => n['provider'] !== '');
}
else {
return parsedData.filter(n => n['provider'].toLowerCase() === decodeURIComponent(name).toLowerCase());
}
}
let respond = (filteredData, res) => {
if(filteredData.length > 1) {
cache.set('/', filteredData, function(err, success) {
if (!err && success) {
objectToSave = {
'mortgages': filteredData
}
return res.status(200).send(objectToSave);
}
});
}
else {
objectToSave = {
'mortgages': filteredData
}
return res.status(200).send(objectToSave);
}
}
let handleError = (error, res) => {
if (error.request) {
res.status(500).send({ error: 'Failed to get the response from server' });
console.error(error.request);
} else {
res.status(500).send({ error: 'Unknown error happened' });
console.error('Error', error.message);
}
console.error(error.config);
}
app.get('/', function (req, res) {
let key = '/';
cache.get(key, (err, value) => {
if (!err) {
if (value == undefined) {
axios.get(link)
.then((response) => {
console.log('[INFO] Parsing');
return parseHtml(response, res);
})
.then((html) => {
console.log('[INFO] Scraping');
return scrape(html);
})
.then((data) => {
console.log('[INFO] Filtering');
return filter(data);
})
.then((filteredData) => {
console.log('[INFO] Responding');
return respond(filteredData, res, key);
})
.catch((error) => {
handleError(error, res);
});
}
else {
console.log('[INFO] JSON retrieved from cache');
return respond(value, res);
}
}
});
});
app.get('/:name', function (req, res) {
let key = req.params.name;
cache.get('/', (err, value) => {
if (!err) {
if (value == undefined) {
axios.get(link)
.then((response) => {
console.log('[INFO] Parsing');
return parseHtml(response, res);
})
.then((html) => {
console.log('[INFO] Scraping');
return scrape(html);
})
.then((data) => {
console.log('[INFO] Filtering');
return filter(data, key);
})
.then((filteredData) => {
console.log('[INFO] Responding');
return respond(filteredData, res, key);
})
.catch((error) => {
handleError(error, res);
});
}
else {
console.log('[INFO] JSON retrieved from cache');
return respond(filter(value, key), res);
}
}
});
});
// app.listen(3000, () => console.log('Example app listening on port 3000!'));
// Export your Express configuration so that it can be consumed by the Lambda handler
module.exports = app