Skip to content

Commit

Permalink
Merge pull request #8 from mcastellaneta/master
Browse files Browse the repository at this point in the history
Add support for sub_title, url and credits
  • Loading branch information
Aleksandr Statciuk authored May 6, 2022
2 parents 16d02ce + 04b935f commit ebf00ca
Show file tree
Hide file tree
Showing 2 changed files with 266 additions and 8 deletions.
148 changes: 146 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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>`
}
Expand All @@ -195,18 +216,29 @@ utils.convertToXMLTV = function ({ channels, programs }) {
})
}

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'
}
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
})
}
Expand Down
Loading

0 comments on commit ebf00ca

Please sign in to comment.