Merge pull request #8 from mcastellaneta/master

Add support for sub_title, url and credits
This commit is contained in:
Aleksandr Statciuk 2022-05-06 13:47:51 +03:00 committed by GitHub
commit ebf00ca590
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 266 additions and 8 deletions

View File

@ -150,7 +150,9 @@ utils.escapeString = function (string, defaultValue = '') {
}
utils.convertToXMLTV = function ({ channels, programs }) {
let output = `<?xml version="1.0" encoding="UTF-8" ?><tv>\r\n`
let output = `<?xml version="1.0" encoding="UTF-8" ?><tv date="${dayjs
.utc()
.format('YYYYMMDD')}">\r\n`
for (let channel of channels) {
const id = utils.escapeString(channel['xmltv_id'])
const displayName = utils.escapeString(channel.name)
@ -178,11 +180,30 @@ utils.convertToXMLTV = function ({ channels, programs }) {
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 = utils.escapeString(program.icon)
const sub_title = 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>`
}
@ -195,6 +216,10 @@ utils.convertToXMLTV = function ({ channels, programs }) {
})
}
if (url) {
output += url
}
if (xmltv_ns) {
output += `<episode-num system="xmltv_ns">${xmltv_ns}</episode-num>`
}
@ -202,11 +227,18 @@ utils.convertToXMLTV = function ({ channels, programs }) {
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'
}
}
@ -230,6 +262,105 @@ utils.convertToXMLTV = function ({ channels, programs }) {
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 }
}
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>`
}
}
}
return output
}
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 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
}
return output
}
@ -348,11 +479,24 @@ utils.parsePrograms = async function (data, config) {
category: program.category || null,
season: program.season || null,
episode: program.episode || null,
sub_title: program.sub_title || null,
url: program.url || null,
icon: program.icon || null,
channel: channel.xmltv_id,
lang: program.lang || channel.lang || config.lang || 'en',
start: program.start ? dayjs(program.start).unix() : null,
stop: program.stop ? dayjs(program.stop).unix() : null
stop: program.stop ? dayjs(program.stop).unix() : null,
date: program.date || null,
director: program.director || null,
actor: program.actor || null,
writer: program.writer || null,
adapter: program.adapter || null,
producer: program.producer || null,
composer: program.composer || null,
editor: program.editor || null,
presenter: program.presenter || null,
commentator: program.commentator || null,
guest: program.guest || null
}
})
}

View File

