2021-05-06 01:11:51 +02:00
|
|
|
const { program } = require('commander')
|
|
|
|
const parser = require('./parser')
|
|
|
|
const utils = require('./utils')
|
|
|
|
const axios = require('axios')
|
|
|
|
const ProgressBar = require('progress')
|
|
|
|
const https = require('https')
|
2021-05-07 01:52:11 +02:00
|
|
|
const chalk = require('chalk')
|
2021-05-06 01:11:51 +02: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', '')
|
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()
|
|
|
|
|
|
|
|
const instance = axios.create({
|
|
|
|
timeout: config.timeout,
|
|
|
|
maxContentLength: 200000,
|
|
|
|
httpsAgent: new https.Agent({
|
|
|
|
rejectUnauthorized: false
|
|
|
|
}),
|
|
|
|
validateStatus: function (status) {
|
|
|
|
return status !== 404
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const ignore = ['Geo-blocked', 'Not 24/7']
|
|
|
|
|
2021-05-07 02:19:28 +02:00
|
|
|
const stats = { broken: 0 }
|
|
|
|
|
2021-05-06 01:11:51 +02:00
|
|
|
async function main() {
|
2021-05-07 02:19:28 +02:00
|
|
|
console.info(`\nStarting...`)
|
|
|
|
console.time('Process completed in')
|
2021-05-07 01:52:11 +02:00
|
|
|
if (config.debug) {
|
|
|
|
console.info(chalk.yellow(`INFO: Debug mode enabled\n`))
|
|
|
|
}
|
2021-05-06 01:11:51 +02:00
|
|
|
const playlists = parseIndex()
|
|
|
|
|
|
|
|
for (const playlist of playlists) {
|
|
|
|
await loadPlaylist(playlist.url).then(checkStatus).then(savePlaylist).then(done)
|
|
|
|
}
|
|
|
|
|
|
|
|
finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseIndex() {
|
|
|
|
console.info(`Parsing 'index.m3u'...`)
|
|
|
|
let playlists = parser.parseIndex()
|
|
|
|
playlists = utils.filterPlaylists(playlists, config.country, config.exclude)
|
|
|
|
console.info(`Found ${playlists.length} playlist(s)\n`)
|
|
|
|
|
|
|
|
return playlists
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadPlaylist(url) {
|
|
|
|
console.info(`Processing '${url}'...`)
|
|
|
|
return parser.parsePlaylist(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkStatus(playlist) {
|
2021-05-07 01:52:11 +02:00
|
|
|
let bar
|
|
|
|
if (!config.debug) {
|
|
|
|
bar = new ProgressBar(' Testing: [:bar] :current/:total (:percent) ', {
|
|
|
|
total: playlist.channels.length
|
|
|
|
})
|
|
|
|
}
|
2021-05-06 01:11:51 +02:00
|
|
|
const results = []
|
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}]`)
|
|
|
|
if (bar) 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://'))
|
|
|
|
) {
|
|
|
|
results.push(channel)
|
2021-05-07 01:52:11 +02:00
|
|
|
if (config.debug) {
|
|
|
|
console.info(` ${counter} ${chalk.green('online')} ${chalk.white(channel.url)}`)
|
|
|
|
}
|
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)
|
|
|
|
|
2021-05-06 01:25:25 +02:00
|
|
|
await instance
|
2021-05-07 15:17:37 +02:00
|
|
|
.get(channel.url, { cancelToken: source.token })
|
2021-05-06 01:25:25 +02:00
|
|
|
.then(() => {
|
2021-05-07 15:17:37 +02:00
|
|
|
clearTimeout(timeout)
|
2021-05-06 01:25:25 +02:00
|
|
|
results.push(channel)
|
2021-05-07 01:52:11 +02:00
|
|
|
if (config.debug) {
|
|
|
|
console.info(` ${counter} ${chalk.green('online')} ${chalk.white(channel.url)}`)
|
|
|
|
}
|
2021-05-06 01:25:25 +02:00
|
|
|
})
|
2021-05-06 01:11:51 +02:00
|
|
|
.then(utils.sleep(config.delay))
|
2021-05-06 01:25:25 +02:00
|
|
|
.catch(err => {
|
2021-05-07 15:17:37 +02:00
|
|
|
clearTimeout(timeout)
|
2021-05-06 01:25:25 +02:00
|
|
|
if (err.response && err.response.status === 404) {
|
|
|
|
//console.error(err)
|
2021-05-07 01:52:11 +02:00
|
|
|
if (config.debug) {
|
|
|
|
console.info(` ${counter} ${chalk.red('offline')} ${chalk.white(channel.url)}`)
|
2021-05-07 02:19:28 +02:00
|
|
|
stats.broken++
|
2021-05-07 01:52:11 +02:00
|
|
|
}
|
2021-05-06 01:25:25 +02:00
|
|
|
} else {
|
|
|
|
results.push(channel)
|
2021-05-07 01:52:11 +02:00
|
|
|
if (config.debug) {
|
|
|
|
console.info(` ${counter} ${chalk.green('online')} ${chalk.white(channel.url)}`)
|
|
|
|
}
|
2021-05-06 01:25:25 +02:00
|
|
|
}
|
|
|
|
})
|
2021-05-06 01:11:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
playlist.channels = results
|
|
|
|
|
|
|
|
return playlist
|
|
|
|
}
|
|
|
|
|
|
|
|
async function savePlaylist(playlist) {
|
|
|
|
const original = utils.readFile(playlist.url)
|
|
|
|
const output = playlist.toString({ raw: true })
|
|
|
|
|
|
|
|
if (original === output) {
|
|
|
|
console.info(`No changes have been made.`)
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
utils.createFile(playlist.url, output)
|
2021-05-07 02:19:28 +02:00
|
|
|
console.info(`Playlist has been updated. Removed ${stats.broken} broken links.`)
|
2021-05-06 01:11:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
async function done() {
|
|
|
|
console.info(` `)
|
|
|
|
}
|
|
|
|
|
|
|
|
function finish() {
|
2021-05-07 02:19:28 +02:00
|
|
|
console.timeEnd('Process completed in')
|
2021-05-06 01:11:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
main()
|