epg/bin/epg-grabber/utils.js
freearhey ed1b894044 wip
2021-03-10 17:37:12 +03:00

69 lines
1.8 KiB
JavaScript

const fs = require('fs')
const path = require('path')
const convert = require('xml-js')
const dayjs = require('dayjs')
const utils = {}
utils.convertToXMLTV = function ({ channels, programs }) {
let output = `<?xml version="1.0" encoding="UTF-8" ?><tv>`
for (let channel of channels) {
output += `
<channel id="${channel['xmltv_id']}">
<display-name>${channel.name}</display-name>
</channel>`
}
for (let program of programs) {
const start = dayjs(program.start).format('YYYYMMDDHHmmss ZZ')
const stop = dayjs(program.stop).format('YYYYMMDDHHmmss ZZ')
output += `
<programme start="${start}" stop="${stop}" channel="${program.channel}">
<title lang="${program.lang}">${program.title}</title>`
if (program.category) {
output += `<category lang="${program.lang}">${program.category}</category>`
}
output += `</programme>`
}
output += '</tv>'
return output
}
utils.parseConfig = function (config) {
const xml = fs.readFileSync(path.resolve(process.cwd(), config), {
encoding: 'utf-8'
})
const result = convert.xml2js(xml)
const settings = result.elements.find(el => el.name === 'settings')
const filename = this.getElementText('filename', settings.elements)
const days = this.getElementText('days', settings.elements)
const userAgent = this.getElementText('user-agent', settings.elements)
const channels = settings.elements
.filter(el => el.name === 'channel')
.map(el => {
const channel = el.attributes
channel.name = el.elements.find(el => el.type === 'text').text
return channel
})
return {
filename,
days,
userAgent,
channels
}
}
utils.getElementText = function (name, elements) {
const el = elements.find(el => el.name === name)
return el ? el.elements.find(el => el.type === 'text').text : null
}
module.exports = utils