epg-grabber/src/xmltv.js

193 lines
4.8 KiB
JavaScript
Raw Normal View History

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-11 20:01:39 +02:00
2022-06-13 16:23:00 +02:00
output += createElements(channels, programs, date)
// console.log(JSON.stringify(elements, null, 2))
2022-06-11 20:01:39 +02:00
2022-06-13 16:23:00 +02:00
// elements.forEach(elem => {
// output += toString(elem)
// })
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 => {
const lang = program.lang || 'en'
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-13 16:23:00 +02:00
el('title', { lang }, [escapeString(program.title)]),
el('sub-title', { lang }, [escapeString(program.sub_title)]),
el('desc', { lang }, [escapeString(program.description)]),
2022-06-13 16:07:28 +02:00
el('date', {}, [date]),
el('icon', { src: program.icon }),
el('url', {}, [program.url]),
el('episode-num', { system: 'xmltv_ns' }, [
formatEpisodeNum(program.season, program.episode, 'xmltv_ns')
]),
el('episode-num', { system: 'onscreen' }, [
formatEpisodeNum(program.season, program.episode, 'onscreen')
]),
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 16:23:00 +02:00
...toArray(program.category).map(category =>
el('category', { lang }, [escapeString(category)])
),
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)
}
return null
}
2022-06-11 20:01:39 +02:00
function createXMLTVNS(s, e) {
if (!e) return null
s = s || 1
return `${s - 1}.${e - 1}.0/1`
}
function createOnScreen(s, e) {
if (!e) return null
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 16:07:28 +02:00
if (elem.children.length) {
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 ''
return `<${elem.name}${attrs}/>`
2022-06-11 20:01:39 +02:00
}