-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (79 loc) · 2.88 KB
/
index.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
var cheerio = require('cheerio'),
request = require('request'),
url = 'http://www.wblivesurf.com/',
data = {};
exports.handler = (event, context, callback) => {
var logoCount = 0;
request(url, function (error, response, body) {
/**
* @todo Add error handling
*/
data.isBase64Encoded = false;
data.statusCode = response.statusCode;
data.headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
};
data.body = {};
var $ = cheerio.load(body);
//Get the date of the report
$('.date').filter(function () {
var date = $(this).text().split('/');
data.body.date = date[1].trim();
});
//Get the time of the report
$('.time').filter(function () {
var time = $(this).text().split('/');
data.body.time = time[0].trim();
});
//Get the report description
$('#wbMainReport .item').each(function (i, elem) {
$('.date', $(this)).remove();
$('h3', $(this)).remove();
$('.time', $(this)).remove();
data.body.desc = $(this).text().split(":").pop().trim();
});
//Get the logos
$('.current div').each(function (i, elem) {
if (this.attribs.class == 'ratingCell half') {
logoCount += 0.5;
}
else if (this.attribs.class == 'ratingCell') {
logoCount += 1;
}
}).attr('class');
data.body.logoCount = logoCount;
//Get other information
$('p', '#wbMainRating').each(function (i, elem) {
var info = $(this).text().split(':');
switch (info[0]) {
case 'Swell Size':
data.body.size = info[1].trim();
break;
case 'Water Surface':
data.body.surface = info[1].trim();
break;
case 'Water Temp':
data.body.waterTemp = info[1].trim();
break;
case 'Wind':
data.body.wind = info[1].trim();
break;
case 'High Tide':
data.body.highTide = `${info[1]}:${info[2]}:${info[3]}`.trim();
break;
case 'Low Tide':
data.body.lowTide = `${info[1]}:${info[2]}:${info[3]}`.trim();
break;
case 'Sunrise':
data.body.sunrise = `${info[1]}:${info[2]}:${info[3]}`.trim();
break;
case 'Sunset':
data.body.sunset = `${info[1]}:${info[2]}:${info[3]}`.trim();
break;
}
});
data.body = JSON.stringify(data.body);
callback(null, data);
});
}