mirror of
https://github.com/iptv-org/iptv.git
synced 2025-05-13 02:20:03 -04:00
Update clean.js
This commit is contained in:
parent
c24aad7fb5
commit
c5860bb8b3
1 changed files with 22 additions and 47 deletions
|
@ -1,93 +1,68 @@
|
||||||
|
const IPTVChecker = require('iptv-checker')
|
||||||
const { program } = require('commander')
|
const { program } = require('commander')
|
||||||
const ProgressBar = require('progress')
|
const ProgressBar = require('progress')
|
||||||
const axios = require('axios')
|
|
||||||
const https = require('https')
|
|
||||||
const chalk = require('chalk')
|
|
||||||
const parser = require('./helpers/parser')
|
const parser = require('./helpers/parser')
|
||||||
const utils = require('./helpers/utils')
|
const utils = require('./helpers/utils')
|
||||||
const log = require('./helpers/log')
|
const log = require('./helpers/log')
|
||||||
|
|
||||||
program
|
program
|
||||||
.usage('[OPTIONS]...')
|
.usage('[OPTIONS]...')
|
||||||
|
.option('-d, --debug', 'Enable debug mode')
|
||||||
.option('-c, --country <country>', 'Comma-separated list of country codes', '')
|
.option('-c, --country <country>', 'Comma-separated list of country codes', '')
|
||||||
.option('-e, --exclude <exclude>', 'Comma-separated list of country codes to be excluded', '')
|
.option('-e, --exclude <exclude>', 'Comma-separated list of country codes to be excluded', '')
|
||||||
.option('--delay <delay>', 'Delay between parser requests', 1000)
|
|
||||||
.option('--timeout <timeout>', 'Set timeout for each request', 5000)
|
.option('--timeout <timeout>', 'Set timeout for each request', 5000)
|
||||||
.parse(process.argv)
|
.parse(process.argv)
|
||||||
|
|
||||||
|
let bar
|
||||||
const config = program.opts()
|
const config = program.opts()
|
||||||
const offlineStatusCodes = [404, 410, 451, 500, 501]
|
|
||||||
const ignoreStatus = ['Geo-blocked', 'Not 24/7', 'Offline']
|
const ignoreStatus = ['Geo-blocked', 'Not 24/7', 'Offline']
|
||||||
const instance = axios.create({
|
const checker = new IPTVChecker({
|
||||||
timeout: config.timeout,
|
timeout: config.timeout
|
||||||
maxContentLength: 200000,
|
|
||||||
httpsAgent: new https.Agent({
|
|
||||||
rejectUnauthorized: false
|
|
||||||
}),
|
|
||||||
validateStatus: function (status) {
|
|
||||||
return !offlineStatusCodes.includes(status)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
let broken = 0
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
log.start()
|
log.start()
|
||||||
|
|
||||||
|
if (config.debug) log.print(`Debug mode enabled\n`)
|
||||||
|
|
||||||
log.print(`Parsing 'index.m3u'...`)
|
log.print(`Parsing 'index.m3u'...`)
|
||||||
let playlists = parser.parseIndex()
|
let playlists = parser.parseIndex()
|
||||||
playlists = utils.filterPlaylists(playlists, config.country, config.exclude)
|
playlists = utils.filterPlaylists(playlists, config.country, config.exclude)
|
||||||
for (const playlist of playlists) {
|
for (const playlist of playlists) {
|
||||||
await parser
|
await parser
|
||||||
.parsePlaylist(playlist.url)
|
.parsePlaylist(playlist.url)
|
||||||
.then(checkStatus)
|
.then(checkPlaylist)
|
||||||
.then(p => p.save())
|
.then(p => p.save())
|
||||||
}
|
}
|
||||||
|
|
||||||
log.finish()
|
log.finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkStatus(playlist) {
|
async function checkPlaylist(playlist) {
|
||||||
let bar = new ProgressBar(`Checking '${playlist.url}': [:bar] :current/:total (:percent) `, {
|
bar = new ProgressBar(`Checking '${playlist.url}': [:bar] :current/:total (:percent) `, {
|
||||||
total: playlist.channels.length
|
total: playlist.channels.length
|
||||||
})
|
})
|
||||||
const channels = []
|
const channels = []
|
||||||
const total = playlist.channels.length
|
const total = playlist.channels.length
|
||||||
for (const [index, channel] of playlist.channels.entries()) {
|
for (const [index, channel] of playlist.channels.entries()) {
|
||||||
const current = index + 1
|
|
||||||
const counter = chalk.gray(`[${current}/${total}]`)
|
|
||||||
const skipChannel =
|
const skipChannel =
|
||||||
channel.status &&
|
channel.status &&
|
||||||
ignoreStatus.map(i => i.toLowerCase()).includes(channel.status.toLowerCase())
|
ignoreStatus.map(i => i.toLowerCase()).includes(channel.status.toLowerCase())
|
||||||
bar.tick()
|
if (skipChannel) {
|
||||||
|
channels.push(channel)
|
||||||
|
} else {
|
||||||
|
const result = await checker.checkStream(channel.data)
|
||||||
if (
|
if (
|
||||||
skipChannel ||
|
result.status.ok ||
|
||||||
(!channel.url.startsWith('http://') && !channel.url.startsWith('https://'))
|
result.status.reason.includes('timed out') ||
|
||||||
|
result.status.reason.includes('access denied')
|
||||||
) {
|
) {
|
||||||
channels.push(channel)
|
channels.push(channel)
|
||||||
} else {
|
} else {
|
||||||
const CancelToken = axios.CancelToken
|
if (config.debug) bar.interrupt(`ERR: ${channel.url}: ${result.status.reason}`)
|
||||||
const source = CancelToken.source()
|
|
||||||
const timeout = setTimeout(() => {
|
|
||||||
source.cancel()
|
|
||||||
}, config.timeout)
|
|
||||||
|
|
||||||
await instance
|
|
||||||
.get(channel.url, { cancelToken: source.token })
|
|
||||||
.then(() => {
|
|
||||||
clearTimeout(timeout)
|
|
||||||
channels.push(channel)
|
|
||||||
})
|
|
||||||
.then(utils.sleep(config.delay))
|
|
||||||
.catch(err => {
|
|
||||||
clearTimeout(timeout)
|
|
||||||
if (err.response && offlineStatusCodes.includes(err.response.status)) {
|
|
||||||
broken++
|
|
||||||
} else {
|
|
||||||
channels.push(channel)
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
bar.tick()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (playlist.channels.length !== channels.length) {
|
if (playlist.channels.length !== channels.length) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue