iptv/scripts/format.js

165 lines
4.0 KiB
JavaScript
Raw Normal View History

2021-01-17 16:00:52 +01:00
const { program } = require('commander')
const blacklist = require('./blacklist')
2021-01-30 00:26:20 +01:00
const parser = require('./parser')
const utils = require('./utils')
const axios = require('axios')
const ProgressBar = require('progress')
const https = require('https')
2019-04-30 15:52:38 +02:00
2021-01-17 16:00:52 +01:00
program
.usage('[OPTIONS]...')
.option('-d, --debug', 'Debug mode')
2021-05-07 02:29:54 +02:00
.option('-r --resolution', 'Parse stream resolution')
2021-01-19 23:02:40 +01:00
.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:13:01 +02:00
.option('--delay <delay>', 'Delay between parser requests', 1000)
2021-05-06 15:05:29 +02:00
.option('--timeout <timeout>', 'Set timeout for each request', 5000)
2021-01-17 16:00:52 +01:00
.parse(process.argv)
const config = program.opts()
2019-07-20 09:06:29 +02:00
2021-01-19 00:38:37 +01:00
const instance = axios.create({
timeout: config.timeout,
2021-05-06 15:05:29 +02:00
maxContentLength: 200000,
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
2021-01-19 00:38:37 +01:00
})
2021-01-18 22:01:25 +01:00
2019-11-03 14:03:35 +01:00
async function main() {
2021-05-07 11:51:57 +02:00
console.info('Starting...')
console.time('Done in')
2021-01-30 00:26:20 +01:00
const playlists = parseIndex()
for (const playlist of playlists) {
await loadPlaylist(playlist.url)
.then(sortChannels)
.then(filterChannels)
.then(detectResolution)
2021-01-30 05:18:06 +01:00
.then(savePlaylist)
.then(done)
2019-11-03 14:03:35 +01:00
}
finish()
2019-11-03 14:03:35 +01:00
}
2019-11-03 08:56:54 +01:00
function parseIndex() {
2021-05-07 11:51:57 +02:00
console.info(`\nParsing 'index.m3u'...`)
2021-01-30 00:26:20 +01:00
let playlists = parser.parseIndex()
playlists = utils
.filterPlaylists(playlists, config.country, config.exclude)
.filter(i => i.url !== 'channels/unsorted.m3u')
2021-01-30 00:26:20 +01:00
console.info(`Found ${playlists.length} playlist(s)\n`)
2019-11-03 17:01:04 +01:00
2021-01-30 00:26:20 +01:00
return playlists
2019-11-03 14:03:35 +01:00
}
async function loadPlaylist(url) {
console.info(`Processing '${url}'...`)
2021-01-30 00:26:20 +01:00
return parser.parsePlaylist(url)
}
async function sortChannels(playlist) {
console.info(` Sorting channels...`)
2021-01-30 00:26:20 +01:00
playlist.channels = utils.sortBy(playlist.channels, ['name', 'url'])
2019-11-03 17:01:04 +01:00
return playlist
}
2019-11-03 14:03:35 +01:00
async function filterChannels(playlist) {
console.info(` Filtering channels...`)
const list = blacklist.map(i => i.toLowerCase())
playlist.channels = playlist.channels.filter(i => {
return !list.includes(i.name.toLowerCase())
})
return playlist
}
async function detectResolution(playlist) {
2021-05-07 02:29:54 +02:00
if (!config.resolution) return playlist
2021-05-06 14:21:05 +02:00
console.log(' Detecting resolution...')
const bar = new ProgressBar(' Progress: [:bar] :current/:total (:percent) ', {
2021-01-30 00:26:20 +01:00
total: playlist.channels.length
})
const results = []
2021-01-30 00:26:20 +01:00
for (const channel of playlist.channels) {
bar.tick()
if (!channel.resolution.height) {
2021-05-07 15:21:33 +02:00
const CancelToken = axios.CancelToken
const source = CancelToken.source()
const timeout = setTimeout(() => {
source.cancel()
}, config.timeout)
const response = await instance
2021-05-07 15:21:33 +02:00
.get(channel.url, { cancelToken: source.token })
2021-05-07 17:55:59 +02:00
.then(res => {
2021-05-07 15:21:33 +02:00
clearTimeout(timeout)
2021-05-07 17:55:59 +02:00
return res
2021-05-07 15:21:33 +02:00
})
.then(utils.sleep(config.delay))
2021-05-07 15:21:33 +02:00
.catch(err => {
clearTimeout(timeout)
})
if (response && response.status === 200) {
2021-01-19 20:08:35 +01:00
if (/^#EXTM3U/.test(response.data)) {
const resolution = parseResolution(response.data)
if (resolution) {
2021-01-30 00:26:20 +01:00
channel.resolution = resolution
2021-01-19 20:08:35 +01:00
}
}
}
}
2019-11-03 14:03:35 +01:00
2021-01-30 00:26:20 +01:00
results.push(channel)
}
2020-04-11 03:34:50 +02:00
2021-01-30 00:26:20 +01:00
playlist.channels = results
2019-11-03 14:03:35 +01:00
return playlist
}
2019-11-03 17:01:04 +01:00
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
}
2021-01-30 05:18:06 +01:00
async function savePlaylist(playlist) {
2021-01-30 00:26:20 +01:00
const original = utils.readFile(playlist.url)
2021-05-06 15:25:00 +02:00
const output = playlist.toString()
if (original === output) {
console.info(`No changes have been made.`)
return false
} else {
2021-01-30 00:26:20 +01:00
utils.createFile(playlist.url, output)
console.info(`Playlist has been updated.`)
2020-04-22 15:41:54 +02:00
}
return true
}
async function done() {
console.info(` `)
}
function finish() {
2021-05-07 11:51:57 +02:00
console.timeEnd('Done in')
2020-04-22 15:41:54 +02:00
}
2019-11-03 08:56:54 +01:00
main()