-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
43 lines (40 loc) · 1.36 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
const express = require('express')
const https = require('https')
const cheerio = require('cheerio')
const app = express()
const request = https.request
const cheerioHandler = html => {
let els = cheerio(html).find('table.table-striped').find('tr')
let data = []
for (let i = 0; i < els.length; i++) {
let item = els.eq(i)
let title = item.find('.views-field-title h2').text().trim()
data.push({
title,
img: item.find('img').eq(0).attr('src'),
start: item.find('.date-display-start').attr('content'),
end: item.find('.date-display-end').attr('content'),
detail: item.text().trim().replace(title, ''),
href: item.find('a').eq(0).attr('href')
})
}
return data
}
const htmlParser = response => {
console.log(`resposne statusCode : ${response.statusCode}`)
return new Promise(resolve => {
let html = ''
response.on('data', chunck => html += chunck)
response.on('end', end => {
resolve(cheerioHandler(html))
})
})
}
app.get('/api/events', (req, res) => {
const xhr = request('https://www.vatsim.net/events', response => htmlParser(response).then(data => {
res.json(data)
}))
xhr.end()
xhr.on('error', console.error)
})
app.listen(8200, listen => console.log(`server is listening on 8200 `))