@ -4,6 +4,8 @@ import axios from 'axios'
import path from 'path'
import fs from 'fs'
jest.useFakeTimers('modern').setSystemTime(new Date('2022-05-05'))
it('can load valid config.js', () => {
const config = utils.loadConfig(require(path.resolve('./tests/input/example.com.config.js')))
expect(config).toMatchObject({
@ -56,7 +58,9 @@ it('can convert object to xmltv string', () => {
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,
category: 'Test',
@ -64,12 +68,19 @@ it('can convert object to xmltv string', () => {
episode: 239,
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
channel: '1TV.com',
lang: 'it'
lang: 'it',
date: '20220505',
director: {
value: 'Director 1',
url: { value: 'http://example.com/director1.html', system: 'TestSystem' }
},
actor: ['Actor 1', 'Actor 2'],
writer: 'Writer 1'
}
]
const output = utils.convertToXMLTV({ channels, programs })
expect(output).toBe(
'<?xml version="1.0" encoding="UTF-8" ?><tv>\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><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><episode-num system="xmltv_ns">8.238.0/1</episode-num><episode-num system="onscreen">S09E239</episode-num><icon src="https://example.com/images/Program1.png?x=шеллы&amp;sid=777"/></programme>\r\n</tv>'
'<?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=шеллы&amp;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>'
)
})
@ -91,7 +102,7 @@ it('can convert object to xmltv string without season number', () => {
]
const output = utils.convertToXMLTV({ channels, programs })
expect(output).toBe(
'<?xml version="1.0" encoding="UTF-8" ?><tv>\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><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><episode-num system="xmltv_ns">0.238.0/1</episode-num><episode-num system="onscreen">S01E239</episode-num><icon src="https://example.com/images/Program1.png?x=шеллы&amp;sid=777"/></programme>\r\n</tv>'
'<?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><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><episode-num system="xmltv_ns">0.238.0/1</episode-num><episode-num system="onscreen">S01E239</episode-num><icon src="https://example.com/images/Program1.png?x=шеллы&amp;sid=777"/></programme>\r\n</tv>'
)
})
@ -113,7 +124,7 @@ it('can convert object to xmltv string without episode number', () => {
]
const output = utils.convertToXMLTV({ channels, programs })
expect(output).toBe(
'<?xml version="1.0" encoding="UTF-8" ?><tv>\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><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><icon src="https://example.com/images/Program1.png?x=шеллы&amp;sid=777"/></programme>\r\n</tv>'
'<?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><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><icon src="https://example.com/images/Program1.png?x=шеллы&amp;sid=777"/></programme>\r\n</tv>'
)
})
@ -140,7 +151,7 @@ it('can convert object to xmltv string without categories', () => {
const config = { site: 'example.com' }
const output = utils.convertToXMLTV({ config, channels, programs })
expect(output).toBe(
'<?xml version="1.0" encoding="UTF-8" ?><tv>\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<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title></programme>\r\n</tv>'
'<?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<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title></programme>\r\n</tv>'
)
})
@ -161,7 +172,110 @@ it('can convert object to xmltv string with multiple categories', () => {
]
const output = utils.convertToXMLTV({ channels, programs })
expect(output).toBe(
'<?xml version="1.0" encoding="UTF-8" ?><tv>\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><desc lang="it">Description for Program 1</desc><category lang="it">Test1</category><category lang="it">Test2</category><icon src="https://example.com/images/Program1.png?x=шеллы&amp;sid=777"/></programme>\r\n</tv>'
'<?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><desc lang="it">Description for Program 1</desc><category lang="it">Test1</category><category lang="it">Test2</category><icon src="https://example.com/images/Program1.png?x=шеллы&amp;sid=777"/></programme>\r\n</tv>'
)
})
it('can convert object to xmltv string with multiple urls', () => {
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
const { channels } = utils.parseChannels(file)
const programs = [
{
title: 'Program 1',
description: 'Description for Program 1',
start: 1616133600,
stop: 1616135400,
category: ['Test1', 'Test2'],
url: [
'https://example.com/noattr.html',
{ value: 'https://example.com/attr.html', system: 'TestSystem' }
],
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
channel: '1TV.com',
lang: 'it'
}
]
const output = utils.convertToXMLTV({ 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><desc lang="it">Description for Program 1</desc><category lang="it">Test1</category><category lang="it">Test2</category><url>https://example.com/noattr.html</url><url system="TestSystem">https://example.com/attr.html</url><icon src="https://example.com/images/Program1.png?x=шеллы&amp;sid=777"/></programme>\r\n</tv>'
)
})
it('can convert object to xmltv string with multiple images', () => {
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
const { channels } = utils.parseChannels(file)
const programs = [
{
title: 'Program 1',
description: 'Description for Program 1',
start: 1616133600,
stop: 1616135400,
category: ['Test1', 'Test2'],
url: [
'https://example.com/noattr.html',
{ value: 'https://example.com/attr.html', system: 'TestSystem' }
],
actor: {
value: 'Actor 1',
image: [
'https://example.com/image1.jpg',
{
value: 'https://example.com/image2.jpg',
type: 'person',
size: '2',
system: 'TestSystem',
orient: 'P'
}
]
},
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
channel: '1TV.com',
lang: 'it'
}
]
const output = utils.convertToXMLTV({ 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><desc lang="it">Description for Program 1</desc><category lang="it">Test1</category><category lang="it">Test2</category><url>https://example.com/noattr.html</url><url system="TestSystem">https://example.com/attr.html</url><icon src="https://example.com/images/Program1.png?x=шеллы&amp;sid=777"/><credits><actor>Actor 1<image>https://example.com/image1.jpg</image><image type="person" size="2" orient="P" system="TestSystem">https://example.com/image2.jpg</image></actor></credits></programme>\r\n</tv>'
)
})
it('can convert object to xmltv string with multiple credits member', () => {
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
const { channels } = utils.parseChannels(file)
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,
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' }
},
actor: {
value: 'Actor 1',
role: 'Manny',
guest: 'yes',
url: { value: 'http://example.com/actor1.html', system: 'TestSystem' }
},
writer: [
{ value: 'Writer 1', url: { value: 'http://example.com/w1.html', system: 'TestSystem' } },
{ value: 'Writer 2', url: { value: 'http://example.com/w2.html', system: 'TestSystem' } }
]
}
]
const output = utils.convertToXMLTV({ 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=шеллы&amp;sid=777"/><credits><director>Director 1<url system="TestSystem">http://example.com/director1.html</url></director><actor role="Manny" guest="yes">Actor 1<url system="TestSystem">http://example.com/actor1.html</url></actor><writer>Writer 1<url system="TestSystem">http://example.com/w1.html</url></writer><writer>Writer 2<url system="TestSystem">http://example.com/w2.html</url></writer></credits></programme>\r\n</tv>'
)
})