Update index.js
This commit is contained in:
parent
4189f4af00
commit
ac4b124f79
32
src/index.js
32
src/index.js
|
@ -1,18 +1,21 @@
|
||||||
const utils = require('./utils')
|
const utils = require('./utils')
|
||||||
|
|
||||||
module.exports = {
|
class EPGGrabber {
|
||||||
grab: async function (channel, date, config, cb) {
|
constructor(config = {}) {
|
||||||
|
this.config = utils.loadConfig(config)
|
||||||
|
this.client = utils.createClient(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
async grab(channel, date, cb = () => {}) {
|
||||||
date = typeof date === 'string' ? utils.getUTCDate(date) : date
|
date = typeof date === 'string' ? utils.getUTCDate(date) : date
|
||||||
config = utils.loadConfig(config)
|
channel.lang = channel.lang || this.config.lang || null
|
||||||
channel.lang = channel.lang || config.lang || null
|
|
||||||
|
|
||||||
let programs = []
|
let programs = []
|
||||||
|
|
||||||
const item = { date, channel }
|
const item = { date, channel }
|
||||||
await utils
|
await utils
|
||||||
.buildRequest(item, config)
|
.buildRequest(item, this.config)
|
||||||
.then(request => utils.fetchData(request))
|
.then(request => utils.fetchData(this.client, request))
|
||||||
.then(response => utils.parseResponse(item, response, config))
|
.then(response => utils.parseResponse(item, response, this.config))
|
||||||
.then(results => {
|
.then(results => {
|
||||||
item.programs = results
|
item.programs = results
|
||||||
cb(item, null)
|
cb(item, null)
|
||||||
|
@ -20,16 +23,19 @@ module.exports = {
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
item.programs = []
|
item.programs = []
|
||||||
if (config.debug) {
|
if (this.config.debug) {
|
||||||
console.log('Error:', JSON.stringify(error, null, 2))
|
console.log('Error:', JSON.stringify(error, null, 2))
|
||||||
}
|
}
|
||||||
cb(item, error)
|
cb(item, error)
|
||||||
})
|
})
|
||||||
|
|
||||||
await utils.sleep(config.delay)
|
await utils.sleep(this.config.delay)
|
||||||
|
|
||||||
return programs
|
return programs
|
||||||
},
|
}
|
||||||
convertToXMLTV: utils.convertToXMLTV,
|
|
||||||
parseChannels: utils.parseChannels
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EPGGrabber.convertToXMLTV = utils.convertToXMLTV
|
||||||
|
EPGGrabber.parseChannels = utils.parseChannels
|
||||||
|
|
||||||
|
module.exports = EPGGrabber
|
||||||
|
|
Loading…
Reference in New Issue