-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
103 lines (90 loc) · 2.43 KB
/
server.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
const Path = require('path');
const Hapi = require('hapi');
const Inert = require('inert');
const Boom = require('boom');
const Req = require('cheerio-req')
// Jersey shore
//const url = 'http://events.gotsport.com/events/schedule.aspx?eventid=63230'
// EDP Spring
// const url = 'http://events.gotsport.com/events/schedule.aspx?eventid=64066'
const baseUrl = 'http://events.gotsport.com/events/schedule.aspx?eventid=';
async function getSchedule(eventId) {
return new Promise((resolve, reject) => {
Req(`${baseUrl}${eventId}`, (err, $) => {
if (err) {
return reject(Boom.badImplementation('Error fetching schedule', err));
}
let currentDay
let games = []
$('div.SubHeading').parent().find('tr').each((i, e) => {
const day = $(e).has('.PageHeading')
// console.info(day.text().trim());
if (day.length) {
currentDay = day.text().trim()
return;
}
if ($(e).is('tr[bgcolor]')) {
const data = $(e).children()
games.push({
day: currentDay,
gameNumber: data.eq(0).text(),
time: data.eq(1).text(),
homeTeam: data.eq(2).children('a').first().text(),
awayTeam: data.eq(4).children('a').first().text(),
location: data.eq(6).children('a').first().text()
})
}
})
games = games.filter(game => {
if (game.awayTeam.toLowerCase().includes('princeton fc') || game.homeTeam.toLowerCase().includes('princeton fc')) {
return true;
}
return false;
})
return resolve(games);
})
});
};
const server = Hapi.server({
port: 3000,
host: 'localhost',
routes: {
files: {
relativeTo: Path.join(__dirname, 'dist')
}
}
});
server.route({
method: 'GET',
path: '/schedule/{eventId}',
handler: (request, h) => {
return getSchedule(request.params.eventId);
}
});
const init = async () => {
await server.register(Inert);
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
return h.file('index.html');
}
});
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: '.',
index: true
}
}
});
await server.start();
console.log(`Server running at: ${server.info.uri}`);
};
process.on('unhandledRejection', (err) => {
console.error(err);
process.exit(1);
});
init();