Update utils.js
This commit is contained in:
		
							parent
							
								
									b498d24815
								
							
						
					
					
						commit
						c74b87f8dc
					
				
							
								
								
									
										36
									
								
								src/utils.js
								
								
								
								
							
							
						
						
									
										36
									
								
								src/utils.js
								
								
								
								
							| 
						 | 
					@ -22,10 +22,6 @@ utils.loadConfig = function (options) {
 | 
				
			||||||
  const configPath = path.resolve(file)
 | 
					  const configPath = path.resolve(file)
 | 
				
			||||||
  const config = require(configPath)
 | 
					  const config = require(configPath)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (options.channels) config.channels = options.channels
 | 
					 | 
				
			||||||
  else if (config.channels) config.channels = path.join(path.dirname(file), config.channels)
 | 
					 | 
				
			||||||
  else throw new Error("The required 'channels' property is missing")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if (!config.site) throw new Error("The required 'site' property is missing")
 | 
					  if (!config.site) throw new Error("The required 'site' property is missing")
 | 
				
			||||||
  if (!config.url) throw new Error("The required 'url' property is missing")
 | 
					  if (!config.url) throw new Error("The required 'url' property is missing")
 | 
				
			||||||
  if (typeof config.url !== 'function' && typeof config.url !== 'string')
 | 
					  if (typeof config.url !== 'function' && typeof config.url !== 'string')
 | 
				
			||||||
| 
						 | 
					@ -60,8 +56,7 @@ utils.parseChannels = function (filename) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const xml = fs.readFileSync(path.resolve(filename), { encoding: 'utf-8' })
 | 
					  const xml = fs.readFileSync(path.resolve(filename), { encoding: 'utf-8' })
 | 
				
			||||||
  const result = convert.xml2js(xml)
 | 
					  const result = convert.xml2js(xml)
 | 
				
			||||||
  const site = result.elements.find(el => el.name === 'site')
 | 
					  const channels = result.elements.find(el => el.name === 'channels')
 | 
				
			||||||
  const channels = site.elements.find(el => el.name === 'channels')
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return channels.elements
 | 
					  return channels.elements
 | 
				
			||||||
    .filter(el => el.name === 'channel')
 | 
					    .filter(el => el.name === 'channel')
 | 
				
			||||||
| 
						 | 
					@ -69,16 +64,13 @@ utils.parseChannels = function (filename) {
 | 
				
			||||||
      const channel = el.attributes
 | 
					      const channel = el.attributes
 | 
				
			||||||
      if (!el.elements) throw new Error(`Channel '${channel.xmltv_id}' has no valid name`)
 | 
					      if (!el.elements) throw new Error(`Channel '${channel.xmltv_id}' has no valid name`)
 | 
				
			||||||
      channel.name = el.elements.find(el => el.type === 'text').text
 | 
					      channel.name = el.elements.find(el => el.type === 'text').text
 | 
				
			||||||
      channel.site = channel.site || site.attributes.site
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      return channel
 | 
					      return channel
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
utils.sleep = function (ms) {
 | 
					utils.sleep = function (ms) {
 | 
				
			||||||
  return function (x) {
 | 
					  return new Promise(resolve => setTimeout(resolve, ms))
 | 
				
			||||||
    return new Promise(resolve => setTimeout(() => resolve(x), ms))
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
utils.escapeString = function (string, defaultValue = '') {
 | 
					utils.escapeString = function (string, defaultValue = '') {
 | 
				
			||||||
| 
						 | 
					@ -199,7 +191,7 @@ utils.getRequestHeaders = async function (item, config) {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return headers
 | 
					    return headers
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  return config.request.headers
 | 
					  return config.request.headers || null
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
utils.getRequestData = async function (item, config) {
 | 
					utils.getRequestData = async function (item, config) {
 | 
				
			||||||
| 
						 | 
					@ -210,7 +202,7 @@ utils.getRequestData = async function (item, config) {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return data
 | 
					    return data
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  return config.request.data
 | 
					  return config.request.data || null
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
utils.getRequestUrl = async function (item, config) {
 | 
					utils.getRequestUrl = async function (item, config) {
 | 
				
			||||||
| 
						 | 
					@ -229,28 +221,20 @@ utils.getUTCDate = function () {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
utils.parseResponse = async (item, response, config) => {
 | 
					utils.parseResponse = async (item, response, config) => {
 | 
				
			||||||
  const options = merge(item, config, {
 | 
					  const data = merge(item, config, {
 | 
				
			||||||
    content: response.data.toString(),
 | 
					    content: response.data.toString(),
 | 
				
			||||||
    buffer: response.data
 | 
					    buffer: response.data
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (!item.channel.logo && config.logo) {
 | 
					  if (!item.channel.logo && config.logo) {
 | 
				
			||||||
    item.channel.logo = await utils.loadLogo(options, config)
 | 
					    item.channel.logo = await utils.loadLogo(data, config)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const parsed = await utils.parsePrograms(options, config)
 | 
					  return await utils.parsePrograms(data, config)
 | 
				
			||||||
 | 
					 | 
				
			||||||
  console.log(
 | 
					 | 
				
			||||||
    `  ${config.site} - ${item.channel.xmltv_id} - ${item.date.format('MMM D, YYYY')} (${
 | 
					 | 
				
			||||||
      parsed.length
 | 
					 | 
				
			||||||
    } programs)`
 | 
					 | 
				
			||||||
  )
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  return parsed
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
utils.parsePrograms = async function (options, config) {
 | 
					utils.parsePrograms = async function (data, config) {
 | 
				
			||||||
  let programs = config.parser(options)
 | 
					  let programs = config.parser(data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (this.isPromise(programs)) {
 | 
					  if (this.isPromise(programs)) {
 | 
				
			||||||
    programs = await programs
 | 
					    programs = await programs
 | 
				
			||||||
| 
						 | 
					@ -260,7 +244,7 @@ utils.parsePrograms = async function (options, config) {
 | 
				
			||||||
    throw new Error('Parser should return an array')
 | 
					    throw new Error('Parser should return an array')
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const channel = options.channel
 | 
					  const channel = data.channel
 | 
				
			||||||
  return programs
 | 
					  return programs
 | 
				
			||||||
    .filter(i => i)
 | 
					    .filter(i => i)
 | 
				
			||||||
    .map(program => {
 | 
					    .map(program => {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue