iptv/scripts/format.js

221 lines
5.5 KiB
JavaScript
Raw Normal View History

2021-01-17 16:00:52 +01:00
const { program } = require('commander')
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-01-17 16:00:52 +01:00
.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 = []
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(sortChannels)
.then(removeDuplicates)
.then(detectResolution)
.then(updateFromEPG)
.then(updatePlaylist)
.then(done)
2019-11-03 14:03:35 +01:00
}
2021-01-30 00:26:20 +01:00
if (playlists.length) {
await loadPlaylist('channels/unsorted.m3u')
.then(removeUnsortedDuplicates)
.then(sortChannels)
.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'...`)
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 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 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
})
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) {
if (!config.resolution) return playlist
2021-01-19 23:02:40 +01:00
const bar = new ProgressBar(' Detecting resolution: [: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()
2021-01-30 00:26:20 +01:00
const url = channel.url
const response = await instance
.get(url)
2021-01-30 00:26:20 +01:00
.then(utils.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) {
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 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}'...`)
2021-01-30 00:26:20 +01:00
return utils
.parseEPG(tvgUrl)
.then(epg => {
if (!epg) return playlist
2021-01-30 00:26:20 +01:00
playlist.channels.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
}
2021-01-29 20:16:18 +01:00
if (!channel.languages.length && epgItem.name.length && epgItem.name[0].lang) {
2021-01-30 00:26:20 +01:00
channel.languages = utils.parseLanguages(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)
2021-01-30 00:26:20 +01:00
const channels = playlist.channels.filter(i => !urls.includes(i.url))
if (channels.length === playlist.channels.length) return playlist
playlist.channels = channels
2019-11-03 17:01:04 +01:00
return playlist
2019-11-03 14:03:35 +01:00
}
async function updatePlaylist(playlist) {
2021-01-30 00:26:20 +01:00
const original = utils.readFile(playlist.url)
let output = playlist.getHeader()
2021-01-30 00:26:20 +01:00
for (let channel of playlist.channels) {
output += channel.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()