Update znbc.co.zm.config.js

This commit is contained in:
Aleksandr Statciuk 2021-08-28 02:26:22 +03:00
parent 22b197d3f4
commit 1489992f00

View file

@ -2,9 +2,11 @@ const jsdom = require('jsdom')
const { JSDOM } = jsdom
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone')
const customParseFormat = require('dayjs/plugin/customParseFormat')
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(customParseFormat)
module.exports = {
@ -24,43 +26,52 @@ module.exports = {
return img ? img.dataset.src : null
},
parser({ content, date }) {
const day = date.day() // 0 => Sunday
const programs = []
const dom = new JSDOM(content)
const tabs = dom.window.document.querySelectorAll(
`.elementor-tabs-content-wrapper > div[id*='elementor-tab-content']`
)
const items = tabs[day].querySelectorAll(`table > tbody > tr`)
const items = parseItems(content, date)
items.forEach(item => {
const row = (item.querySelector('td > p') || { textContent: '' }).textContent
const parts = row.split(' ')
const time = parts.shift()
const title = parts
.filter(str => str && /\S/g.test(str))
.map(i => i.trim())
.join(' ')
if (!time || !title) return false
const start = dayjs
.utc(time, 'HH:mm')
.set('D', date.get('D'))
.set('M', date.get('M'))
.set('y', date.get('y'))
if (!start.isValid()) return false
if (programs.length && !programs[programs.length - 1].stop) {
const title = parseTitle(item)
const start = parseStart(item, date)
const stop = parseStop(item, start)
if (programs.length) {
programs[programs.length - 1].stop = start
}
programs.push({
title,
start
})
programs.push({ title, start, stop })
})
return programs
}
}
function parseStop(item, date) {
return date.endOf('d')
}
function parseStart(item, date) {
const row = (item.querySelector('td > p') || { textContent: '' }).textContent
let time = row.split(' ').shift()
time = `${date.format('MM/DD/YYYY')} ${time}`
return dayjs.tz(time, 'MM/DD/YYYY HH:mm', 'Africa/Lusaka')
}
function parseTitle(item) {
const row = (item.querySelector('td > p') || { textContent: '' }).textContent
const title = row.split(' ')
title.shift()
return title
.map(i => i.trim())
.filter(s => s)
.join(' ')
}
function parseItems(content, date) {
const day = date.day() // 0 => Sunday
const dom = new JSDOM(content)
const tabs = dom.window.document.querySelectorAll(
`.elementor-tabs-content-wrapper > div[id*='elementor-tab-content']`
)
return tabs[day].querySelectorAll(`table > tbody > tr:not(:first-child)`)
}