2022-06-16 14:07:56 +02:00
|
|
|
const { padStart } = require('lodash')
|
|
|
|
const { toArray, toUnix, parseNumber } = require('./utils')
|
2022-07-01 01:45:59 +02:00
|
|
|
const Channel = require('./Channel')
|
2022-06-16 14:07:56 +02:00
|
|
|
|
|
|
|
class Program {
|
2022-07-01 01:45:59 +02:00
|
|
|
constructor(p, c) {
|
|
|
|
if (!(c instanceof Channel)) {
|
|
|
|
throw new Error('The second argument in the constructor must be the "Channel" class')
|
|
|
|
}
|
|
|
|
|
2022-06-16 14:07:56 +02:00
|
|
|
const data = {
|
2022-11-20 14:15:31 +01:00
|
|
|
site: c.site || '',
|
|
|
|
channel: c.id || '',
|
2022-07-01 01:45:59 +02:00
|
|
|
titles: toArray(p.titles || p.title).map(text => toTextObject(text, c.lang)),
|
|
|
|
sub_titles: toArray(p.sub_titles || p.sub_title).map(text => toTextObject(text, c.lang)),
|
|
|
|
descriptions: toArray(p.descriptions || p.description || p.desc).map(text =>
|
|
|
|
toTextObject(text, c.lang)
|
|
|
|
),
|
2022-06-16 14:07:56 +02:00
|
|
|
icon: toIconObject(p.icon),
|
2022-11-20 13:19:49 +01:00
|
|
|
episodeNumbers: p.episodeNum || p.episodeNumbers || getEpisodeNumbers(p.season, p.episode),
|
2022-06-16 14:07:56 +02:00
|
|
|
date: p.date ? toUnix(p.date) : null,
|
2022-06-16 20:38:26 +02:00
|
|
|
start: p.start ? toUnix(p.start) : null,
|
|
|
|
stop: p.stop ? toUnix(p.stop) : null,
|
2022-06-16 18:11:11 +02:00
|
|
|
urls: toArray(p.urls || p.url).map(toUrlObject),
|
2022-06-16 14:07:56 +02:00
|
|
|
ratings: toArray(p.ratings || p.rating).map(toRatingObject),
|
2022-07-01 01:45:59 +02:00
|
|
|
categories: toArray(p.categories || p.category).map(text => toTextObject(text, c.lang)),
|
2022-06-16 14:07:56 +02:00
|
|
|
directors: toArray(p.directors || p.director).map(toPersonObject),
|
|
|
|
actors: toArray(p.actors || p.actor).map(toPersonObject),
|
|
|
|
writers: toArray(p.writers || p.writer).map(toPersonObject),
|
|
|
|
adapters: toArray(p.adapters || p.adapter).map(toPersonObject),
|
|
|
|
producers: toArray(p.producers || p.producer).map(toPersonObject),
|
|
|
|
composers: toArray(p.composers || p.composer).map(toPersonObject),
|
|
|
|
editors: toArray(p.editors || p.editor).map(toPersonObject),
|
|
|
|
presenters: toArray(p.presenters || p.presenter).map(toPersonObject),
|
|
|
|
commentators: toArray(p.commentators || p.commentator).map(toPersonObject),
|
|
|
|
guests: toArray(p.guests || p.guest).map(toPersonObject)
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let key in data) {
|
|
|
|
this[key] = data[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Program
|
|
|
|
|
2022-07-01 01:45:59 +02:00
|
|
|
function toTextObject(text, lang) {
|
|
|
|
if (typeof text === 'string') {
|
|
|
|
return { value: text, lang }
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
value: text.value,
|
|
|
|
lang: text.lang
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 14:07:56 +02:00
|
|
|
function toPersonObject(person) {
|
|
|
|
if (typeof person === 'string') {
|
|
|
|
return {
|
|
|
|
value: person,
|
|
|
|
url: [],
|
|
|
|
image: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
value: person.value,
|
|
|
|
url: toArray(person.url).map(toUrlObject),
|
|
|
|
image: toArray(person.image).map(toImageObject)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function toImageObject(image) {
|
|
|
|
if (typeof image === 'string') return { type: '', size: '', orient: '', system: '', value: image }
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: image.type || '',
|
|
|
|
size: image.size || '',
|
|
|
|
orient: image.orient || '',
|
|
|
|
system: image.system || '',
|
|
|
|
value: image.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function toRatingObject(rating) {
|
|
|
|
if (typeof rating === 'string') return { system: '', icon: '', value: rating }
|
|
|
|
|
|
|
|
return {
|
|
|
|
system: rating.system || '',
|
|
|
|
icon: rating.icon || '',
|
|
|
|
value: rating.value || ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function toUrlObject(url) {
|
|
|
|
if (typeof url === 'string') return { system: '', value: url }
|
|
|
|
|
|
|
|
return {
|
|
|
|
system: url.system || '',
|
|
|
|
value: url.value || ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function toIconObject(icon) {
|
2022-06-16 20:38:26 +02:00
|
|
|
if (!icon) return { src: '' }
|
|
|
|
if (typeof icon === 'string') return { src: icon }
|
2022-06-16 14:07:56 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
src: icon.src || ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEpisodeNumbers(s, e) {
|
|
|
|
s = parseNumber(s)
|
|
|
|
e = parseNumber(e)
|
|
|
|
|
|
|
|
return [createXMLTVNS(s, e), createOnScreen(s, e)].filter(Boolean)
|
|
|
|
}
|
|
|
|
|
|
|
|
function createXMLTVNS(s, e) {
|
|
|
|
if (!e) return null
|
|
|
|
s = s || 1
|
|
|
|
|
|
|
|
return {
|
|
|
|
system: 'xmltv_ns',
|
|
|
|
value: `${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 {
|
|
|
|
system: 'onscreen',
|
|
|
|
value: `S${s}E${e}`
|
|
|
|
}
|
|
|
|
}
|