iptv/scripts/clean.js

78 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-08-08 12:19:18 +02:00
const IPTVChecker = require('iptv-checker')
2021-05-06 01:11:51 +02:00
const { program } = require('commander')
const ProgressBar = require('progress')
2021-08-01 18:46:42 +02:00
const parser = require('./helpers/parser')
const utils = require('./helpers/utils')
const log = require('./helpers/log')
2021-05-06 01:11:51 +02:00
program
.usage('[OPTIONS]...')
2021-08-08 12:19:18 +02:00
.option('-d, --debug', 'Enable debug mode')
2021-05-06 01:11:51 +02:00
.option('-c, --country <country>', 'Comma-separated list of country codes', '')
.option('-e, --exclude <exclude>', 'Comma-separated list of country codes to be excluded', '')
2021-05-06 14:50:20 +02:00
.option('--timeout <timeout>', 'Set timeout for each request', 5000)
2021-05-06 01:11:51 +02:00
.parse(process.argv)
2021-08-08 12:19:18 +02:00
let bar
2021-05-06 01:11:51 +02:00
const config = program.opts()
2021-08-06 06:49:35 +02:00
const ignoreStatus = ['Geo-blocked', 'Not 24/7', 'Offline']
2021-08-08 12:19:18 +02:00
const checker = new IPTVChecker({
timeout: config.timeout
2021-05-06 01:11:51 +02:00
})
async function main() {
2021-08-01 18:46:42 +02:00
log.start()
2021-05-06 01:11:51 +02:00
2021-08-08 12:19:18 +02:00
if (config.debug) log.print(`Debug mode enabled\n`)
2021-08-01 18:46:42 +02:00
log.print(`Parsing 'index.m3u'...`)
2021-05-06 01:11:51 +02:00
let playlists = parser.parseIndex()
playlists = utils.filterPlaylists(playlists, config.country, config.exclude)
2021-08-01 18:46:42 +02:00
for (const playlist of playlists) {
await parser
.parsePlaylist(playlist.url)
2021-08-08 12:19:18 +02:00
.then(checkPlaylist)
2021-08-01 18:46:42 +02:00
.then(p => p.save())
}
2021-05-06 01:11:51 +02:00
2021-08-01 18:46:42 +02:00
log.finish()
2021-05-06 01:11:51 +02:00
}
2021-08-08 12:19:18 +02:00
async function checkPlaylist(playlist) {
bar = new ProgressBar(`Checking '${playlist.url}': [:bar] :current/:total (:percent) `, {
2021-08-01 18:46:42 +02:00
total: playlist.channels.length
})
2021-08-01 19:49:00 +02:00
const channels = []
2021-05-07 01:52:11 +02:00
const total = playlist.channels.length
for (const [index, channel] of playlist.channels.entries()) {
2021-08-06 06:49:35 +02:00
const skipChannel =
channel.status &&
ignoreStatus.map(i => i.toLowerCase()).includes(channel.status.toLowerCase())
2021-08-08 12:19:18 +02:00
if (skipChannel) {
2021-08-01 19:49:00 +02:00
channels.push(channel)
2021-05-06 01:11:51 +02:00
} else {
2021-08-08 12:19:18 +02:00
const result = await checker.checkStream(channel.data)
if (
result.status.ok ||
result.status.reason.includes('timed out') ||
result.status.reason.includes('access denied')
) {
channels.push(channel)
} else {
if (config.debug) bar.interrupt(`ERR: ${channel.url}: ${result.status.reason}`)
}
2021-05-06 01:11:51 +02:00
}
2021-08-08 12:19:18 +02:00
bar.tick()
2021-05-06 01:11:51 +02:00
}
2021-08-01 19:49:00 +02:00
if (playlist.channels.length !== channels.length) {
log.print(`File '${playlist.url}' has been updated\n`)
playlist.channels = channels
2021-08-02 18:07:10 +02:00
playlist.updated = true
2021-08-01 19:49:00 +02:00
}
2021-05-06 01:11:51 +02:00
return playlist
}
main()