2022-06-11 20:01:39 +02:00
|
|
|
const { padStart } = require('lodash')
|
|
|
|
const { escapeString, getUTCDate } = require('./utils')
|
|
|
|
const dayjs = require('dayjs')
|
|
|
|
const utc = require('dayjs/plugin/utc')
|
|
|
|
|
|
|
|
dayjs.extend(utc)
|
|
|
|
|
|
|
|
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
|
|
|
const el = createElement
|
|
|
|
|
|
|
|
function createElements(channels, programs, date) {
|
|
|
|
date = formatDate(date, 'YYYYMMDD')
|
2022-06-13 16:23:00 +02:00
|
|
|
return el('tv', { date }, [
|
|
|
|
...channels.map(channel => {
|
|
|
|
const url = channel.site ? `https://${channel.site}` : ''
|
|
|
|
|
|
|
|
return (
|
|
|
|
'\r\n' +
|
|
|
|
el('channel', { id: channel.xmltv_id }, [
|
|
|
|
el('display-name', {}, [escapeString(channel.name)]),
|
2022-06-13 16:07:28 +02:00
|
|
|
el('icon', { src: channel.logo }),
|
|
|
|
el('url', {}, [url])
|
|
|
|
])
|
2022-06-13 16:23:00 +02:00
|
|
|
)
|
|
|
|
}),
|
|
|
|
...programs.map(program => {
|
2022-06-13 17:18:03 +02:00
|
|
|
const programDate = program.date ? formatDate(program.date, 'YYYYMMDD') : ''
|
2022-06-13 16:23:00 +02:00
|
|
|
|
|
|
|
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', {}, [
|
|
|
|
...toArray(program.director).map(data => createCastMember('director', data)),
|
|
|
|
...toArray(program.actor).map(data => createCastMember('actor', data)),
|
|
|
|
...toArray(program.writer).map(data => createCastMember('writer', data)),
|
|
|
|
...toArray(program.adapter).map(data => createCastMember('adapter', data)),
|
|
|
|
...toArray(program.producer).map(data => createCastMember('producer', data)),
|
|
|
|
...toArray(program.composer).map(data => createCastMember('composer', data)),
|
|
|
|
...toArray(program.editor).map(data => createCastMember('editor', data)),
|
|
|
|
...toArray(program.presenter).map(data => createCastMember('presenter', data)),
|
|
|
|
...toArray(program.commentator).map(data => createCastMember('commentator', data)),
|
|
|
|
...toArray(program.guest).map(data => createCastMember('guest', data))
|
|
|
|
]),
|
2022-06-13 17:18:03 +02:00
|
|
|
el('date', {}, [programDate]),
|
2022-06-13 16:23:00 +02:00
|
|
|
...toArray(program.category).map(category =>
|
2022-06-15 16:20:59 +02:00
|
|
|
el('category', {}, [escapeString(category)])
|
2022-06-13 16:23:00 +02:00
|
|
|
),
|
2022-06-13 17:18:03 +02:00
|
|
|
el('icon', { src: program.icon }),
|
|
|
|
...toArray(program.url).map(createURL),
|
|
|
|
el('episode-num', { system: 'xmltv_ns' }, [
|
|
|
|
formatEpisodeNum(program.season, program.episode, 'xmltv_ns')
|
|
|
|
]),
|
|
|
|
el('episode-num', { system: 'onscreen' }, [
|
|
|
|
formatEpisodeNum(program.season, program.episode, 'onscreen')
|
|
|
|
]),
|
2022-06-13 16:07:28 +02:00
|
|
|
...toArray(program.rating).map(rating =>
|
|
|
|
el('rating', { system: rating.system }, [
|
|
|
|
el('value', {}, [rating.value]),
|
|
|
|
el('icon', { src: rating.icon })
|
|
|
|
])
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2022-06-13 16:23:00 +02:00
|
|
|
)
|
|
|
|
})
|
|
|
|
])
|
2022-06-13 16:07:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function formatEpisodeNum(s, e, system) {
|
|
|
|
switch (system) {
|
|
|
|
case 'xmltv_ns':
|
|
|
|
return createXMLTVNS(s, e)
|
|
|
|
case 'onscreen':
|
|
|
|
return createOnScreen(s, e)
|
|
|
|
}
|
|
|
|
|
2022-06-13 17:18:03 +02:00
|
|
|
return ''
|
2022-06-13 16:07:28 +02:00
|
|
|
}
|
|
|
|
|
2022-06-11 20:01:39 +02:00
|
|
|
function createXMLTVNS(s, e) {
|
2022-06-13 17:18:03 +02:00
|
|
|
if (!e) return ''
|
2022-06-11 20:01:39 +02:00
|
|
|
s = s || 1
|
|
|
|
|
|
|
|
return `${s - 1}.${e - 1}.0/1`
|
|
|
|
}
|
|
|
|
|
|
|
|
function createOnScreen(s, e) {
|
2022-06-13 17:18:03 +02:00
|
|
|
if (!e) return ''
|
2022-06-11 20:01:39 +02:00
|
|
|
s = s || 1
|
|
|
|
|
|
|
|
s = padStart(s, 2, '0')
|
|
|
|
e = padStart(e, 2, '0')
|
|
|
|
|
|
|
|
return `S${s}E${e}`
|
|
|
|
}
|
|
|
|
|
2022-06-13 16:07:28 +02:00
|
|
|
function createCastMember(position, data) {
|
|
|
|
data = toObject(data)
|
|
|
|
return el(position, {}, [
|
2022-06-13 16:23:00 +02:00
|
|
|
escapeString(data.value),
|
2022-06-13 16:07:28 +02:00
|
|
|
...toArray(data.url).map(createURL),
|
|
|
|
...toArray(data.image).map(createImage)
|
|
|
|
])
|
|
|
|
}
|
2022-06-11 20:01:39 +02:00
|
|
|
|
2022-06-13 16:07:28 +02:00
|
|
|
function createImage(image) {
|
|
|
|
image = toObject(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) {
|
|
|
|
url = toObject(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 toObject(value) {
|
|
|
|
if (typeof value === 'string') return { value }
|
2022-06-11 20:01:39 +02:00
|
|
|
|
2022-06-13 16:07:28 +02:00
|
|
|
return value
|
|
|
|
}
|
2022-06-11 20:01:39 +02:00
|
|
|
|
2022-06-13 16:07:28 +02:00
|
|
|
function toArray(value) {
|
|
|
|
if (Array.isArray(value)) return value.filter(Boolean)
|
2022-06-11 20:01:39 +02:00
|
|
|
|
2022-06-13 16:07:28 +02:00
|
|
|
return [value].filter(Boolean)
|
|
|
|
}
|
2022-06-11 20:01:39 +02:00
|
|
|
|
2022-06-13 16:07:28 +02:00
|
|
|
function formatDate(date, format) {
|
|
|
|
return date ? dayjs.utc(date).format(format) : null
|
|
|
|
}
|
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-13 16:23:00 +02:00
|
|
|
if (typeof elem === 'string') 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
|
|
|
}
|