epg-grabber/src/xmltv.js

148 lines
4.5 KiB
JavaScript
Raw Normal View History

2022-06-16 20:38:26 +02:00
const Channel = require('./Channel')
const Program = require('./Program')
const { escapeString, getUTCDate, formatDate, isDate } = require('./utils')
2022-06-16 14:07:56 +02:00
const el = createElement
2022-06-11 20:01:39 +02:00
module.exports.generate = generate
function generate({ channels, programs, date = getUTCDate() }) {
2023-01-04 02:43:13 +01:00
if (!channels.every(c => c instanceof Channel)) {
throw new Error('"channels" must be an array of Channels')
}
2022-06-16 20:38:26 +02:00
2023-01-04 02:43:13 +01:00
if (!programs.every(p => p instanceof Program)) {
throw new Error('"programs" must be an array of Programs')
}
2022-06-16 20:38:26 +02:00
2023-01-04 02:43:13 +01:00
if (!isDate(date)) {
throw new Error('"date" must be a valid date')
}
2022-06-16 20:38:26 +02:00
2023-01-04 02:43:13 +01:00
let output = `<?xml version="1.0" encoding="UTF-8" ?>`
output += createElements(channels, programs, date)
2022-06-11 20:01:39 +02:00
2023-01-04 02:43:13 +01:00
return output
2022-06-11 20:01:39 +02:00
}
2022-06-13 16:07:28 +02:00
function createElements(channels, programs, date) {
2023-01-04 02:43:13 +01:00
return el('tv', { date: formatDate(date, 'YYYYMMDD') }, [
...channels.map(channel => {
return (
'\r\n' +
el('channel', { id: channel.id }, [
el('display-name', {}, [escapeString(channel.name)]),
el('icon', { src: channel.logo }),
el('url', {}, [channel.url])
])
)
}),
...programs.map(program => {
return (
'\r\n' +
el(
'programme',
{
start: formatDate(program.start, 'YYYYMMDDHHmmss ZZ'),
stop: formatDate(program.stop, 'YYYYMMDDHHmmss ZZ'),
channel: program.channel
},
[
...program.titles.map(title =>
el('title', { lang: title.lang }, [escapeString(title.value)])
),
...program.sub_titles.map(sub_title =>
el('sub-title', { lang: sub_title.lang }, [escapeString(sub_title.value)])
),
...program.descriptions.map(desc =>
el('desc', { lang: desc.lang }, [escapeString(desc.value)])
),
el('credits', {}, [
...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))
]),
el('date', {}, [formatDate(program.date, 'YYYYMMDD')]),
...program.categories.map(category =>
el('category', { lang: category.lang }, [escapeString(category.value)])
),
el('icon', { src: program.icon.src }),
...program.urls.map(createURL),
...program.episodeNumbers.map(episode =>
el('episode-num', { system: episode.system }, [episode.value])
),
...program.ratings.map(rating =>
el('rating', { system: rating.system }, [
el('value', {}, [escapeString(rating.value)]),
el('icon', { src: rating.icon })
])
)
]
)
)
}),
'\r\n'
])
2022-06-13 16:07:28 +02:00
}
function createCastMember(position, data) {
2023-01-04 02:43:13 +01:00
return el(position, {}, [
escapeString(data.value),
...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) {
2023-01-04 02:43:13 +01:00
return el(
'image',
{
type: image.type,
size: image.size,
orient: image.orient,
system: image.system
},
[image.value]
)
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 createURL(url) {
2023-01-04 02:43:13 +01:00
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 = []) {
2023-01-04 02:43:13 +01: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) {
2023-01-04 02:43:13 +01:00
if (typeof elem === 'string' || typeof elem === 'number') return elem
2022-06-11 20:01:39 +02:00
2023-01-04 02:43:13 +01: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
2023-01-04 02:43:13 +01:00
if (elem.children.filter(Boolean).length) {
let children = ''
elem.children.forEach(childElem => {
children += toString(childElem)
})
2022-06-11 20:01:39 +02:00
2023-01-04 02:43:13 +01:00
return `<${elem.name}${attrs}>${children}</${elem.name}>`
}
2022-06-13 16:07:28 +02:00
2023-01-04 02:43:13 +01:00
if (!attrs) return ''
if (!['icon'].includes(elem.name)) return ''
2022-06-13 16:07:28 +02:00
2023-01-04 02:43:13 +01:00
return `<${elem.name}${attrs}/>`
2022-06-11 20:01:39 +02:00
}