epg-grabber/src/xmltv.js

126 lines
3.5 KiB
JavaScript
Raw Normal View History

2022-06-16 14:07:56 +02:00
const { escapeString, getUTCDate, formatDate } = require('./utils')
const el = createElement
2022-06-11 20:01:39 +02:00
module.exports.generate = generate
function generate({ channels, programs, date = getUTCDate() }) {
2022-06-13 16:07:28 +02:00
let output = `<?xml version="1.0" encoding="UTF-8" ?>`
2022-06-13 16:23:00 +02:00
output += createElements(channels, programs, date)
2022-06-11 20:01:39 +02:00
return output
}
2022-06-13 16:07:28 +02:00
function createElements(channels, programs, date) {
2022-06-16 14:07:56 +02:00
return el('tv', { date: formatDate(date, 'YYYYMMDD') }, [
2022-06-13 16:23:00 +02:00
...channels.map(channel => {
return (
'\r\n' +
2022-06-16 14:07:56 +02:00
el('channel', { id: channel.id }, [
2022-06-13 16:23:00 +02:00
el('display-name', {}, [escapeString(channel.name)]),
2022-06-13 16:07:28 +02:00
el('icon', { src: channel.logo }),
2022-06-16 14:07:56 +02:00
el('url', {}, [channel.url])
2022-06-13 16:07:28 +02:00
])
2022-06-13 16:23:00 +02:00
)
}),
...programs.map(program => {
return (
'\r\n' +
el(
2022-06-13 16:07:28 +02:00
'programme',
{
start: formatDate(program.start, 'YYYYMMDDHHmmss ZZ'),
stop: formatDate(program.stop, 'YYYYMMDDHHmmss ZZ'),
channel: program.channel
},
[
2022-06-15 16:20:59 +02:00
el('title', {}, [escapeString(program.title)]),
el('sub-title', {}, [escapeString(program.sub_title)]),
el('desc', {}, [escapeString(program.description)]),
2022-06-13 16:07:28 +02:00
el('credits', {}, [
2022-06-16 14:07:56 +02:00
...program.directors.map(data => createCastMember('director', data)),
...program.actors.map(data => createCastMember('actor', data)),
...program.writers.map(data => createCastMember('writer', data)),
...program.adapters.map(data => createCastMember('adapter', data)),
...program.producers.map(data => createCastMember('producer', data)),
...program.composers.map(data => createCastMember('composer', data)),
...program.editors.map(data => createCastMember('editor', data)),
...program.presenters.map(data => createCastMember('presenter', data)),
...program.commentators.map(data => createCastMember('commentator', data)),
...program.guests.map(data => createCastMember('guest', data))
2022-06-13 16:07:28 +02:00
]),
2022-06-16 14:07:56 +02:00
el('date', {}, [formatDate(program.date, 'YYYYMMDD')]),
...program.categories.map(category => el('category', {}, [escapeString(category)])),
el('icon', { src: program.icon.src }),
...program.urls.map(createURL),
...program.episodeNumbers.map(episode =>
el('episode-num', { system: episode.system }, [episode.value])
2022-06-13 16:23:00 +02:00
),
2022-06-16 14:07:56 +02:00
...program.ratings.map(rating =>
2022-06-13 16:07:28 +02:00
el('rating', { system: rating.system }, [
el('value', {}, [rating.value]),
el('icon', { src: rating.icon })
])
)
]
)
2022-06-13 16:23:00 +02:00
)
2022-06-16 18:11:11 +02:00
}),
'\r\n'
2022-06-13 16:23:00 +02:00
])
2022-06-13 16:07:28 +02:00
}
function createCastMember(position, data) {
return el(position, {}, [
2022-06-13 16:23:00 +02:00
escapeString(data.value),
2022-06-16 14:07:56 +02:00
...data.url.map(createURL),
...data.image.map(createImage)
2022-06-13 16:07:28 +02:00
])
}
2022-06-11 20:01:39 +02:00
2022-06-13 16:07:28 +02:00
function createImage(image) {
return el(
'image',
{
type: image.type,
size: image.size,
orient: image.orient,
system: image.system
},
[image.value]
)
}
2022-06-11 20:01:39 +02:00
2022-06-13 16:07:28 +02:00
function createURL(url) {
return el('url', { system: url.system }, [url.value])
2022-06-11 20:01:39 +02:00
}
2022-06-13 16:07:28 +02:00
function createElement(name, attrs = {}, children = []) {
2022-06-13 16:23:00 +02:00
return toString({ name, attrs, children })
2022-06-13 16:07:28 +02:00
}
2022-06-11 20:01:39 +02:00
2022-06-13 16:07:28 +02:00
function toString(elem) {
2022-06-16 14:07:56 +02:00
if (typeof elem === 'string' || typeof elem === 'number') return elem
2022-06-11 20:01:39 +02:00
2022-06-13 16:07:28 +02:00
let attrs = ''
for (let key in elem.attrs) {
let value = elem.attrs[key]
if (value) {
attrs += ` ${key}="${escapeString(value)}"`
2022-06-11 20:01:39 +02:00
}
}
2022-06-13 17:18:03 +02:00
if (elem.children.filter(Boolean).length) {
2022-06-13 16:07:28 +02:00
let children = ''
elem.children.forEach(childElem => {
children += toString(childElem)
})
2022-06-11 20:01:39 +02:00
2022-06-13 16:07:28 +02:00
return `<${elem.name}${attrs}>${children}</${elem.name}>`
2022-06-11 20:01:39 +02:00
}
2022-06-13 16:07:28 +02:00
if (!attrs) return ''
2022-06-13 17:18:03 +02:00
if (!['icon'].includes(elem.name)) return ''
2022-06-13 16:07:28 +02:00
return `<${elem.name}${attrs}/>`
2022-06-11 20:01:39 +02:00
}