iptv/scripts/test.js

75 lines
2.0 KiB
JavaScript
Raw Normal View History

2021-01-20 01:11:19 +01:00
const { program } = require('commander')
2019-11-02 12:10:57 +01:00
const helper = require('./helper')
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() {
2019-11-02 10:45:09 +01:00
const playlist = helper.parsePlaylist('index.m3u')
2018-11-16 00:41:15 +01:00
const countries = helper.filterPlaylists(playlist.items, config.country, config.exclude)
2018-11-16 00:41:15 +01:00
for (let country of countries) {
2021-01-20 01:11:19 +01:00
const playlist = helper.parsePlaylist(country.url)
const bar = new ProgressBar(`Processing '${country.url}'...:current/:total\n`, {
total: playlist.items.length
})
2021-01-20 01:11:19 +01:00
stats.playlists++
2018-11-16 00:41:15 +01:00
2021-01-20 01:11:19 +01:00
for (let channel of playlist.items) {
bar.tick()
stats.channels++
await instance
.get(channel.url)
.then(helper.sleep(config.delay))
.catch(error => {
if (error.response) {
stats.failures++
helper.writeToLog(country.url, error.message, channel.url)
console.log(`Error: ${error.message} '${channel.url}'`)
}
})
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(
2021-01-20 01:11:19 +01:00
`\nFAILURES! (${stats.playlists} playlists, ${stats.channels} channels, ${stats.failures} failures)`
)
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()