2021-08-01 05:52:03 +02:00
|
|
|
const { program } = require('commander')
|
|
|
|
const ProgressBar = require('progress')
|
2021-08-01 19:12:17 +02:00
|
|
|
const axios = require('axios')
|
2021-08-01 05:52:03 +02:00
|
|
|
const https = require('https')
|
2021-08-01 19:12:17 +02:00
|
|
|
const parser = require('./helpers/parser')
|
|
|
|
const utils = require('./helpers/utils')
|
|
|
|
const log = require('./helpers/log')
|
2021-08-01 05:52:03 +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', '')
|
|
|
|
.option('--delay <delay>', 'Delay between parser requests', 1000)
|
|
|
|
.option('--timeout <timeout>', 'Set timeout for each request', 5000)
|
|
|
|
.parse(process.argv)
|
|
|
|
|
|
|
|
const config = program.opts()
|
2021-08-06 06:49:39 +02:00
|
|
|
const ignoreStatus = ['Offline']
|
2021-08-01 05:52:03 +02:00
|
|
|
const instance = axios.create({
|
|
|
|
timeout: config.timeout,
|
|
|
|
maxContentLength: 200000,
|
|
|
|
httpsAgent: new https.Agent({
|
|
|
|
rejectUnauthorized: false
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
async function main() {
|
2021-08-01 19:12:17 +02:00
|
|
|
log.start()
|
2021-08-01 05:52:03 +02:00
|
|
|
|
2021-08-01 19:12:17 +02:00
|
|
|
log.print(`Parsing 'index.m3u'...\n`)
|
2021-08-01 05:52:03 +02:00
|
|
|
let playlists = parser.parseIndex()
|
|
|
|
playlists = utils
|
|
|
|
.filterPlaylists(playlists, config.country, config.exclude)
|
|
|
|
.filter(i => i.url !== 'channels/unsorted.m3u')
|
|
|
|
|
2021-08-01 19:12:17 +02:00
|
|
|
for (const playlist of playlists) {
|
|
|
|
await parser
|
|
|
|
.parsePlaylist(playlist.url)
|
|
|
|
.then(detectResolution)
|
|
|
|
.then(p => p.save())
|
|
|
|
}
|
2021-08-01 05:52:03 +02:00
|
|
|
|
2021-08-01 19:12:17 +02:00
|
|
|
log.finish()
|
2021-08-01 05:52:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function detectResolution(playlist) {
|
2021-08-01 19:12:17 +02:00
|
|
|
const channels = []
|
2021-08-01 05:52:03 +02:00
|
|
|
const bar = new ProgressBar(`Processing '${playlist.url}': [:bar] :current/:total (:percent) `, {
|
|
|
|
total: playlist.channels.length
|
|
|
|
})
|
2021-08-01 19:12:17 +02:00
|
|
|
let updated = false
|
2021-08-01 05:52:03 +02:00
|
|
|
for (const channel of playlist.channels) {
|
|
|
|
bar.tick()
|
2021-08-06 06:49:39 +02:00
|
|
|
const skipChannel =
|
|
|
|
channel.status &&
|
|
|
|
ignoreStatus.map(i => i.toLowerCase()).includes(channel.status.toLowerCase())
|
|
|
|
if (!channel.resolution.height && !skipChannel) {
|
2021-08-01 05:52:03 +02:00
|
|
|
const CancelToken = axios.CancelToken
|
|
|
|
const source = CancelToken.source()
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
source.cancel()
|
|
|
|
}, config.timeout)
|
|
|
|
|
|
|
|
const response = await instance
|
|
|
|
.get(channel.url, { cancelToken: source.token })
|
|
|
|
.then(res => {
|
|
|
|
clearTimeout(timeout)
|
|
|
|
|
|
|
|
return res
|
|
|
|
})
|
|
|
|
.then(utils.sleep(config.delay))
|
|
|
|
.catch(err => {
|
|
|
|
clearTimeout(timeout)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (response && response.status === 200) {
|
|
|
|
if (/^#EXTM3U/.test(response.data)) {
|
|
|
|
const resolution = parseResolution(response.data)
|
|
|
|
if (resolution) {
|
|
|
|
channel.resolution = resolution
|
2021-08-01 19:12:17 +02:00
|
|
|
updated = true
|
2021-08-01 05:52:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 19:12:17 +02:00
|
|
|
channels.push(channel)
|
2021-08-01 05:52:03 +02:00
|
|
|
}
|
|
|
|
|
2021-08-01 19:12:17 +02:00
|
|
|
if (updated) {
|
|
|
|
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:12:17 +02:00
|
|
|
}
|
2021-08-01 05:52:03 +02:00
|
|
|
|
|
|
|
return playlist
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseResolution(string) {
|
|
|
|
const regex = /RESOLUTION=(\d+)x(\d+)/gm
|
|
|
|
const match = string.matchAll(regex)
|
|
|
|
const arr = Array.from(match).map(m => ({
|
|
|
|
width: parseInt(m[1]),
|
|
|
|
height: parseInt(m[2])
|
|
|
|
}))
|
|
|
|
|
|
|
|
return arr.length
|
|
|
|
? arr.reduce(function (prev, current) {
|
|
|
|
return prev.height > current.height ? prev : current
|
|
|
|
})
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
main()
|