Update format.js

This commit is contained in:
Aleksandr Statciuk 2021-08-10 03:13:05 +03:00
parent 6563598492
commit 4865fb3b65
1 changed files with 89 additions and 23 deletions

View File

@ -1,21 +1,44 @@
const IPTVChecker = require('iptv-checker')
const { program } = require('commander')
const ProgressBar = require('progress')
const parser = require('./helpers/parser')
const utils = require('./helpers/utils')
const file = require('./helpers/file')
const log = require('./helpers/log')
program
.usage('[OPTIONS]...')
.option('-d, --debug', 'Enable debug mode')
.option('-s, --status', 'Update stream status')
.option('-r, --resolution', 'Detect stream resolution')
.option('-c, --country <country>', 'Comma-separated list of country codes', '')
.option('-e, --exclude <exclude>', 'Comma-separated list of country codes to be excluded', '')
.option('--timeout <timeout>', 'Set timeout for each request', 5000)
.parse(process.argv)
let bar
const ignoreStatus = ['Geo-blocked', 'Not 24/7']
const config = program.opts()
const checker = new IPTVChecker({
timeout: config.timeout
})
async function main() {
log.start()
log.print(`Parsing 'index.m3u'...`)
if (config.debug) log.print(`Debug mode enabled\n`)
if (config.status) log.print(`Updating channel status...\n`)
if (config.resolution) log.print(`Detecting channel resolution...\n`)
let playlists = parser.parseIndex().filter(i => i.url !== 'channels/unsorted.m3u')
playlists = utils.filterPlaylists(playlists, config.country, config.exclude)
for (const playlist of playlists) {
log.print(`\nProcessing '${playlist.url}'...`)
await parser
.parsePlaylist(playlist.url)
.then(formatPlaylist)
.then(updatePlaylist)
.then(playlist => {
if (file.read(playlist.url) !== playlist.toString()) {
log.print('updated')
log.print(`File '${playlist.url}' has been updated\n`)
playlist.updated = true
}
@ -23,23 +46,55 @@ async function main() {
})
}
log.print('\n')
log.finish()
}
async function formatPlaylist(playlist) {
async function updatePlaylist(playlist) {
if (!config.debug) {
bar = new ProgressBar(`Processing '${playlist.url}': [:bar] :current/:total (:percent) `, {
total: playlist.channels.length
})
}
for (const channel of playlist.channels) {
const code = file.getBasename(playlist.url)
// add missing tvg-name
if (!channel.tvg.name && code !== 'unsorted' && channel.name) {
addMissingData(channel)
const checkOnline = config.status || config.resolution
const skip =
channel.status &&
ignoreStatus.map(i => i.toLowerCase()).includes(channel.status.toLowerCase())
if (checkOnline && !skip) {
await checker
.checkStream(channel.data)
.then(result => {
if (result.status.ok || result.status.reason.includes('timed out')) {
if (config.status) updateStatus(channel, null)
if (config.resolution) updateResolution(channel, result.status.metadata)
} else {
if (config.debug) log.print(`ERR: ${channel.url} (${result.status.reason})\n`)
if (config.status) updateStatus(channel, 'Offline')
}
})
.catch(err => {
if (config.debug) log.print(`ERR: ${channel.url} (${err.message})\n`)
})
}
if (!config.debug) bar.tick()
}
return playlist
}
function addMissingData(channel) {
// add tvg-name
if (!channel.tvg.name && channel.name) {
channel.tvg.name = channel.name.replace(/\"/gi, '')
}
// add missing tvg-id
if (!channel.tvg.id && code !== 'unsorted' && channel.tvg.name) {
// add tvg-id
if (!channel.tvg.id && channel.tvg.name) {
const id = utils.name2id(channel.tvg.name)
channel.tvg.id = id ? `${id}.${code}` : ''
}
// add missing country
// add country
if (!channel.countries.length) {
const name = utils.code2name(code)
channel.countries = name ? [{ code, name }] : []
@ -47,9 +102,20 @@ async function formatPlaylist(playlist) {
}
// update group-title
channel.group.title = channel.category
}
}
return playlist
function updateStatus(channel, status) {
channel.status = status
}
function updateResolution(channel, metadata) {
const streams = metadata ? metadata.streams.filter(stream => stream.codec_type === 'video') : []
if (!channel.resolution.height && streams.length) {
channel.resolution = streams.reduce((acc, curr) => {
if (curr.height > acc.height) return { width: curr.width, height: curr.height }
return acc
})
}
}
main()