Added support of lang attribute

This commit is contained in:
Aleksandr Statciuk 2022-07-01 02:45:59 +03:00
parent 953c43a4b5
commit dd54b9f0ef
6 changed files with 138 additions and 100 deletions

View File

@ -1,14 +1,21 @@
const { padStart } = require('lodash')
const { toArray, toUnix, parseNumber } = require('./utils')
const Channel = require('./Channel')
class Program {
constructor(p) {
constructor(p, c) {
if (!(c instanceof Channel)) {
throw new Error('The second argument in the constructor must be the "Channel" class')
}
const data = {
site: p.site || '',
channel: p.channel || '',
title: p.title || '',
sub_title: p.sub_title || '',
description: [p.description, p.desc].find(i => i) || '',
site: p.site || c.site || '',
channel: p.channel || c.id || '',
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)
),
icon: toIconObject(p.icon),
episodeNumbers: p.episodeNumbers || getEpisodeNumbers(p.season, p.episode),
date: p.date ? toUnix(p.date) : null,
@ -16,7 +23,7 @@ class Program {
stop: p.stop ? toUnix(p.stop) : null,
urls: toArray(p.urls || p.url).map(toUrlObject),
ratings: toArray(p.ratings || p.rating).map(toRatingObject),
categories: toArray(p.categories || p.category),
categories: toArray(p.categories || p.category).map(text => toTextObject(text, c.lang)),
directors: toArray(p.directors || p.director).map(toPersonObject),
actors: toArray(p.actors || p.actor).map(toPersonObject),
writers: toArray(p.writers || p.writer).map(toPersonObject),
@ -37,6 +44,17 @@ class Program {
module.exports = Program
function toTextObject(text, lang) {
if (typeof text === 'string') {
return { value: text, lang }
}
return {
value: text.value,
lang: text.lang
}
}
function toPersonObject(person) {
if (typeof person === 'string') {
return {

View File

@ -41,12 +41,5 @@ async function parsePrograms(data) {
throw new Error('Parser should return an array')
}
return programs
.filter(i => i)
.map(p => {
p.site = channel.site
p.channel = p.channel || channel.id
return new Program(p)
})
return programs.filter(i => i).map(p => new Program(p, channel))
}

View File

@ -47,9 +47,15 @@ function createElements(channels, programs, date) {
channel: program.channel
},
[
el('title', {}, [escapeString(program.title)]),
el('sub-title', {}, [escapeString(program.sub_title)]),
el('desc', {}, [escapeString(program.description)]),
...program.titles.map(title =>
el('title', { lang: title.lang }, [escapeString(title.value)])
),
...program.sub_titles.map(sub_title =>
el('sub-title', { lang: sub_title.lang }, [escapeString(sub_title.value)])
),
...program.descriptions.map(desc =>
el('desc', { lang: desc.lang }, [escapeString(desc.value)])
),
el('credits', {}, [
...program.directors.map(data => createCastMember('director', data)),
...program.actors.map(data => createCastMember('actor', data)),
@ -63,7 +69,9 @@ function createElements(channels, programs, date) {
...program.guests.map(data => createCastMember('guest', data))
]),
el('date', {}, [formatDate(program.date, 'YYYYMMDD')]),
...program.categories.map(category => el('category', {}, [escapeString(category)])),
...program.categories.map(category =>
el('category', { lang: category.lang }, [escapeString(category.value)])
),
el('icon', { src: program.icon.src }),
...program.urls.map(createURL),
...program.episodeNumbers.map(episode =>

View File

@ -1,12 +1,11 @@
import Channel from '../src/Channel'
import Program from '../src/Program'
const channel = new Channel({ xmltv_id: '1tv', lang: 'en', site: 'example.com' })
const channel = new Channel({ xmltv_id: '1tv', lang: 'fr', site: 'example.com' })
it('can create new Program', () => {
const program = new Program({
site: channel.site,
channel: channel.id,
const program = new Program(
{
title: 'Title',
sub_title: 'Subtitle',
description: 'Description',
@ -46,16 +45,21 @@ it('can create new Program', () => {
image: ['https://example.com/image1.jpg', 'https://example.com/image2.jpg']
}
]
})
},
channel
)
expect(program).toMatchObject({
site: 'example.com',
channel: '1tv',
title: 'Title',
sub_title: 'Subtitle',
description: 'Description',
titles: [{ value: 'Title', lang: 'fr' }],
sub_titles: [{ value: 'Subtitle', lang: 'fr' }],
descriptions: [{ value: 'Description', lang: 'fr' }],
urls: [{ system: '', value: 'http://example.com/title.html' }],
categories: ['Category1', 'Category2'],
categories: [
{ value: 'Category1', lang: 'fr' },
{ value: 'Category2', lang: 'fr' }
],
icon: { src: 'https://example.com/image.jpg' },
episodeNumbers: [
{ system: 'xmltv_ns', value: '8.237.0/1' },
@ -132,9 +136,9 @@ it('can create new Program', () => {
})
it('can create program from exist object', () => {
const program = new Program({
channel: channel.id,
title: 'Program 1',
const program = new Program(
{
titles: [{ value: 'Program 1', lang: 'de' }],
start: '2021-03-19T06:00:00.000Z',
stop: '2021-03-19T06:30:00.000Z',
ratings: {
@ -143,13 +147,15 @@ it('can create program from exist object', () => {
icon: 'http://example.com/pg_symbol.png'
},
actors: [{ value: 'Actor1', url: [], image: [] }]
})
},
channel
)
expect(program).toMatchObject({
channel: '1tv',
title: 'Program 1',
sub_title: '',
description: '',
titles: [{ value: 'Program 1', lang: 'de' }],
sub_titles: [],
descriptions: [],
urls: [],
categories: [],
icon: {},
@ -178,13 +184,15 @@ it('can create program from exist object', () => {
})
it('can create program without season number', () => {
const program = new Program({
channel: channel.id,
const program = new Program(
{
title: 'Program 1',
start: '2021-03-19T06:00:00.000Z',
stop: '2021-03-19T06:30:00.000Z',
episode: 238
})
},
channel
)
expect(program.episodeNumbers).toMatchObject([
{ system: 'xmltv_ns', value: '0.237.0/1' },
@ -193,13 +201,16 @@ it('can create program without season number', () => {
})
it('can create program without episode number', () => {
const program = new Program({
const program = new Program(
{
channel: channel.id,
title: 'Program 1',
start: '2021-03-19T06:00:00.000Z',
stop: '2021-03-19T06:30:00.000Z',
season: 3
})
},
channel
)
expect(program.episodeNumbers).toMatchObject([])
})

View File

@ -14,7 +14,7 @@ it('can parse valid channels.xml', () => {
})
it('can parse programs', done => {
const channel = { xmltv_id: '1tv' }
const channel = new Channel({ xmltv_id: '1tv' })
const config = require('./input/example.config.js')
parsePrograms({ channel, config })
@ -27,7 +27,7 @@ it('can parse programs', done => {
})
it('can parse programs async', done => {
const channel = { xmltv_id: '1tv' }
const channel = new Channel({ xmltv_id: '1tv' })
const config = require('./input/async.config.js')
parsePrograms({ channel, config })

View File

@ -14,11 +14,12 @@ const channels = [
new Channel({
xmltv_id: '2TV.co',
name: '2 TV',
site: 'example.com'
site: 'example.com',
lang: 'es'
})
]
fit('can generate xmltv', () => {
it('can generate xmltv', () => {
const programs = [
new Program(
{
@ -33,7 +34,6 @@ fit('can generate xmltv', () => {
season: 9,
episode: 239,
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
channel: '1TV.co',
rating: {
system: 'MPAA',
value: 'PG',
@ -60,12 +60,20 @@ fit('can generate xmltv', () => {
writer: 'Writer 1'
},
channels[0]
),
new Program(
{
title: 'Program 2',
start: '2021-03-19T06:00:00.000Z',
stop: '2021-03-19T06:30:00.000Z'
},
channels[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.co"><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.co"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.co"><title>Program 1</title><sub-title>Sub-title &amp; 1</sub-title><desc>Description for Program 1</desc><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><date>20220506</date><category>Test</category><icon src="https://example.com/images/Program1.png?x=шеллы&amp;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><rating system="MPAA"><value>PG</value><icon src="http://example.com/pg_symbol.png"/></rating></programme>\r\n</tv>'
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.co"><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.co"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.co"><title>Program 1</title><sub-title>Sub-title &amp; 1</sub-title><desc>Description for Program 1</desc><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><date>20220506</date><category>Test</category><icon src="https://example.com/images/Program1.png?x=шеллы&amp;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><rating system="MPAA"><value>PG</value><icon src="http://example.com/pg_symbol.png"/></rating></programme>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="2TV.co"><title lang="es">Program 2</title></programme>\r\n</tv>'
)
})