Update xmltv.js

This commit is contained in:
Aleksandr Statciuk 2023-01-04 04:43:13 +03:00
parent 464c2c81cd
commit ef788ed044
1 changed files with 111 additions and 111 deletions

View File

@ -6,142 +6,142 @@ const el = createElement
module.exports.generate = generate module.exports.generate = generate
function generate({ channels, programs, date = getUTCDate() }) { function generate({ channels, programs, date = getUTCDate() }) {
if (!channels.every(c => c instanceof Channel)) { if (!channels.every(c => c instanceof Channel)) {
throw new Error('"channels" must be an array of Channels') throw new Error('"channels" must be an array of Channels')
} }
if (!programs.every(p => p instanceof Program)) { if (!programs.every(p => p instanceof Program)) {
throw new Error('"programs" must be an array of Programs') throw new Error('"programs" must be an array of Programs')
} }
if (!isDate(date)) { if (!isDate(date)) {
throw new Error('"date" must be a valid date') throw new Error('"date" must be a valid date')
} }
let output = `<?xml version="1.0" encoding="UTF-8" ?>` let output = `<?xml version="1.0" encoding="UTF-8" ?>`
output += createElements(channels, programs, date) output += createElements(channels, programs, date)
return output return output
} }
function createElements(channels, programs, date) { function createElements(channels, programs, date) {
return el('tv', { date: formatDate(date, 'YYYYMMDD') }, [ return el('tv', { date: formatDate(date, 'YYYYMMDD') }, [
...channels.map(channel => { ...channels.map(channel => {
return ( return (
'\r\n' + '\r\n' +
el('channel', { id: channel.id }, [ el('channel', { id: channel.id }, [
el('display-name', {}, [escapeString(channel.name)]), el('display-name', {}, [escapeString(channel.name)]),
el('icon', { src: channel.logo }), el('icon', { src: channel.logo }),
el('url', {}, [channel.url]) el('url', {}, [channel.url])
]) ])
) )
}), }),
...programs.map(program => { ...programs.map(program => {
return ( return (
'\r\n' + '\r\n' +
el( el(
'programme', 'programme',
{ {
start: formatDate(program.start, 'YYYYMMDDHHmmss ZZ'), start: formatDate(program.start, 'YYYYMMDDHHmmss ZZ'),
stop: formatDate(program.stop, 'YYYYMMDDHHmmss ZZ'), stop: formatDate(program.stop, 'YYYYMMDDHHmmss ZZ'),
channel: program.channel channel: program.channel
}, },
[ [
...program.titles.map(title => ...program.titles.map(title =>
el('title', { lang: title.lang }, [escapeString(title.value)]) el('title', { lang: title.lang }, [escapeString(title.value)])
), ),
...program.sub_titles.map(sub_title => ...program.sub_titles.map(sub_title =>
el('sub-title', { lang: sub_title.lang }, [escapeString(sub_title.value)]) el('sub-title', { lang: sub_title.lang }, [escapeString(sub_title.value)])
), ),
...program.descriptions.map(desc => ...program.descriptions.map(desc =>
el('desc', { lang: desc.lang }, [escapeString(desc.value)]) el('desc', { lang: desc.lang }, [escapeString(desc.value)])
), ),
el('credits', {}, [ el('credits', {}, [
...program.directors.map(data => createCastMember('director', data)), ...program.directors.map(data => createCastMember('director', data)),
...program.actors.map(data => createCastMember('actor', data)), ...program.actors.map(data => createCastMember('actor', data)),
...program.writers.map(data => createCastMember('writer', data)), ...program.writers.map(data => createCastMember('writer', data)),
...program.adapters.map(data => createCastMember('adapter', data)), ...program.adapters.map(data => createCastMember('adapter', data)),
...program.producers.map(data => createCastMember('producer', data)), ...program.producers.map(data => createCastMember('producer', data)),
...program.composers.map(data => createCastMember('composer', data)), ...program.composers.map(data => createCastMember('composer', data)),
...program.editors.map(data => createCastMember('editor', data)), ...program.editors.map(data => createCastMember('editor', data)),
...program.presenters.map(data => createCastMember('presenter', data)), ...program.presenters.map(data => createCastMember('presenter', data)),
...program.commentators.map(data => createCastMember('commentator', data)), ...program.commentators.map(data => createCastMember('commentator', data)),
...program.guests.map(data => createCastMember('guest', data)) ...program.guests.map(data => createCastMember('guest', data))
]), ]),
el('date', {}, [formatDate(program.date, 'YYYYMMDD')]), el('date', {}, [formatDate(program.date, 'YYYYMMDD')]),
...program.categories.map(category => ...program.categories.map(category =>
el('category', { lang: category.lang }, [escapeString(category.value)]) el('category', { lang: category.lang }, [escapeString(category.value)])
), ),
el('icon', { src: program.icon.src }), el('icon', { src: program.icon.src }),
...program.urls.map(createURL), ...program.urls.map(createURL),
...program.episodeNumbers.map(episode => ...program.episodeNumbers.map(episode =>
el('episode-num', { system: episode.system }, [episode.value]) el('episode-num', { system: episode.system }, [episode.value])
), ),
...program.ratings.map(rating => ...program.ratings.map(rating =>
el('rating', { system: rating.system }, [ el('rating', { system: rating.system }, [
el('value', {}, [rating.value]), el('value', {}, [escapeString(rating.value)]),
el('icon', { src: rating.icon }) el('icon', { src: rating.icon })
]) ])
) )
] ]
) )
) )
}), }),
'\r\n' '\r\n'
]) ])
} }
function createCastMember(position, data) { function createCastMember(position, data) {
return el(position, {}, [ return el(position, {}, [
escapeString(data.value), escapeString(data.value),
...data.url.map(createURL), ...data.url.map(createURL),
...data.image.map(createImage) ...data.image.map(createImage)
]) ])
} }
function createImage(image) { function createImage(image) {
return el( return el(
'image', 'image',
{ {
type: image.type, type: image.type,
size: image.size, size: image.size,
orient: image.orient, orient: image.orient,
system: image.system system: image.system
}, },
[image.value] [image.value]
) )
} }
function createURL(url) { function createURL(url) {
return el('url', { system: url.system }, [url.value]) return el('url', { system: url.system }, [url.value])
} }
function createElement(name, attrs = {}, children = []) { function createElement(name, attrs = {}, children = []) {
return toString({ name, attrs, children }) return toString({ name, attrs, children })
} }
function toString(elem) { function toString(elem) {
if (typeof elem === 'string' || typeof elem === 'number') return elem if (typeof elem === 'string' || typeof elem === 'number') return elem
let attrs = '' let attrs = ''
for (let key in elem.attrs) { for (let key in elem.attrs) {
let value = elem.attrs[key] let value = elem.attrs[key]
if (value) { if (value) {
attrs += ` ${key}="${escapeString(value)}"` attrs += ` ${key}="${escapeString(value)}"`
} }
} }
if (elem.children.filter(Boolean).length) { if (elem.children.filter(Boolean).length) {
let children = '' let children = ''
elem.children.forEach(childElem => { elem.children.forEach(childElem => {
children += toString(childElem) children += toString(childElem)
}) })
return `<${elem.name}${attrs}>${children}</${elem.name}>` return `<${elem.name}${attrs}>${children}</${elem.name}>`
} }
if (!attrs) return '' if (!attrs) return ''
if (!['icon'].includes(elem.name)) return '' if (!['icon'].includes(elem.name)) return ''
return `<${elem.name}${attrs}/>` return `<${elem.name}${attrs}/>`
} }