iptv/scripts/clean.js

100 lines
2.8 KiB
JavaScript
Raw Normal View History

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 axios = require('axios')
2021-05-06 01:11:51 +02:00
const https = require('https')
2021-05-07 01:52:11 +02:00
const chalk = require('chalk')
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]...')
.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('--delay <delay>', 'Delay between parser requests', 1000)
.option('--timeout <timeout>', 'Set timeout for each request', 5000)
2021-05-06 01:11:51 +02:00
.parse(process.argv)
const config = program.opts()
2021-06-07 20:25:10 +02:00
const offlineStatusCodes = [404, 410, 500, 501]
2021-08-01 18:46:42 +02:00
const ignore = ['Geo-blocked', 'Not 24/7']
2021-05-06 01:11:51 +02:00
const instance = axios.create({
timeout: config.timeout,
maxContentLength: 200000,
httpsAgent: new https.Agent({
rejectUnauthorized: false
}),
validateStatus: function (status) {
2021-06-07 20:25:10 +02:00
return !offlineStatusCodes.includes(status)
2021-05-06 01:11:51 +02:00
}
})
2021-08-01 18:46:42 +02:00
let broken = 0
2021-05-07 02:19:28 +02:00
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-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)
.then(checkStatus)
.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
}
async function checkStatus(playlist) {
2021-08-01 18:46:42 +02:00
let bar = new ProgressBar(`Checking '${playlist.url}': [:bar] :current/:total (:percent) `, {
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()) {
const current = index + 1
const counter = chalk.gray(`[${current}/${total}]`)
2021-08-01 18:46:42 +02:00
bar.tick()
2021-05-06 01:11:51 +02:00
if (
(channel.status && ignore.map(i => i.toLowerCase()).includes(channel.status.toLowerCase())) ||
(!channel.url.startsWith('http://') && !channel.url.startsWith('https://'))
) {
2021-08-01 19:49:00 +02:00
channels.push(channel)
2021-05-06 01:11:51 +02:00
} else {
2021-05-07 15:17:37 +02:00
const CancelToken = axios.CancelToken
const source = CancelToken.source()
const timeout = setTimeout(() => {
source.cancel()
}, config.timeout)
await instance
2021-05-07 15:17:37 +02:00
.get(channel.url, { cancelToken: source.token })
.then(() => {
2021-05-07 15:17:37 +02:00
clearTimeout(timeout)
2021-08-01 19:49:00 +02:00
channels.push(channel)
})
2021-05-06 01:11:51 +02:00
.then(utils.sleep(config.delay))
.catch(err => {
2021-05-07 15:17:37 +02:00
clearTimeout(timeout)
2021-06-07 20:25:10 +02:00
if (err.response && offlineStatusCodes.includes(err.response.status)) {
2021-08-01 18:46:42 +02:00
broken++
} else {
2021-08-01 19:49:00 +02:00
channels.push(channel)
}
})
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()