wip
This commit is contained in:
		
							parent
							
								
									b56b7bdfc6
								
							
						
					
					
						commit
						748d805be6
					
				| 
						 | 
					@ -4,15 +4,16 @@ const { toArray, toUnix, parseNumber } = require('./utils')
 | 
				
			||||||
class Program {
 | 
					class Program {
 | 
				
			||||||
	constructor(p) {
 | 
						constructor(p) {
 | 
				
			||||||
		const data = {
 | 
							const data = {
 | 
				
			||||||
			channel: p.channel,
 | 
								site: p.site || '',
 | 
				
			||||||
			title: p.title,
 | 
								channel: p.channel || '',
 | 
				
			||||||
 | 
								title: p.title || '',
 | 
				
			||||||
			sub_title: p.sub_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),
 | 
								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,
 | 
								date: p.date ? toUnix(p.date) : null,
 | 
				
			||||||
			start: toUnix(p.start),
 | 
								start: p.start ? toUnix(p.start) : null,
 | 
				
			||||||
			stop: toUnix(p.stop),
 | 
								stop: p.stop ? toUnix(p.stop) : null,
 | 
				
			||||||
			urls: toArray(p.urls || p.url).map(toUrlObject),
 | 
								urls: toArray(p.urls || p.url).map(toUrlObject),
 | 
				
			||||||
			ratings: toArray(p.ratings || p.rating).map(toRatingObject),
 | 
								ratings: toArray(p.ratings || p.rating).map(toRatingObject),
 | 
				
			||||||
			categories: toArray(p.categories || p.category),
 | 
								categories: toArray(p.categories || p.category),
 | 
				
			||||||
| 
						 | 
					@ -84,7 +85,8 @@ function toUrlObject(url) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function toIconObject(icon) {
 | 
					function toIconObject(icon) {
 | 
				
			||||||
	if (!icon || typeof icon === 'string') return { src: icon }
 | 
						if (!icon) return { src: '' }
 | 
				
			||||||
 | 
						if (typeof icon === 'string') return { src: icon }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return {
 | 
						return {
 | 
				
			||||||
		src: icon.src || ''
 | 
							src: icon.src || ''
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,9 +4,13 @@ const { parseChannels, parsePrograms } = require('./parser')
 | 
				
			||||||
const { generate: generateXMLTV } = require('./xmltv')
 | 
					const { generate: generateXMLTV } = require('./xmltv')
 | 
				
			||||||
const { load: loadConfig } = require('./config')
 | 
					const { load: loadConfig } = require('./config')
 | 
				
			||||||
const { sleep, isPromise } = require('./utils')
 | 
					const { sleep, isPromise } = require('./utils')
 | 
				
			||||||
 | 
					const Channel = require('./Channel')
 | 
				
			||||||
 | 
					const Program = require('./Program')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports.generateXMLTV = generateXMLTV
 | 
					module.exports.generateXMLTV = generateXMLTV
 | 
				
			||||||
module.exports.parseChannels = parseChannels
 | 
					module.exports.parseChannels = parseChannels
 | 
				
			||||||
 | 
					module.exports.Channel = Channel
 | 
				
			||||||
 | 
					module.exports.Program = Program
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class EPGGrabber {
 | 
					class EPGGrabber {
 | 
				
			||||||
  constructor(config = {}) {
 | 
					  constructor(config = {}) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -44,6 +44,7 @@ async function parsePrograms(data) {
 | 
				
			||||||
  return programs
 | 
					  return programs
 | 
				
			||||||
    .filter(i => i)
 | 
					    .filter(i => i)
 | 
				
			||||||
    .map(p => {
 | 
					    .map(p => {
 | 
				
			||||||
 | 
					      p.site = channel.site
 | 
				
			||||||
      p.channel = p.channel || channel.id
 | 
					      p.channel = p.channel || channel.id
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      return new Program(p)
 | 
					      return new Program(p)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -12,11 +12,16 @@ module.exports.parseNumber = parseNumber
 | 
				
			||||||
module.exports.formatDate = formatDate
 | 
					module.exports.formatDate = formatDate
 | 
				
			||||||
module.exports.toArray = toArray
 | 
					module.exports.toArray = toArray
 | 
				
			||||||
module.exports.toUnix = toUnix
 | 
					module.exports.toUnix = toUnix
 | 
				
			||||||
 | 
					module.exports.isDate = isDate
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function sleep(ms) {
 | 
					function sleep(ms) {
 | 
				
			||||||
  return new Promise(resolve => setTimeout(resolve, ms))
 | 
					  return new Promise(resolve => setTimeout(resolve, ms))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function isDate(d) {
 | 
				
			||||||
 | 
					  return dayjs(d).isValid()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function isObject(a) {
 | 
					function isObject(a) {
 | 
				
			||||||
  return !!a && a.constructor === Object
 | 
					  return !!a && a.constructor === Object
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										16
									
								
								src/xmltv.js
								
								
								
								
							
							
						
						
									
										16
									
								
								src/xmltv.js
								
								
								
								
							| 
						 | 
					@ -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
 | 
					const el = createElement
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports.generate = generate
 | 
					module.exports.generate = generate
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function generate({ channels, programs, date = getUTCDate() }) {
 | 
					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" ?>`
 | 
						let output = `<?xml version="1.0" encoding="UTF-8" ?>`
 | 
				
			||||||
	output += createElements(channels, programs, date)
 | 
						output += createElements(channels, programs, date)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,10 +1,11 @@
 | 
				
			||||||
import Channel from '../src/Channel'
 | 
					import Channel from '../src/Channel'
 | 
				
			||||||
import Program from '../src/Program'
 | 
					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', () => {
 | 
					it('can create new Program', () => {
 | 
				
			||||||
  const program = new Program({
 | 
					  const program = new Program({
 | 
				
			||||||
 | 
					    site: channel.site,
 | 
				
			||||||
    channel: channel.id,
 | 
					    channel: channel.id,
 | 
				
			||||||
    title: 'Title',
 | 
					    title: 'Title',
 | 
				
			||||||
    sub_title: 'Subtitle',
 | 
					    sub_title: 'Subtitle',
 | 
				
			||||||
| 
						 | 
					@ -48,6 +49,7 @@ it('can create new Program', () => {
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  expect(program).toMatchObject({
 | 
					  expect(program).toMatchObject({
 | 
				
			||||||
 | 
					    site: 'example.com',
 | 
				
			||||||
    channel: '1tv',
 | 
					    channel: '1tv',
 | 
				
			||||||
    title: 'Title',
 | 
					    title: 'Title',
 | 
				
			||||||
    sub_title: 'Subtitle',
 | 
					    sub_title: 'Subtitle',
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue