iptv/scripts/format.js

234 lines
5.7 KiB
JavaScript
Raw Normal View History

2021-01-17 16:00:52 +01:00
const { program } = require('commander')
2019-11-02 10:45:09 +01:00
const helper = require('./helper')
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
.version('1.0.0', '-v, --version')
.usage('[OPTIONS]...')
.option('-d, --debug', 'Debug mode')
.option('-c, --country <country>', 'Comma-separated list of country codes')
.option('-e, --exclude <exclude>', 'Comma-separated list of country codes to be excluded ')
.option('--epg', 'Turn on EPG parser')
2021-01-18 19:20:07 +01:00
.option('--resolution', 'Turn on resolution parser')
2021-01-18 19:28:22 +01:00
.option('--delay <delay>', 'Delay between parser requests', 0)
2021-01-18 22:01:25 +01: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-01-19 20:08:35 +01:00
maxContentLength: 20000,
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
2021-01-19 00:38:37 +01:00
})
2021-01-18 22:01:25 +01:00
let globalBuffer = []
let bar
2019-11-03 14:03:35 +01:00
async function main() {
2019-11-03 17:01:04 +01:00
const index = parseIndex()
for (const item of index.items) {
await loadPlaylist(item.url)
.then(addToBuffer)
.then(sortChannels)
.then(removeDuplicates)
.then(detectResolution)
.then(updateFromEPG)
.then(updatePlaylist)
.then(done)
2019-11-03 14:03:35 +01:00
}
if (index.items.length) {
await loadPlaylist('channels/unsorted.m3u')
.then(removeUnsortedDuplicates)
.then(updatePlaylist)
.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'...`)
2019-11-03 17:01:04 +01:00
const playlist = helper.parsePlaylist('index.m3u')
playlist.items = helper
.filterPlaylists(playlist.items, config.country, config.exclude)
.filter(i => i.url !== 'channels/unsorted.m3u')
console.info(`Found ${playlist.items.length} playlist(s)\n`)
2019-11-03 17:01:04 +01:00
return playlist
2019-11-03 14:03:35 +01:00
}
async function loadPlaylist(url) {
console.info(`Processing '${url}'...`)
2019-11-03 17:01:04 +01:00
const playlist = helper.parsePlaylist(url)
playlist.url = url
playlist.items = playlist.items
.map(item => {
return helper.createChannel(item)
})
.filter(i => i.url)
2019-11-03 17:01:04 +01:00
return playlist
}
async function addToBuffer(playlist) {
if (playlist.url === 'channels/unsorted.m3u') return playlist
globalBuffer = globalBuffer.concat(playlist.items)
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...`)
2020-05-04 15:33:02 +02:00
playlist.items = helper.sortBy(playlist.items, ['name', 'url'])
2019-11-03 17:01:04 +01:00
return playlist
}
2019-11-03 14:03:35 +01:00
async function removeDuplicates(playlist) {
console.info(` Looking for duplicates...`)
2019-11-03 17:01:04 +01:00
let buffer = {}
const items = playlist.items.filter(i => {
const result = typeof buffer[i.url] === 'undefined'
2020-04-11 03:34:50 +02:00
if (result) {
2019-11-03 17:01:04 +01:00
buffer[i.url] = true
2019-11-03 14:03:35 +01:00
}
2019-11-03 17:01:04 +01:00
return result
})
playlist.items = items
2019-11-03 17:01:04 +01:00
return playlist
}
async function detectResolution(playlist) {
if (!config.resolution) return playlist
bar = new ProgressBar(' Detecting resolution: [:bar] :current/:total (:percent) ', {
total: playlist.items.length
})
const results = []
for (const item of playlist.items) {
bar.tick()
const url = item.url
const response = await instance
.get(url)
.then(sleep(config.delay))
.catch(err => {})
2021-01-19 20:08:35 +01:00
if (response) {
if (response.status === 200) {
if (/^#EXTM3U/.test(response.data)) {
const resolution = parseResolution(response.data)
if (resolution) {
item.resolution = resolution
}
}
}
}
2019-11-03 14:03:35 +01:00
results.push(item)
}
2020-04-11 03:34:50 +02:00
playlist.items = 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 updateFromEPG(playlist) {
if (!config.epg) return playlist
const tvgUrl = playlist.header.attrs['x-tvg-url']
if (!tvgUrl) return playlist
console.info(` Adding data from '${tvgUrl}'...`)
return helper
.parseEPG(tvgUrl)
.then(epg => {
if (!epg) return playlist
playlist.items.map(channel => {
if (!channel.tvg.id) return channel
const epgItem = epg.channels[channel.tvg.id]
if (!epgItem) return channel
if (!channel.tvg.name && epgItem.name.length) {
channel.tvg.name = epgItem.name[0].value
}
if (!channel.language.length && epgItem.name.length && epgItem.name[0].lang) {
channel.setLanguage(epgItem.name[0].lang)
}
if (!channel.logo && epgItem.icon.length) {
channel.logo = epgItem.icon[0]
}
})
2019-11-03 17:01:04 +01:00
return playlist
})
.catch(err => {
console.log(`Error: EPG could not be loaded`)
})
}
2019-11-03 17:01:04 +01:00
async function removeUnsortedDuplicates(playlist) {
console.info(` Looking for duplicates...`)
const urls = globalBuffer.map(i => i.url)
const items = playlist.items.filter(i => !urls.includes(i.url))
if (items.length === playlist.items.length) return playlist
playlist.items = items
2019-11-03 17:01:04 +01:00
return playlist
2019-11-03 14:03:35 +01:00
}
function sleep(ms) {
return function (x) {
return new Promise(resolve => setTimeout(() => resolve(x), ms))
2019-11-03 14:03:35 +01:00
}
2019-08-07 14:40:58 +02:00
}
2019-04-30 15:52:38 +02:00
async function updatePlaylist(playlist) {
const original = helper.readFile(playlist.url)
let output = playlist.getHeader()
for (let channel of playlist.items) {
output += channel.toShortString()
}
if (original === output) {
console.info(`No changes have been made.`)
return false
} else {
helper.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()