You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
super-hog/resources/actions/oberealtstadtEvents.js

139 lines
3.2 KiB

import got from 'got'
import path from 'path'
import dayjs from 'dayjs'
import { createWriteStream } from 'fs'
import stream from 'node:stream'
import { promisify } from 'util'
import crypto from 'crypto'
import { parse } from 'node-html-parser'
import Action from './../../src/runner/actions/action.js'
import logger from './../../src/runner/helpers/logger.js'
import EventStore from './../directus/event.js'
import { createHash } from 'node:crypto'
import url from 'node:url'
import TurndownService from 'turndown'
/**
* Download Episodes from a RSS-Feed,
*
*
* @extends Action
*
*/
class OberealtstadtEvents extends Action
{
async run() {
const results = []
const turndownService = new TurndownService()
const eventStore = new EventStore()
const currentUrl = new URL(this.config.url)
for (let item of this.data) {
const hash = createHash('sha256').update(item.id.toString()).digest('hex')
if (item.status !== 'publish') {
continue
}
const event = await eventStore.findOneBySource(currentUrl.host, hash)
if (event.data.length > 0 && event.data[0].date_updated_source <= item.modified) {
continue
}
let result = {
title: item.title.rendered,
date_updated_source: item.modified,
content: turndownService.turndown(item.content.rendered),
source_host: currentUrl.host,
source_url: item.link,
source_hash: hash
}
const htmlString = await got(item.guid.rendered, {
resolveBodyOnly: true
})
const html = parse(htmlString)
result = this._parseDates(html, result)
result = this._parseMedia(html, result)
results.push(result)
}
return results
}
/**
*
*
*/
_parseDates(html, result) {
const dates = html.querySelectorAll('.wpem-event-date-time-text')
if (dates[0]) {
result.start = this._getDate(dates[0])
}
if (dates[1]) {
result.end = this._getDate(dates[1])
}
return result
}
/**
*
*/
_getDate(string) {
const dateString = string.textContent.replace('um ', '').trim()
const dateArray = dateString.split(' ')
if (dateArray.length === 2) {
let time = dateString.split(' ')[1]
let day = dateString.split(' ')[0]
// getting day and time
day = day.split('.')[2] + '-' + day.split('.')[1] + '-' + day.split('.')[0]
time = time.split(':')[0] + ':' + time.split(':')[1]
const date = dayjs(day + ' ' + time)
if (date.isValid()) {
return date.format()
}
}
return false
}
/**
*
*
*/
_parseMedia(html, result) {
const img = html.querySelector('.wpem-event-single-image img')
if (img) {
result.mediaUrl = img.getAttribute('src')
}
return result
}
}
export default OberealtstadtEvents