Facebook-Events-iCal-Converter/lib/parser.js

72 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-10-15 22:45:03 +02:00
const cheerio = require('cheerio')
const dayjs = require('dayjs')
2019-10-15 09:22:08 +02:00
const parseHTML = (html) => {
let data = {}
2019-10-15 22:45:03 +02:00
const root = cheerio.load(html)
2019-10-15 09:22:08 +02:00
2019-10-15 22:45:03 +02:00
const $title = root('#event_header_primary h1')
const titleContent = $title ? $title.text() : null
2019-10-15 09:22:08 +02:00
2019-10-15 22:45:03 +02:00
if (titleContent) {
2019-10-15 09:22:08 +02:00
data = {
...data,
2019-10-15 22:45:03 +02:00
title: titleContent,
}
}
const $timeInfo = root('#event_time_info')
const $time = $timeInfo.find('[content]')
const timeContent = $time ? $time.attr('content') : null
if (timeContent) {
const parsedTimeContent = timeContent
.split('to')
.map(part => part.trim())
const startDate = dayjs(parsedTimeContent[0]) || dayjs()
const endDate = dayjs(parsedTimeContent[1]) || dayjs()
const start = [
startDate.year(),
startDate.month(),
startDate.date(),
startDate.hour(),
startDate.minute(),
]
const end = [
endDate.year(),
endDate.month(),
endDate.date(),
endDate.hour(),
endDate.minute()
]
data = {
...data,
start,
end,
// TODO: Create duration from end date
duration: { hours: 1 },
}
}
const $locations = $timeInfo.next().find('table tr td').get(1)
const $location = $locations.children[0].children[0]
const locationContent = $location ? root($location).text() : null
if (locationContent) {
data = {
...data,
location: locationContent,
2019-10-15 09:22:08 +02:00
}
}
return data
}
module.exports = parseHTML