This commit is contained in:
Aleksandr Statciuk 2022-06-13 17:23:00 +03:00
parent 99b96a7d19
commit a7e601792c
2 changed files with 34 additions and 27 deletions

View File

@ -10,12 +10,12 @@ module.exports.generate = generate
function generate({ channels, programs, date = getUTCDate() }) { function generate({ channels, programs, date = getUTCDate() }) {
let output = `<?xml version="1.0" encoding="UTF-8" ?>` let output = `<?xml version="1.0" encoding="UTF-8" ?>`
const elements = createElements(channels, programs, date) output += createElements(channels, programs, date)
console.log(JSON.stringify(elements, null, 2)) // console.log(JSON.stringify(elements, null, 2))
elements.forEach(elem => { // elements.forEach(elem => {
output += toString(elem) // output += toString(elem)
}) // })
return output return output
} }
@ -24,19 +24,25 @@ const el = createElement
function createElements(channels, programs, date) { function createElements(channels, programs, date) {
date = formatDate(date, 'YYYYMMDD') date = formatDate(date, 'YYYYMMDD')
return [ return el('tv', { date }, [
el('tv', { date }, [ ...channels.map(channel => {
...channels.map(channel => { const url = channel.site ? `https://${channel.site}` : ''
const url = channel.site ? `https://${channel.site}` : ''
return el('channel', { id: channel.xmltv_id }, [ return (
el('display-name', {}, [channel.name]), '\r\n' +
el('channel', { id: channel.xmltv_id }, [
el('display-name', {}, [escapeString(channel.name)]),
el('icon', { src: channel.logo }), el('icon', { src: channel.logo }),
el('url', {}, [url]) el('url', {}, [url])
]) ])
}), )
...programs.map(program => { }),
const lang = program.lang || 'en' ...programs.map(program => {
return el( const lang = program.lang || 'en'
return (
'\r\n' +
el(
'programme', 'programme',
{ {
start: formatDate(program.start, 'YYYYMMDDHHmmss ZZ'), start: formatDate(program.start, 'YYYYMMDDHHmmss ZZ'),
@ -44,9 +50,9 @@ function createElements(channels, programs, date) {
channel: program.channel channel: program.channel
}, },
[ [
el('title', { lang }, [program.title]), el('title', { lang }, [escapeString(program.title)]),
el('sub-title', { lang }, [program.sub_title]), el('sub-title', { lang }, [escapeString(program.sub_title)]),
el('desc', { lang }, [program.description]), el('desc', { lang }, [escapeString(program.description)]),
el('date', {}, [date]), el('date', {}, [date]),
el('icon', { src: program.icon }), el('icon', { src: program.icon }),
el('url', {}, [program.url]), el('url', {}, [program.url]),
@ -68,7 +74,9 @@ function createElements(channels, programs, date) {
...toArray(program.commentator).map(data => createCastMember('commentator', data)), ...toArray(program.commentator).map(data => createCastMember('commentator', data)),
...toArray(program.guest).map(data => createCastMember('guest', data)) ...toArray(program.guest).map(data => createCastMember('guest', data))
]), ]),
...toArray(program.category).map(category => el('category', { lang }, [category])), ...toArray(program.category).map(category =>
el('category', { lang }, [escapeString(category)])
),
...toArray(program.rating).map(rating => ...toArray(program.rating).map(rating =>
el('rating', { system: rating.system }, [ el('rating', { system: rating.system }, [
el('value', {}, [rating.value]), el('value', {}, [rating.value]),
@ -77,9 +85,9 @@ function createElements(channels, programs, date) {
) )
] ]
) )
}) )
]) })
] ])
} }
function formatEpisodeNum(s, e, system) { function formatEpisodeNum(s, e, system) {
@ -113,7 +121,7 @@ function createOnScreen(s, e) {
function createCastMember(position, data) { function createCastMember(position, data) {
data = toObject(data) data = toObject(data)
return el(position, {}, [ return el(position, {}, [
data.value, escapeString(data.value),
...toArray(data.url).map(createURL), ...toArray(data.url).map(createURL),
...toArray(data.image).map(createImage) ...toArray(data.image).map(createImage)
]) ])
@ -155,11 +163,11 @@ function formatDate(date, format) {
} }
function createElement(name, attrs = {}, children = []) { function createElement(name, attrs = {}, children = []) {
return { name, attrs, children } return toString({ name, attrs, children })
} }
function toString(elem) { function toString(elem) {
if (typeof elem === 'string') return escapeString(elem) if (typeof elem === 'string') return elem
let attrs = '' let attrs = ''
for (let key in elem.attrs) { for (let key in elem.attrs) {

View File

@ -1,5 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505"> <?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="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> <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 &amp; 1</sub-title><desc lang="it">Description for Program 1</desc><date>20220505</date><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><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> <programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><sub-title lang="it">Sub-title &amp; 1</sub-title><desc lang="it">Description for Program 1</desc><date>20220505</date><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><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>
</tv>