iptv/scripts/test.js

75 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-01-20 01:11:19 +01:00
const { program } = require('commander')
2021-01-30 04:41:19 +01:00
const utils = require('./utils')
const parser = require('./parser')
2021-01-20 01:11:19 +01:00
const axios = require('axios')
const https = require('https')
const ProgressBar = require('progress')
2018-11-16 00:41:15 +01:00
2021-01-20 01:11:19 +01:00
program
.usage('[OPTIONS]...')
.option('-d, --debug', 'Debug mode')
.option('-c, --country <country>', 'Comma-separated list of country codes', '')
.option('-e, --exclude <exclude>', 'Comma-separated list of country codes to be excluded', '')
.option('--delay <delay>', 'Delay between parser requests', 1000)
.option('--timeout <timeout>', 'Set timeout for each request', 5000)
.parse(process.argv)
const config = program.opts()
const instance = axios.create({
timeout: config.timeout,
maxContentLength: 20000,
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
})
2018-11-16 00:41:15 +01:00
2019-07-20 09:33:16 +02:00
let stats = {
2019-11-03 18:21:35 +01:00
playlists: 0,
2019-07-20 09:33:16 +02:00
channels: 0,
failures: 0
}
2018-11-16 00:41:15 +01:00
2019-07-20 09:33:16 +02:00
async function test() {
2021-01-30 04:41:19 +01:00
let items = parser.parseIndex()
items = utils.filterPlaylists(items, config.country, config.exclude)
2018-11-16 00:41:15 +01:00
2021-01-30 04:41:19 +01:00
for (const item of items) {
const playlist = parser.parsePlaylist(item.url)
const bar = new ProgressBar(`Processing '${item.url}'...:current/:total`, {
total: playlist.channels.length
2021-01-20 01:11:19 +01:00
})
2021-01-20 01:11:19 +01:00
stats.playlists++
2018-11-16 00:41:15 +01:00
2021-01-30 04:41:19 +01:00
for (let channel of playlist.channels) {
2021-01-20 01:11:19 +01:00
bar.tick()
stats.channels++
await instance
.get(channel.url)
2021-01-30 04:41:19 +01:00
.then(utils.sleep(config.delay))
2021-01-20 01:11:19 +01:00
.catch(error => {
if (error.response) {
stats.failures++
utils.writeToLog(item.url, error.message, channel.url)
2021-01-20 01:11:19 +01:00
}
})
2018-11-16 00:41:15 +01:00
}
2018-12-22 20:07:47 +01:00
}
2018-11-16 00:41:15 +01:00
if (stats.failures === 0) {
2021-01-20 01:11:19 +01:00
console.log(`\nOK (${stats.playlists} playlists, ${stats.channels} channels)`)
2018-12-22 20:07:47 +01:00
} else {
console.log(
`\nFAILURES! (${stats.playlists} playlists, ${stats.channels} channels, ${stats.failures} failures)
\n\nCheck the "error.log" file to see which links failed.`
)
2018-11-16 00:41:15 +01:00
2019-09-07 17:24:59 +02:00
process.exit(1)
2018-12-22 20:07:47 +01:00
}
2018-11-16 00:41:15 +01:00
}
2021-01-20 01:11:19 +01:00
console.log('Test is running...\n')
2018-12-26 20:41:17 +01:00
2019-07-20 09:33:16 +02:00
test()