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() }) {
|
2022-06-16 20:38:26 +02:00
|
|
|
if (!channels.every(c => c instanceof Channel)) {
|
|
|
|
throw new Error('"channels" must be an array of Channels')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!programs.every(p => p instanceof Program)) {
|
|
|
|
throw new Error('"programs" must be an array of Programs')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isDate(date)) {
|
|
|
|
throw new Error('"date" must be a valid date')
|
|
|
|
}
|
|
|
|
|
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-07-01 01:45:59 +02:00
|
|
|
...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)])
|
|
|
|
),
|
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')]),
|
2022-07-01 01:45:59 +02:00
|
|
|
...program.categories.map(category =>
|
|
|
|
el('category', { lang: category.lang }, [escapeString(category.value)])
|
|
|
|
),
|
2022-06-16 14:07:56 +02:00
|
|
|
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
|
|
|
}
|