2021-01-17 16:00:52 +01:00
|
|
|
const { program } = require('commander')
|
2021-02-28 23:43:45 +01:00
|
|
|
const blacklist = require('./blacklist')
|
2021-01-30 00:26:20 +01:00
|
|
|
const parser = require('./parser')
|
|
|
|
const utils = require('./utils')
|
2021-01-11 11:55:57 +01:00
|
|
|
const axios = require('axios')
|
2021-01-11 13:06:03 +01:00
|
|
|
const ProgressBar = require('progress')
|
2021-01-19 13:53:22 +01:00
|
|
|
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,
|
2021-01-19 13:53:22 +01:00
|
|
|
httpsAgent: new https.Agent({
|
|
|
|
rejectUnauthorized: false
|
|
|
|
})
|
2021-01-19 00:38:37 +01:00
|
|
|
})
|
2021-01-18 22:01:25 +01:00
|
|
|
|
2021-01-11 11:55:57 +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)
|
2021-01-11 11:55:57 +01:00
|
|
|
.then(addToBuffer)
|
2021-04-16 04:18:18 +02:00
|
|
|
.then(removeDuplicates)
|
2021-01-11 11:55:57 +01:00
|
|
|
.then(sortChannels)
|
2021-02-28 23:43:45 +01:00
|
|
|
.then(filterChannels)
|
2021-01-11 11:55:57 +01:00
|
|
|
.then(detectResolution)
|
2021-01-30 05:18:06 +01:00
|
|
|
.then(savePlaylist)
|
2021-01-11 11:55:57 +01:00
|
|
|
.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
|
|
|
|
2021-01-11 11:55:57 +01:00
|
|
|
finish()
|
2019-11-03 14:03:35 +01:00
|
|
|
}
|
2019-11-03 08:56:54 +01:00
|
|
|
|
|
|
|
function parseIndex() {
|
2021-01-11 11:55:57 +01:00
|
|
|
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)
|
2021-01-11 11:55:57 +01:00
|
|
|
.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
|
|
|
}
|
|
|
|
|
2021-01-11 11:55:57 +01:00
|
|
|
async function loadPlaylist(url) {
|
|
|
|
console.info(`Processing '${url}'...`)
|
2021-01-30 00:26:20 +01:00
|
|
|
return parser.parsePlaylist(url)
|
2021-01-11 11:55:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-01-11 11:55:57 +01:00
|
|
|
async function sortChannels(playlist) {
|
2021-01-11 13:06:03 +01:00
|
|
|
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
|
|
|
|
2021-02-28 23:43:45 +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
|
|
|
|
}
|
|
|
|
|
2021-01-11 11:55:57 +01:00
|
|
|
async function removeDuplicates(playlist) {
|
2021-01-11 13:06:03 +01:00
|
|
|
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 => {
|
2021-03-27 13:09:13 +01:00
|
|
|
const url = utils.removeProtocol(i.url)
|
2021-02-28 23:09:51 +01:00
|
|
|
const result = typeof buffer[url] === 'undefined'
|
2020-04-11 03:34:50 +02:00
|
|
|
if (result) {
|
2021-02-28 23:09:51 +01:00
|
|
|
buffer[url] = true
|
2019-11-03 14:03:35 +01:00
|
|
|
}
|
2021-01-11 13:06:03 +01:00
|
|
|
|
2019-11-03 17:01:04 +01:00
|
|
|
return result
|
|
|
|
})
|
|
|
|
|
2021-01-30 00:26:20 +01:00
|
|
|
playlist.channels = channels
|
2021-01-11 11:55:57 +01:00
|
|
|
|
2019-11-03 17:01:04 +01:00
|
|
|
return playlist
|
|
|
|
}
|
|
|
|
|
2021-01-11 11:55:57 +01:00
|
|
|
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
|
2021-01-11 13:06:03 +01:00
|
|
|
})
|
2021-01-11 11:55:57 +01:00
|
|
|
const results = []
|
2021-01-30 00:26:20 +01:00
|
|
|
for (const channel of playlist.channels) {
|
2021-01-11 13:06:03 +01:00
|
|
|
bar.tick()
|
2021-05-06 00:09:32 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-01-11 11:55:57 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-03 14:03:35 +01:00
|
|
|
|
2021-01-30 00:26:20 +01:00
|
|
|
results.push(channel)
|
2021-01-11 11:55:57 +01:00
|
|
|
}
|
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
|
|
|
|
2021-01-11 11:55:57 +01:00
|
|
|
return playlist
|
|
|
|
}
|
2019-11-03 17:01:04 +01:00
|
|
|
|
2021-01-11 13:06:03 +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-11 11:55:57 +01:00
|
|
|
async function removeUnsortedDuplicates(playlist) {
|
2021-01-11 13:06:03 +01:00
|
|
|
console.info(` Looking for duplicates...`)
|
2021-03-27 13:09:13 +01:00
|
|
|
// 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-03-27 13:09:13 +01:00
|
|
|
|
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)
|
2021-01-11 13:06:03 +01:00
|
|
|
|
|
|
|
if (original === output) {
|
2021-01-11 11:55:57 +01:00
|
|
|
console.info(`No changes have been made.`)
|
|
|
|
return false
|
2021-01-11 13:06:03 +01:00
|
|
|
} else {
|
2021-01-30 00:26:20 +01:00
|
|
|
utils.createFile(playlist.url, output)
|
2021-01-11 13:06:03 +01:00
|
|
|
console.info(`Playlist has been updated.`)
|
2020-04-22 15:41:54 +02:00
|
|
|
}
|
2021-01-11 11:55:57 +01:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
async function done() {
|
2021-01-11 13:06:03 +01:00
|
|
|
console.info(` `)
|
2021-01-11 11:55:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|