fix location and duration parsing

This commit is contained in:
Ondrej Synacek 2019-10-17 22:14:08 +02:00
parent 0b241ffd07
commit a94d3a3550
2 changed files with 27 additions and 18 deletions

View File

@ -2,14 +2,7 @@ const ics = require('ics')
const generateICS = async(data) => {
return new Promise((resolve, reject) => {
ics.createEvent({
title: data.title || '',
start: data.start || [],
duration: data.duration || { hours: 2 },
location: data.location || '',
description: data.description || '',
url: data.url || '',
}, (err, value) => {
ics.createEvent(data, (err, value) => {
if (err) {
reject(err)
return

View File

@ -1,16 +1,29 @@
const dayjs = require('dayjs')
const cheerio = require('cheerio')
// NOTE: Specific formatting for `ics` library
const parseDates = (startDate, endDate) => {
const start = [
const start = startDate ? [
startDate.year(),
startDate.month() + 1,
startDate.date(),
startDate.hour(),
startDate.minute(),
]
] : (() => {
const now = dayjs()
return [
now.year(),
now.month() + 1,
now.date()
]
})()
const diffInMinutes = endDate ?
endDate.diff(startDate, 'minutes') :
120
const duration = { minutes: diffInMinutes }
const duration = { hours: 1 }
return {
start,
duration,
@ -24,21 +37,22 @@ const parseEventData = (eventData) => {
const { location } = eventData || {}
const { address } = location || {}
const locationStr = location && [
const locationStr = location ? [
location.name || '',
address.streetAddress || '',
address.addressLocality || '',
address.postalCode || '',
address.addressCountry || '',
].join(', ')
const title = eventData.name
const url = eventData.url
const description = eventData.description
].join(' ') : ''
const cleanedLocationStr = locationStr.replace(/\r?\n|\r/g, ' ')
const title = eventData.name || ''
const url = eventData.url || ''
const description = eventData.description || ''
return {
start,
duration,
location: locationStr,
location: cleanedLocationStr,
title,
url,
description,
@ -65,7 +79,9 @@ const parseHTML = (html) => {
return data
}, null)
const eventData = JSON.parse(rawData.slice(12, -5))
const eventData = rawData ?
JSON.parse(rawData.slice(12, -5)) :
{}
const data = parseEventData(eventData)
console.log(data)