2019-10-23 20:37:06 +02:00
|
|
|
const crawl = require('./crawler')
|
2019-10-23 22:03:20 +02:00
|
|
|
const parseUsingLDJSONData = require('./ldjson-parser')
|
|
|
|
const parseUsingDOM = require('./dom-parser')
|
2019-10-23 20:37:06 +02:00
|
|
|
const generateICS = require('./ics-generator')
|
2019-10-23 22:03:20 +02:00
|
|
|
const { createParserError, getNormalizedUrl } = require('../utils')
|
2019-10-23 20:37:06 +02:00
|
|
|
|
2019-10-24 10:21:41 +02:00
|
|
|
const retrieveICS = async (URLparameter, { logger }) => {
|
2019-10-23 20:37:06 +02:00
|
|
|
try {
|
|
|
|
const url = getNormalizedUrl(URLparameter)
|
2019-10-24 10:21:41 +02:00
|
|
|
const html = await crawl(url, { logger })
|
|
|
|
const LDJSONEventData = parseUsingLDJSONData(html, { logger })
|
|
|
|
const eventData = LDJSONEventData || parseUsingDOM(html, url, { logger })
|
2019-10-23 22:03:20 +02:00
|
|
|
|
|
|
|
if (!eventData) {
|
|
|
|
throw createParserError()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:37:06 +02:00
|
|
|
const icsFile = await generateICS(eventData)
|
|
|
|
return icsFile
|
|
|
|
} catch (err) {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = retrieveICS
|