epg-grabber 0.19.0 → 0.22.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 +1 -0
- package/bin/epg-grabber.js +19 -15
- package/package.json +1 -1
- package/src/index.js +21 -24
- package/src/utils.js +19 -2
- package/tests/index.test.js +2 -2
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
|
|
package/bin/epg-grabber.js
CHANGED
|
@@ -85,22 +85,26 @@ async function main() {
|
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
package/src/index.js
CHANGED
|
@@ -1,35 +1,32 @@
|
|
|
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
|
-
|
|
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(error => {
|
|
22
|
+
item.programs = []
|
|
23
|
+
if (config.debug) {
|
|
24
|
+
console.log('Error:', JSON.stringify(error, null, 2))
|
|
25
|
+
}
|
|
26
|
+
cb(item, error)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
await utils.sleep(config.delay)
|
|
33
30
|
|
|
34
31
|
return programs
|
|
35
32
|
},
|
package/src/utils.js
CHANGED
|
@@ -259,14 +259,31 @@ 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) => {
|
|
269
|
+
if (config.debug) {
|
|
270
|
+
console.log(
|
|
271
|
+
'Response:',
|
|
272
|
+
JSON.stringify(
|
|
273
|
+
{
|
|
274
|
+
headers: response.headers,
|
|
275
|
+
data: response.data.toString()
|
|
276
|
+
},
|
|
277
|
+
null,
|
|
278
|
+
2
|
|
279
|
+
)
|
|
280
|
+
)
|
|
281
|
+
}
|
|
282
|
+
|
|
267
283
|
const data = merge(item, config, {
|
|
268
284
|
content: response.data.toString(),
|
|
269
|
-
buffer: response.data
|
|
285
|
+
buffer: response.data,
|
|
286
|
+
headers: response.headers
|
|
270
287
|
})
|
|
271
288
|
|
|
272
289
|
if (!item.channel.logo && config.logo) {
|
package/tests/index.test.js
CHANGED
|
@@ -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()
|