This commit is contained in:
Aleksandr Statciuk 2022-06-16 21:38:26 +03:00
parent b56b7bdfc6
commit 748d805be6
6 changed files with 37 additions and 9 deletions

View File

@ -4,15 +4,16 @@ const { toArray, toUnix, parseNumber } = require('./utils')
class Program {
constructor(p) {
const data = {
channel: p.channel,
title: p.title,
site: p.site || '',
channel: p.channel || '',
title: p.title || '',
sub_title: p.sub_title || '',
description: [p.description, p.desc, ''].find(i => i !== undefined),
description: [p.description, p.desc].find(i => i) || '',
icon: toIconObject(p.icon),
episodeNumbers: getEpisodeNumbers(p.season, p.episode),
episodeNumbers: p.episodeNumbers || getEpisodeNumbers(p.season, p.episode),
date: p.date ? toUnix(p.date) : null,
start: toUnix(p.start),
stop: toUnix(p.stop),
start: p.start ? toUnix(p.start) : null,
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),
@ -84,7 +85,8 @@ function toUrlObject(url) {
}
function toIconObject(icon) {
if (!icon || typeof icon === 'string') return { src: icon }
if (!icon) return { src: '' }
if (typeof icon === 'string') return { src: icon }
return {
src: icon.src || ''

View File

@ -4,9 +4,13 @@ const { parseChannels, parsePrograms } = require('./parser')
const { generate: generateXMLTV } = require('./xmltv')
const { load: loadConfig } = require('./config')
const { sleep, isPromise } = require('./utils')
const Channel = require('./Channel')
const Program = require('./Program')
module.exports.generateXMLTV = generateXMLTV
module.exports.parseChannels = parseChannels
module.exports.Channel = Channel
module.exports.Program = Program
class EPGGrabber {
constructor(config = {}) {

View File

@ -44,6 +44,7 @@ async function parsePrograms(data) {
return programs
.filter(i => i)
.map(p => {
p.site = channel.site
p.channel = p.channel || channel.id
return new Program(p)

View File

@ -12,11 +12,16 @@ module.exports.parseNumber = parseNumber
module.exports.formatDate = formatDate
module.exports.toArray = toArray
module.exports.toUnix = toUnix
module.exports.isDate = isDate
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
function isDate(d) {
return dayjs(d).isValid()
}
function isObject(a) {
return !!a && a.constructor === Object
}

View File

@ -1,9 +1,23 @@
const { escapeString, getUTCDate, formatDate } = require('./utils')
const Channel = require('./Channel')
const Program = require('./Program')
const { escapeString, getUTCDate, formatDate, isDate } = require('./utils')
const el = createElement
module.exports.generate = generate
function generate({ channels, programs, date = getUTCDate() }) {
if (!channels.every(c => c instanceof Channel)) {
throw new Error('"channels" must be an array of Channels')
}
if (!programs.every(p => p instanceof Program)) {
throw new Error('"programs" must be an array of Programs')
}
if (!isDate(date)) {
throw new Error('"date" must be a valid date')
}
let output = `<?xml version="1.0" encoding="UTF-8" ?>`
output += createElements(channels, programs, date)

View File

@ -1,10 +1,11 @@
import Channel from '../src/Channel'
import Program from '../src/Program'
const channel = new Channel({ xmltv_id: '1tv', lang: 'en' })
const channel = new Channel({ xmltv_id: '1tv', lang: 'en', site: 'example.com' })
it('can create new Program', () => {
const program = new Program({
site: channel.site,
channel: channel.id,
title: 'Title',
sub_title: 'Subtitle',
@ -48,6 +49,7 @@ it('can create new Program', () => {
})
expect(program).toMatchObject({
site: 'example.com',
channel: '1tv',
title: 'Title',
sub_title: 'Subtitle',