You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iptv/scripts/clean.js

75 lines
2.1 KiB
JavaScript

3 years ago
const IPTVChecker = require('iptv-checker')
4 years ago
const { program } = require('commander')
const ProgressBar = require('progress')
3 years ago
const parser = require('./helpers/parser')
const utils = require('./helpers/utils')
const log = require('./helpers/log')
4 years ago
program
.usage('[OPTIONS]...')
3 years ago
.option('-d, --debug', 'Enable debug mode')
4 years ago
.option('-c, --country <country>', 'Comma-separated list of country codes', '')
.option('-e, --exclude <exclude>', 'Comma-separated list of country codes to be excluded', '')
4 years ago
.option('--timeout <timeout>', 'Set timeout for each request', 5000)
4 years ago
.parse(process.argv)
3 years ago
let bar
4 years ago
const config = program.opts()
3 years ago
const ignoreStatus = ['Geo-blocked', 'Not 24/7', 'Offline']
3 years ago
const checker = new IPTVChecker({
timeout: config.timeout
4 years ago
})
async function main() {
3 years ago
log.start()
4 years ago
3 years ago
if (config.debug) log.print(`Debug mode enabled\n`)
4 years ago
let playlists = parser.parseIndex()
playlists = utils.filterPlaylists(playlists, config.country, config.exclude)
3 years ago
for (const playlist of playlists) {
await parser
.parsePlaylist(playlist.url)
3 years ago
.then(checkPlaylist)
3 years ago
.then(p => p.save())
}
4 years ago
3 years ago
log.finish()
4 years ago
}
3 years ago
async function checkPlaylist(playlist) {
3 years ago
if (!config.debug) {
bar = new ProgressBar(`Checking '${playlist.url}': [:bar] :current/:total (:percent) `, {
total: playlist.channels.length
})
}
3 years ago
const channels = []
4 years ago
const total = playlist.channels.length
for (const [index, channel] of playlist.channels.entries()) {
3 years ago
const skipChannel =
channel.status &&
ignoreStatus.map(i => i.toLowerCase()).includes(channel.status.toLowerCase())
3 years ago
if (skipChannel) {
3 years ago
channels.push(channel)
4 years ago
} else {
3 years ago
const result = await checker.checkStream(channel.data)
3 years ago
if (result.status.ok || result.status.reason.includes('timed out')) {
3 years ago
channels.push(channel)
} else {
3 years ago
if (config.debug) log.print(`ERR: ${channel.url} (${result.status.reason})\n`)
3 years ago
}
4 years ago
}
3 years ago
if (!config.debug) bar.tick()
4 years ago
}
3 years ago
if (playlist.channels.length !== channels.length) {
log.print(`File '${playlist.url}' has been updated\n`)
playlist.channels = channels
playlist.updated = true
3 years ago
}
4 years ago
return playlist
}
main()