iptv/scripts/format.js

206 lines
5.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-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
let globalBuffer = []
2019-11-03 14:03:35 +01:00
async function main() {
2021-01-30 00:26:20 +01:00
const playlists = parseIndex()
for (const playlist of playlists) {
await loadPlaylist(playlist.url)
.then(addToBuffer)
.then(removeDuplicates)
.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
}
2021-05-06 15:05:29 +02:00
// if (playlists.length) {
// await loadPlaylist('channels/unsorted.m3u')
// .then(removeUnsortedDuplicates)
// .then(filterChannels)
// .then(sortChannels)
// .then(savePlaylist)
// .then(done)
// }
2020-04-22 15:41:54 +02:00
finish()
2019-11-03 14:03:35 +01:00
}
2019-11-03 08:56:54 +01:00
function parseIndex() {
console.info(`Parsing '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 addToBuffer(playlist) {
if (playlist.url === 'channels/unsorted.m3u') return playlist
2021-01-30 00:26:20 +01:00
globalBuffer = globalBuffer.concat(playlist.channels)
2019-11-03 17:01:04 +01:00
return playlist
2019-11-03 08:56:54 +01:00
}
2019-04-30 15:52:38 +02:00
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 removeDuplicates(playlist) {
console.info(` Looking for duplicates...`)
2019-11-03 17:01:04 +01:00
let buffer = {}
2021-01-30 00:26:20 +01:00
const channels = playlist.channels.filter(i => {
const url = utils.removeProtocol(i.url)
const result = typeof buffer[url] === 'undefined'
2020-04-11 03:34:50 +02:00
if (result) {
buffer[url] = true
2019-11-03 14:03:35 +01:00
}
2019-11-03 17:01:04 +01:00
return result
})
2021-01-30 00:26:20 +01:00
playlist.channels = channels
2019-11-03 17:01:04 +01:00
return playlist
}
async function detectResolution(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) {
const response = await instance
.get(channel.url)
.then(utils.sleep(config.delay))
.catch(err => {})
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
}
async function removeUnsortedDuplicates(playlist) {
console.info(` Looking for duplicates...`)
// locally
let buffer = {}
let channels = playlist.channels.filter(i => {
const url = utils.removeProtocol(i.url)
const result = typeof buffer[url] === 'undefined'
if (result) buffer[url] = true
return result
})
// globally
const urls = globalBuffer.map(i => utils.removeProtocol(i.url))
channels = channels.filter(i => !urls.includes(utils.removeProtocol(i.url)))
2021-01-30 00:26:20 +01:00
if (channels.length === playlist.channels.length) return playlist
2021-01-30 00:26:20 +01:00
playlist.channels = channels
2019-11-03 17:01:04 +01:00
return playlist
2019-11-03 14:03:35 +01:00
}
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-01-30 05:18:06 +01:00
const output = playlist.toString(true)
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-01-11 13:07:38 +01:00
console.info('Done.')
2020-04-22 15:41:54 +02:00
}
2019-11-03 08:56:54 +01:00
main()