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

View File

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