wip
This commit is contained in:
parent
4d6f4fb435
commit
99b96a7d19
327
src/xmltv.js
327
src/xmltv.js
|
@ -8,105 +8,91 @@ dayjs.extend(utc)
|
|||
module.exports.generate = generate
|
||||
|
||||
function generate({ channels, programs, date = getUTCDate() }) {
|
||||
let output = `<?xml version="1.0" encoding="UTF-8" ?><tv date="${dayjs(date).format(
|
||||
'YYYYMMDD'
|
||||
)}">\r\n`
|
||||
let output = `<?xml version="1.0" encoding="UTF-8" ?>`
|
||||
|
||||
for (let channel of channels) {
|
||||
const id = escapeString(channel['xmltv_id'])
|
||||
const displayName = escapeString(channel.name)
|
||||
output += `<channel id="${id}"><display-name>${displayName}</display-name>`
|
||||
if (channel.logo) {
|
||||
const logo = escapeString(channel.logo)
|
||||
output += `<icon src="${logo}"/>`
|
||||
}
|
||||
if (channel.site) {
|
||||
const url = channel.site ? 'https://' + channel.site : null
|
||||
output += `<url>${url}</url>`
|
||||
}
|
||||
output += `</channel>\r\n`
|
||||
}
|
||||
const elements = createElements(channels, programs, date)
|
||||
console.log(JSON.stringify(elements, null, 2))
|
||||
|
||||
for (let program of programs) {
|
||||
if (!program) continue
|
||||
|
||||
const channel = escapeString(program.channel)
|
||||
const title = escapeString(program.title)
|
||||
const description = escapeString(program.description)
|
||||
const categories = Array.isArray(program.category) ? program.category : [program.category]
|
||||
const start = program.start ? dayjs.unix(program.start).utc().format('YYYYMMDDHHmmss ZZ') : ''
|
||||
const stop = program.stop ? dayjs.unix(program.stop).utc().format('YYYYMMDDHHmmss ZZ') : ''
|
||||
const lang = program.lang || 'en'
|
||||
const xmltv_ns = createXMLTVNS(program.season, program.episode)
|
||||
const onscreen = createOnScreen(program.season, program.episode)
|
||||
const date = program.date || ''
|
||||
const credits = createCredits({
|
||||
director: program.director,
|
||||
actor: program.actor,
|
||||
writer: program.writer,
|
||||
adapter: program.adapter,
|
||||
producer: program.producer,
|
||||
composer: program.composer,
|
||||
editor: program.editor,
|
||||
presenter: program.presenter,
|
||||
commentator: program.commentator,
|
||||
guest: program.guest
|
||||
})
|
||||
const icon = escapeString(program.icon)
|
||||
const sub_title = escapeString(program.sub_title)
|
||||
const url = program.url ? createURL(program.url, channel) : ''
|
||||
|
||||
if (start && stop && title) {
|
||||
output += `<programme start="${start}" stop="${stop}" channel="${channel}"><title lang="${lang}">${title}</title>`
|
||||
|
||||
if (sub_title) {
|
||||
output += `<sub-title>${sub_title}</sub-title>`
|
||||
}
|
||||
|
||||
if (description) {
|
||||
output += `<desc lang="${lang}">${description}</desc>`
|
||||
}
|
||||
|
||||
if (categories.length) {
|
||||
categories.forEach(category => {
|
||||
if (category) {
|
||||
output += `<category lang="${lang}">${escapeString(category)}</category>`
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (url) {
|
||||
output += url
|
||||
}
|
||||
|
||||
if (xmltv_ns) {
|
||||
output += `<episode-num system="xmltv_ns">${xmltv_ns}</episode-num>`
|
||||
}
|
||||
|
||||
if (onscreen) {
|
||||
output += `<episode-num system="onscreen">${onscreen}</episode-num>`
|
||||
}
|
||||
if (date) {
|
||||
output += `<date>${date}</date>`
|
||||
}
|
||||
|
||||
if (icon) {
|
||||
output += `<icon src="${icon}"/>`
|
||||
}
|
||||
|
||||
if (credits) {
|
||||
output += `<credits>${credits}</credits>`
|
||||
}
|
||||
|
||||
output += '</programme>\r\n'
|
||||
}
|
||||
}
|
||||
|
||||
output += '</tv>'
|
||||
elements.forEach(elem => {
|
||||
output += toString(elem)
|
||||
})
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
const el = createElement
|
||||
|
||||
function createElements(channels, programs, date) {
|
||||
date = formatDate(date, 'YYYYMMDD')
|
||||
return [
|
||||
el('tv', { date }, [
|
||||
...channels.map(channel => {
|
||||
const url = channel.site ? `https://${channel.site}` : ''
|
||||
return el('channel', { id: channel.xmltv_id }, [
|
||||
el('display-name', {}, [channel.name]),
|
||||
el('icon', { src: channel.logo }),
|
||||
el('url', {}, [url])
|
||||
])
|
||||
}),
|
||||
...programs.map(program => {
|
||||
const lang = program.lang || 'en'
|
||||
return el(
|
||||
'programme',
|
||||
{
|
||||
start: formatDate(program.start, 'YYYYMMDDHHmmss ZZ'),
|
||||
stop: formatDate(program.stop, 'YYYYMMDDHHmmss ZZ'),
|
||||
channel: program.channel
|
||||
},
|
||||
[
|
||||
el('title', { lang }, [program.title]),
|
||||
el('sub-title', { lang }, [program.sub_title]),
|
||||
el('desc', { lang }, [program.description]),
|
||||
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))
|
||||
]),
|
||||
...toArray(program.category).map(category => el('category', { lang }, [category])),
|
||||
...toArray(program.rating).map(rating =>
|
||||
el('rating', { system: rating.system }, [
|
||||
el('value', {}, [rating.value]),
|
||||
el('icon', { src: rating.icon })
|
||||
])
|
||||
)
|
||||
]
|
||||
)
|
||||
})
|
||||
])
|
||||
]
|
||||
}
|
||||
|
||||
function formatEpisodeNum(s, e, system) {
|
||||
switch (system) {
|
||||
case 'xmltv_ns':
|
||||
return createXMLTVNS(s, e)
|
||||
case 'onscreen':
|
||||
return createOnScreen(s, e)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
function createXMLTVNS(s, e) {
|
||||
if (!e) return null
|
||||
s = s || 1
|
||||
|
@ -124,102 +110,75 @@ function createOnScreen(s, e) {
|
|||
return `S${s}E${e}`
|
||||
}
|
||||
|
||||
function createURL(urlObj, channel = '') {
|
||||
const urls = Array.isArray(urlObj) ? urlObj : [urlObj]
|
||||
let output = ''
|
||||
for (let url of urls) {
|
||||
if (typeof url === 'string' || url instanceof String) {
|
||||
url = { value: url }
|
||||
}
|
||||
function createCastMember(position, data) {
|
||||
data = toObject(data)
|
||||
return el(position, {}, [
|
||||
data.value,
|
||||
...toArray(data.url).map(createURL),
|
||||
...toArray(data.image).map(createImage)
|
||||
])
|
||||
}
|
||||
|
||||
let attr = url.system ? ` system="${url.system}"` : ''
|
||||
if (url.value.includes('http')) {
|
||||
output += `<url${attr}>${url.value}</url>`
|
||||
} else if (channel) {
|
||||
let chan = channels.find(c => c.xmltv_id.localeCompare(channel) === 0)
|
||||
if (chan && chan.site) {
|
||||
output += `<url${attr}>https://${chan.site}${url.value}</url>`
|
||||
}
|
||||
function createImage(image) {
|
||||
image = toObject(image)
|
||||
return el(
|
||||
'image',
|
||||
{
|
||||
type: image.type,
|
||||
size: image.size,
|
||||
orient: image.orient,
|
||||
system: image.system
|
||||
},
|
||||
[image.value]
|
||||
)
|
||||
}
|
||||
|
||||
function createURL(url) {
|
||||
url = toObject(url)
|
||||
return el('url', { system: url.system }, [url.value])
|
||||
}
|
||||
|
||||
function toObject(value) {
|
||||
if (typeof value === 'string') return { value }
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
function toArray(value) {
|
||||
if (Array.isArray(value)) return value.filter(Boolean)
|
||||
|
||||
return [value].filter(Boolean)
|
||||
}
|
||||
|
||||
function formatDate(date, format) {
|
||||
return date ? dayjs.utc(date).format(format) : null
|
||||
}
|
||||
|
||||
function createElement(name, attrs = {}, children = []) {
|
||||
return { name, attrs, children }
|
||||
}
|
||||
|
||||
function toString(elem) {
|
||||
if (typeof elem === 'string') return escapeString(elem)
|
||||
|
||||
let attrs = ''
|
||||
for (let key in elem.attrs) {
|
||||
let value = elem.attrs[key]
|
||||
if (value) {
|
||||
attrs += ` ${key}="${escapeString(value)}"`
|
||||
}
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
if (elem.children.length) {
|
||||
let children = ''
|
||||
elem.children.forEach(childElem => {
|
||||
children += toString(childElem)
|
||||
})
|
||||
|
||||
function createImage(imgObj, channel = '') {
|
||||
const imgs = Array.isArray(imgObj) ? imgObj : [imgObj]
|
||||
let output = ''
|
||||
for (let img of imgs) {
|
||||
if (typeof img === 'string' || img instanceof String) {
|
||||
img = { value: img }
|
||||
}
|
||||
|
||||
const imageTypes = ['poster', 'backdrop', 'still', 'person', 'character']
|
||||
const imageSizes = ['1', '2', '3']
|
||||
const imageOrients = ['P', 'L']
|
||||
|
||||
let attr = ''
|
||||
|
||||
if (img.type && imageTypes.some(el => img.type.includes(el))) {
|
||||
attr += ` type="${img.type}"`
|
||||
}
|
||||
|
||||
if (img.size && imageSizes.some(el => img.size.includes(el))) {
|
||||
attr += ` size="${img.size}"`
|
||||
}
|
||||
|
||||
if (img.orient && imageOrients.some(el => img.orient.includes(el))) {
|
||||
attr += ` orient="${img.orient}"`
|
||||
}
|
||||
|
||||
if (img.system) {
|
||||
attr += ` system="${img.system}"`
|
||||
}
|
||||
|
||||
if (img.value.includes('http')) {
|
||||
output += `<image${attr}>${img.value}</image>`
|
||||
} else if (channel) {
|
||||
let chan = channels.find(c => c.xmltv_id.localeCompare(channel) === 0)
|
||||
if (chan && chan.site) {
|
||||
output += `<image${attr}>https://${chan.site}${img.value}</image>`
|
||||
}
|
||||
}
|
||||
return `<${elem.name}${attrs}>${children}</${elem.name}>`
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
function createCredits(obj) {
|
||||
let cast = Object.entries(obj)
|
||||
.filter(x => x[1])
|
||||
.map(([name, value]) => ({ name, value }))
|
||||
|
||||
let output = ''
|
||||
for (let type of cast) {
|
||||
const r = Array.isArray(type.value) ? type.value : [type.value]
|
||||
for (let person of r) {
|
||||
if (typeof person === 'string' || person instanceof String) {
|
||||
person = { value: person }
|
||||
}
|
||||
|
||||
let attr = ''
|
||||
if (type.name.localeCompare('actor') === 0 && type.value.role) {
|
||||
attr += ` role="${type.value.role}"`
|
||||
}
|
||||
if (type.name.localeCompare('actor') === 0 && type.value.guest) {
|
||||
attr += ` guest="${type.value.guest}"`
|
||||
}
|
||||
output += `<${type.name}${attr}>${person.value}`
|
||||
|
||||
if (person.url) {
|
||||
output += createURL(person.url)
|
||||
}
|
||||
if (person.image) {
|
||||
output += createImage(person.image)
|
||||
}
|
||||
|
||||
output += `</${type.name}>`
|
||||
}
|
||||
}
|
||||
return output
|
||||
if (!attrs) return ''
|
||||
|
||||
return `<${elem.name}${attrs}/>`
|
||||
}
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">
|
||||
<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>
|
||||
<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>
|
||||
<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><sub-title lang="it">Sub-title & 1</sub-title><desc lang="it">Description for Program 1</desc><date>20220505</date><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/><url>http://example.com/title.html</url><episode-num system="xmltv_ns">8.238.0/1</episode-num><episode-num system="onscreen">S09E239</episode-num><credits><director>Director 1<url system="TestSystem">http://example.com/director1.html</url><image>https://example.com/image1.jpg</image><image type="person" size="2" orient="P" system="TestSystem">https://example.com/image2.jpg</image></director><director>Director 2</director><actor>Actor 1</actor><actor>Actor 2</actor><writer>Writer 1</writer></credits><category lang="it">Test</category><rating system="MPAA"><value>PG</value><icon src="http://example.com/pg_symbol.png"/></rating></programme>
|
||||
</tv>
|
|
@ -1,3 +1,4 @@
|
|||
import fs from 'fs'
|
||||
import xmltv from '../src/xmltv'
|
||||
|
||||
jest.useFakeTimers('modern').setSystemTime(new Date('2022-05-05'))
|
||||
|
@ -16,34 +17,50 @@ const channels = [
|
|||
}
|
||||
]
|
||||
|
||||
it('can generate xmltv', () => {
|
||||
fit('can generate xmltv', () => {
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
sub_title: 'Sub-title & 1',
|
||||
description: 'Description for Program 1',
|
||||
url: 'http://example.com/title.html',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
start: '2021-03-19T06:00:00.000Z',
|
||||
stop: '2021-03-19T06:30:00.000Z',
|
||||
category: 'Test',
|
||||
season: 9,
|
||||
episode: 239,
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
lang: 'it',
|
||||
date: '20220505',
|
||||
director: {
|
||||
value: 'Director 1',
|
||||
url: { value: 'http://example.com/director1.html', system: 'TestSystem' }
|
||||
rating: {
|
||||
system: 'MPAA',
|
||||
value: 'PG',
|
||||
icon: 'http://example.com/pg_symbol.png'
|
||||
},
|
||||
director: [
|
||||
{
|
||||
value: 'Director 1',
|
||||
url: { value: 'http://example.com/director1.html', system: 'TestSystem' },
|
||||
image: [
|
||||
'https://example.com/image1.jpg',
|
||||
{
|
||||
value: 'https://example.com/image2.jpg',
|
||||
type: 'person',
|
||||
size: '2',
|
||||
system: 'TestSystem',
|
||||
orient: 'P'
|
||||
}
|
||||
]
|
||||
},
|
||||
'Director 2'
|
||||
],
|
||||
actor: ['Actor 1', 'Actor 2'],
|
||||
writer: 'Writer 1'
|
||||
}
|
||||
]
|
||||
const output = xmltv.generate({ channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><sub-title>Sub-title & 1</sub-title><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><url>http://example.com/title.html</url><episode-num system="xmltv_ns">8.238.0/1</episode-num><episode-num system="onscreen">S09E239</episode-num><date>20220505</date><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/><credits><director>Director 1<url system="TestSystem">http://example.com/director1.html</url></director><actor>Actor 1</actor><actor>Actor 2</actor><writer>Writer 1</writer></credits></programme>\r\n</tv>'
|
||||
)
|
||||
const expected = fs.readFileSync('./tests/expected/guide.xml', { encoding: 'utf-8' })
|
||||
expect(output).toBe(expected)
|
||||
})
|
||||
|
||||
it('can generate xmltv without season number', () => {
|
||||
|
@ -51,8 +68,8 @@ it('can generate xmltv without season number', () => {
|
|||
{
|
||||
title: 'Program 1',
|
||||
description: 'Description for Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
start: '2021-03-19T06:00:00.000Z',
|
||||
stop: '2021-03-19T06:30:00.000Z',
|
||||
category: 'Test',
|
||||
episode: 239,
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
|
@ -71,8 +88,8 @@ it('can generate xmltv without episode number', () => {
|
|||
{
|
||||
title: 'Program 1',
|
||||
description: 'Description for Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
start: '2021-03-19T06:00:00.000Z',
|
||||
stop: '2021-03-19T06:30:00.000Z',
|
||||
category: 'Test',
|
||||
season: 1,
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
|
@ -90,8 +107,8 @@ it('can generate xmltv without categories', () => {
|
|||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
start: '2021-03-19T06:00:00.000Z',
|
||||
stop: '2021-03-19T06:30:00.000Z',
|
||||
channel: '1TV.com',
|
||||
lang: 'it'
|
||||
}
|
||||
|
@ -108,8 +125,8 @@ it('can generate xmltv with multiple categories', () => {
|
|||
{
|
||||
title: 'Program 1',
|
||||
description: 'Description for Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
start: '2021-03-19T06:00:00.000Z',
|
||||
stop: '2021-03-19T06:30:00.000Z',
|
||||
category: ['Test1', 'Test2'],
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
|
@ -127,8 +144,8 @@ it('can generate xmltv with multiple urls', () => {
|
|||
{
|
||||
title: 'Program 1',
|
||||
description: 'Description for Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
start: '2021-03-19T06:00:00.000Z',
|
||||
stop: '2021-03-19T06:30:00.000Z',
|
||||
category: ['Test1', 'Test2'],
|
||||
url: [
|
||||
'https://example.com/noattr.html',
|
||||
|
@ -150,8 +167,8 @@ it('can generate xmltv with multiple images', () => {
|
|||
{
|
||||
title: 'Program 1',
|
||||
description: 'Description for Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
start: '2021-03-19T06:00:00.000Z',
|
||||
stop: '2021-03-19T06:30:00.000Z',
|
||||
category: ['Test1', 'Test2'],
|
||||
url: [
|
||||
'https://example.com/noattr.html',
|
||||
|
@ -188,15 +205,14 @@ it('can generate xmltv with multiple credits member', () => {
|
|||
sub_title: 'Sub-title 1',
|
||||
description: 'Description for Program 1',
|
||||
url: 'http://example.com/title.html',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
start: '2021-03-19T06:00:00.000Z',
|
||||
stop: '2021-03-19T06:30:00.000Z',
|
||||
category: 'Test',
|
||||
season: 9,
|
||||
episode: 239,
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
lang: 'it',
|
||||
date: '20220505',
|
||||
director: {
|
||||
value: 'Director 1',
|
||||
url: { value: 'http://example.com/director1.html', system: 'TestSystem' }
|
||||
|
|
Loading…
Reference in New Issue