epg-grabber 0.18.0 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -181,6 +181,7 @@ From each function in `config.js` you can access a `context` object containing t
181
181
  - `date`: The 'dayjs' instance with the requested date
182
182
  - `content`: The response data as a String
183
183
  - `buffer`: The response data as an ArrayBuffer
184
+ - `headers`: The response headers
184
185
 
185
186
  ## Channels List
186
187
 
@@ -79,28 +79,32 @@ async function main() {
79
79
  if (!config.channels) return logger.error('Path to [site].channels.xml is missing')
80
80
  logger.info(`Loading '${config.channels}'...`)
81
81
  const channelsXML = fs.readFileSync(path.resolve(config.channels), { encoding: 'utf-8' })
82
- const channels = utils.parseChannels(channelsXML)
82
+ const { channels } = utils.parseChannels(channelsXML)
83
83
 
84
84
  let programs = []
85
85
  let i = 1
86
86
  let days = options.days || 1
87
87
  const total = channels.length * days
88
+ const utcDate = utils.getUTCDate()
89
+ const dates = Array.from({ length: config.days }, (_, i) => utcDate.add(i, 'd'))
88
90
  for (let channel of channels) {
89
- await grabber
90
- .grab(channel, config, (data, err) => {
91
- logger.info(
92
- `[${i}/${total}] ${config.site} - ${data.channel.xmltv_id} - ${data.date.format(
93
- 'MMM D, YYYY'
94
- )} (${data.programs.length} programs)`
95
- )
96
-
97
- if (err) logger.error(err.message)
98
-
99
- if (i < total) i++
100
- })
101
- .then(results => {
102
- programs = programs.concat(results)
103
- })
91
+ for (let date of dates) {
92
+ await grabber
93
+ .grab(channel, date, config, (data, err) => {
94
+ logger.info(
95
+ `[${i}/${total}] ${config.site} - ${data.channel.xmltv_id} - ${data.date.format(
96
+ 'MMM D, YYYY'
97
+ )} (${data.programs.length} programs)`
98
+ )
99
+
100
+ if (err) logger.error(err.message)
101
+
102
+ if (i < total) i++
103
+ })
104
+ .then(results => {
105
+ programs = programs.concat(results)
106
+ })
107
+ }
104
108
  }
105
109
 
106
110
  const xml = utils.convertToXMLTV({ config, channels, programs })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "epg-grabber",
3
- "version": "0.18.0",
3
+ "version": "0.21.0",
4
4
  "description": "Node.js CLI tool for grabbing EPG from different sites",
5
5
  "main": "src/index.js",
6
6
  "preferGlobal": true,
package/src/index.js CHANGED
@@ -1,35 +1,29 @@
1
1
  const utils = require('./utils')
2
2
 
3
3
  module.exports = {
4
- grab: async function (channel, config, cb) {
4
+ grab: async function (channel, date, config, cb) {
5
+ date = typeof date === 'string' ? utils.getUTCDate(date) : date
5
6
  config = utils.loadConfig(config)
6
7
  channel.lang = channel.lang || config.lang || null
7
8
 
8
- const utcDate = utils.getUTCDate()
9
- const dates = Array.from({ length: config.days }, (_, i) => utcDate.add(i, 'd'))
10
- const queue = []
11
- dates.forEach(date => {
12
- queue.push({ date, channel })
13
- })
14
-
15
9
  let programs = []
16
- for (let item of queue) {
17
- await utils
18
- .buildRequest(item, config)
19
- .then(request => utils.fetchData(request))
20
- .then(response => utils.parseResponse(item, response, config))
21
- .then(results => {
22
- item.programs = results
23
- cb(item, null)
24
- programs = programs.concat(results)
25
- })
26
- .catch(err => {
27
- item.programs = []
28
- cb(item, err)
29
- })
30
10
 
31
- await utils.sleep(config.delay)
32
- }
11
+ const item = { date, channel }
12
+ await utils
13
+ .buildRequest(item, config)
14
+ .then(request => utils.fetchData(request))
15
+ .then(response => utils.parseResponse(item, response, config))
16
+ .then(results => {
17
+ item.programs = results
18
+ cb(item, null)
19
+ programs = programs.concat(results)
20
+ })
21
+ .catch(err => {
22
+ item.programs = []
23
+ cb(item, err)
24
+ })
25
+
26
+ await utils.sleep(config.delay)
33
27
 
34
28
  return programs
35
29
  },
package/src/utils.js CHANGED
@@ -65,7 +65,7 @@ utils.parseChannels = function (xml) {
65
65
  return channel
66
66
  })
67
67
 
68
- return channels
68
+ return { site, channels }
69
69
  }
70
70
 
71
71
  utils.sleep = function (ms) {
@@ -259,14 +259,17 @@ utils.getRequestUrl = async function (item, config) {
259
259
  return config.url
260
260
  }
261
261
 
262
- utils.getUTCDate = function () {
262
+ utils.getUTCDate = function (d = null) {
263
+ if (typeof d === 'string') return dayjs.utc(d).startOf('d')
264
+
263
265
  return dayjs.utc().startOf('d')
264
266
  }
265
267
 
266
268
  utils.parseResponse = async (item, response, config) => {
267
269
  const data = merge(item, config, {
268
270
  content: response.data.toString(),
269
- buffer: response.data
271
+ buffer: response.data,
272
+ headers: response.headers
270
273
  })
271
274
 
272
275
  if (!item.channel.logo && config.logo) {
@@ -27,7 +27,7 @@ it('return "Connection timeout" error if server does not response', done => {
27
27
  lang: 'en',
28
28
  name: 'CNN'
29
29
  }
30
- grabber.grab(channel, config, (data, err) => {
30
+ grabber.grab(channel, '2022-01-01', config, (data, err) => {
31
31
  expect(err.message).toBe('Connection timeout')
32
32
  done()
33
33
  })
@@ -54,7 +54,7 @@ it('can grab single channel programs', done => {
54
54
  name: '1TV'
55
55
  }
56
56
  grabber
57
- .grab(channel, config, (data, err) => {
57
+ .grab(channel, '2022-01-01', config, (data, err) => {
58
58
  if (err) {
59
59
  console.log(` Error: ${err.message}`)
60
60
  done()
@@ -28,7 +28,7 @@ it('can load valid config.js', () => {
28
28
 
29
29
  it('can parse valid channels.xml', () => {
30
30
  const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
31
- const channels = utils.parseChannels(file)
31
+ const { channels } = utils.parseChannels(file)
32
32
  expect(channels).toEqual([
33
33
  {
34
34
  name: '1 TV',
@@ -51,7 +51,7 @@ it('can parse valid channels.xml', () => {
51
51
 
52
52
  it('can convert object to xmltv string', () => {
53
53
  const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
54
- const channels = utils.parseChannels(file)
54
+ const { channels } = utils.parseChannels(file)
55
55
  const programs = [
56
56
  {
57
57
  title: 'Program 1',
@@ -101,7 +101,7 @@ it('can convert object to xmltv string without categories', () => {
101
101
 
102
102
  it('can convert object to xmltv string with multiple categories', () => {
103
103
  const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
104
- const channels = utils.parseChannels(file)
104
+ const { channels } = utils.parseChannels(file)
105
105
  const programs = [
106
106
  {
107
107
  title: 'Program 1',