Update clean.js
This commit is contained in:
parent
f7ea225cac
commit
e41bf2c72d
|
@ -1,14 +1,14 @@
|
||||||
const { program } = require('commander')
|
const { program } = require('commander')
|
||||||
const parser = require('./parser')
|
|
||||||
const utils = require('./utils')
|
|
||||||
const axios = require('axios')
|
|
||||||
const ProgressBar = require('progress')
|
const ProgressBar = require('progress')
|
||||||
|
const axios = require('axios')
|
||||||
const https = require('https')
|
const https = require('https')
|
||||||
const chalk = require('chalk')
|
const chalk = require('chalk')
|
||||||
|
const parser = require('./helpers/parser')
|
||||||
|
const utils = require('./helpers/utils')
|
||||||
|
const log = require('./helpers/log')
|
||||||
|
|
||||||
program
|
program
|
||||||
.usage('[OPTIONS]...')
|
.usage('[OPTIONS]...')
|
||||||
.option('-d, --debug', '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('--delay <delay>', 'Delay between parser requests', 1000)
|
||||||
|
@ -16,8 +16,8 @@ program
|
||||||
.parse(process.argv)
|
.parse(process.argv)
|
||||||
|
|
||||||
const config = program.opts()
|
const config = program.opts()
|
||||||
|
|
||||||
const offlineStatusCodes = [404, 410, 500, 501]
|
const offlineStatusCodes = [404, 410, 500, 501]
|
||||||
|
const ignore = ['Geo-blocked', 'Not 24/7']
|
||||||
const instance = axios.create({
|
const instance = axios.create({
|
||||||
timeout: config.timeout,
|
timeout: config.timeout,
|
||||||
maxContentLength: 200000,
|
maxContentLength: 200000,
|
||||||
|
@ -29,60 +29,39 @@ const instance = axios.create({
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const ignore = ['Geo-blocked', 'Not 24/7']
|
let broken = 0
|
||||||
|
|
||||||
const stats = { broken: 0 }
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
console.info(`\nStarting...`)
|
log.start()
|
||||||
console.time('Done in')
|
|
||||||
if (config.debug) {
|
|
||||||
console.info(chalk.yellow(`INFO: Debug mode enabled\n`))
|
|
||||||
}
|
|
||||||
const playlists = parseIndex()
|
|
||||||
|
|
||||||
for (const playlist of playlists) {
|
log.print(`Parsing 'index.m3u'...`)
|
||||||
await loadPlaylist(playlist.url).then(checkStatus).then(savePlaylist).then(done)
|
|
||||||
}
|
|
||||||
|
|
||||||
finish()
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseIndex() {
|
|
||||||
console.info(`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)
|
||||||
console.info(`Found ${playlists.length} playlist(s)\n`)
|
for (const playlist of playlists) {
|
||||||
|
await parser
|
||||||
|
.parsePlaylist(playlist.url)
|
||||||
|
.then(checkStatus)
|
||||||
|
.then(p => p.save())
|
||||||
|
}
|
||||||
|
|
||||||
return playlists
|
log.finish()
|
||||||
}
|
|
||||||
|
|
||||||
async function loadPlaylist(url) {
|
|
||||||
console.info(`Processing '${url}'...`)
|
|
||||||
return parser.parsePlaylist(url)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkStatus(playlist) {
|
async function checkStatus(playlist) {
|
||||||
let bar
|
let bar = new ProgressBar(`Checking '${playlist.url}': [:bar] :current/:total (:percent) `, {
|
||||||
if (!config.debug) {
|
total: playlist.channels.length
|
||||||
bar = new ProgressBar(' Testing: [:bar] :current/:total (:percent) ', {
|
})
|
||||||
total: playlist.channels.length
|
|
||||||
})
|
|
||||||
}
|
|
||||||
const results = []
|
const results = []
|
||||||
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 current = index + 1
|
||||||
const counter = chalk.gray(`[${current}/${total}]`)
|
const counter = chalk.gray(`[${current}/${total}]`)
|
||||||
if (bar) bar.tick()
|
bar.tick()
|
||||||
if (
|
if (
|
||||||
(channel.status && ignore.map(i => i.toLowerCase()).includes(channel.status.toLowerCase())) ||
|
(channel.status && ignore.map(i => i.toLowerCase()).includes(channel.status.toLowerCase())) ||
|
||||||
(!channel.url.startsWith('http://') && !channel.url.startsWith('https://'))
|
(!channel.url.startsWith('http://') && !channel.url.startsWith('https://'))
|
||||||
) {
|
) {
|
||||||
results.push(channel)
|
results.push(channel)
|
||||||
if (config.debug) {
|
|
||||||
console.info(` ${counter} ${chalk.green('online')} ${chalk.white(channel.url)}`)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
const CancelToken = axios.CancelToken
|
const CancelToken = axios.CancelToken
|
||||||
const source = CancelToken.source()
|
const source = CancelToken.source()
|
||||||
|
@ -95,23 +74,14 @@ async function checkStatus(playlist) {
|
||||||
.then(() => {
|
.then(() => {
|
||||||
clearTimeout(timeout)
|
clearTimeout(timeout)
|
||||||
results.push(channel)
|
results.push(channel)
|
||||||
if (config.debug) {
|
|
||||||
console.info(` ${counter} ${chalk.green('online')} ${chalk.white(channel.url)}`)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.then(utils.sleep(config.delay))
|
.then(utils.sleep(config.delay))
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
clearTimeout(timeout)
|
clearTimeout(timeout)
|
||||||
if (err.response && offlineStatusCodes.includes(err.response.status)) {
|
if (err.response && offlineStatusCodes.includes(err.response.status)) {
|
||||||
if (config.debug) {
|
broken++
|
||||||
console.info(` ${counter} ${chalk.red('offline')} ${chalk.white(channel.url)}`)
|
|
||||||
}
|
|
||||||
stats.broken++
|
|
||||||
} else {
|
} else {
|
||||||
results.push(channel)
|
results.push(channel)
|
||||||
if (config.debug) {
|
|
||||||
console.info(` ${counter} ${chalk.green('online')} ${chalk.white(channel.url)}`)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -122,27 +92,4 @@ async function checkStatus(playlist) {
|
||||||
return playlist
|
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)
|
|
||||||
console.info(`Playlist has been updated. Removed ${stats.broken} links.`)
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
async function done() {
|
|
||||||
console.info(` `)
|
|
||||||
}
|
|
||||||
|
|
||||||
function finish() {
|
|
||||||
console.timeEnd('Done in')
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue