Facebook-Events-iCal-Converter/lib/services/ics-retriever.js

36 lines
971 B
JavaScript
Raw Normal View History

const parseUsingLDJSONData = require('./ldjson-parser')
const parseUsingDOM = require('./dom-parser')
2019-10-23 20:37:06 +02:00
const generateICS = require('./ics-generator')
const { createParserError, getNormalizedUrl } = require('../utils')
2019-10-23 20:37:06 +02:00
const extractEventDataFromHTML = (html, url, { logger }) => {
2020-07-16 16:37:20 +02:00
const LDJSONEventData = parseUsingLDJSONData(html, { logger })
2020-07-16 16:48:05 +02:00
const rawEventData = LDJSONEventData || parseUsingDOM(html, { logger })
2020-07-16 16:48:05 +02:00
if (!rawEventData) {
2020-07-16 16:37:20 +02:00
throw createParserError()
return
2019-10-23 20:37:06 +02:00
}
2020-07-16 16:37:20 +02:00
2020-07-16 16:48:05 +02:00
const eventData = {
...rawEventData,
url: rawEventData.url || getNormalizedUrl(url),
2020-07-16 16:48:05 +02:00
}
return eventData
}
const retrieveICS = async (URLparameter, { logger, crawl }) => {
const url = getNormalizedUrl(URLparameter)
const html = await crawl(url, { logger })
const eventData = extractEventDataFromHTML(html, url, { logger })
2020-07-16 16:37:20 +02:00
const icsContent = await generateICS(eventData)
2020-07-16 16:37:20 +02:00
return icsContent
2019-10-23 20:37:06 +02:00
}
module.exports = {
retrieveICS,
extractEventDataFromHTML,
}