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

100 lines
2.8 KiB
JavaScript

4 years ago
const { program } = require('commander')
const ProgressBar = require('progress')
3 years ago
const axios = require('axios')
4 years ago
const https = require('https')
4 years ago
const chalk = require('chalk')
3 years ago
const parser = require('./helpers/parser')
const utils = require('./helpers/utils')
const log = require('./helpers/log')
4 years ago
program
.usage('[OPTIONS]...')
.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('--delay <delay>', 'Delay between parser requests', 1000)
.option('--timeout <timeout>', 'Set timeout for each request', 5000)
4 years ago
.parse(process.argv)
const config = program.opts()
4 years ago
const offlineStatusCodes = [404, 410, 500, 501]
3 years ago
const ignore = ['Geo-blocked', 'Not 24/7']
4 years ago
const instance = axios.create({
timeout: config.timeout,
maxContentLength: 200000,
httpsAgent: new https.Agent({
rejectUnauthorized: false
}),
validateStatus: function (status) {
4 years ago
return !offlineStatusCodes.includes(status)
4 years ago
}
})
3 years ago
let broken = 0
4 years ago
4 years ago
async function main() {
3 years ago
log.start()
4 years ago
3 years ago
log.print(`Parsing 'index.m3u'...`)
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)
.then(checkStatus)
.then(p => p.save())
}
4 years ago
3 years ago
log.finish()
4 years ago
}
async function checkStatus(playlist) {
3 years ago
let 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()) {
const current = index + 1
const counter = chalk.gray(`[${current}/${total}]`)
3 years ago
bar.tick()
4 years ago
if (
(channel.status && ignore.map(i => i.toLowerCase()).includes(channel.status.toLowerCase())) ||
(!channel.url.startsWith('http://') && !channel.url.startsWith('https://'))
) {
3 years ago
channels.push(channel)
4 years ago
} else {
const CancelToken = axios.CancelToken
const source = CancelToken.source()
const timeout = setTimeout(() => {
source.cancel()
}, config.timeout)
await instance
.get(channel.url, { cancelToken: source.token })
.then(() => {
clearTimeout(timeout)
3 years ago
channels.push(channel)
})
4 years ago
.then(utils.sleep(config.delay))
.catch(err => {
clearTimeout(timeout)
4 years ago
if (err.response && offlineStatusCodes.includes(err.response.status)) {
3 years ago
broken++
} else {
3 years ago
channels.push(channel)
}
})
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()