Update format.js

This commit is contained in:
Aleksandr Statciuk 2021-08-18 16:39:26 +03:00
parent 572ca9953c
commit 9d74ff3095
1 changed files with 135 additions and 81 deletions

View File

@ -7,99 +7,151 @@ const utils = require('./helpers/utils')
const file = require('./helpers/file')
const log = require('./helpers/log')
const ignoreStatus = ['Geo-blocked', 'Not 24/7']
program
.usage('[OPTIONS]...')
.option('-d, --debug', 'Enable debug mode')
.option('-s, --status', 'Update stream status')
.option('-r, --resolution', 'Detect stream resolution')
.option('--debug', 'Enable debug mode')
.option('-d, --delay <delay>', 'Set delay for each request', parseNumber, 0)
.option('-t, --timeout <timeout>', 'Set timeout for each request', parseNumber, 5000)
.option('-c, --country <country>', 'Comma-separated list of country codes', '')
.option('-e, --exclude <exclude>', 'Comma-separated list of country codes to be excluded', '')
.option('--delay <delay>', 'Set delay for each request', 0)
.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
})
let buffer, origins
async function main() {
log.start()
if (config.debug) log.print(`Debug mode enabled\n`)
if (config.status) log.print(`Status check enabled\n`)
if (config.resolution) log.print(`Resolution detection enabled\n`)
let playlists = parser.parseIndex().filter(i => i.url !== 'channels/unsorted.m3u')
playlists = utils.filterPlaylists(playlists, config.country, config.exclude)
if (!playlists.length) log.print(`No playlist is selected\n`)
for (const playlist of playlists) {
await parser
.parsePlaylist(playlist.url)
.then(updatePlaylist)
.then(playlist => {
if (file.read(playlist.url) !== playlist.toString()) {
log.print(`File '${playlist.url}' has been updated\n`)
playlist.updated = true
}
playlist.save()
})
await parser.parsePlaylist(playlist.url).then(updatePlaylist).then(savePlaylist)
}
log.finish()
}
async function updatePlaylist(playlist) {
if (!config.debug) {
bar = new ProgressBar(`Processing '${playlist.url}': [:bar] :current/:total (:percent) `, {
total: playlist.channels.length
})
} else {
log.print(`Processing '${playlist.url}'...\n`)
function savePlaylist(playlist) {
if (file.read(playlist.url) !== playlist.toString()) {
log.print(`File '${playlist.url}' has been updated\n`)
playlist.updated = true
}
for (const channel of playlist.channels) {
addMissingData(channel, playlist)
updateGroupTitle(channel)
playlist.save()
}
async function updatePlaylist(playlist) {
const bar = new ProgressBar(`Processing '${playlist.url}': [:bar] :current/:total (:percent) `, {
total: playlist.channels.length
})
buffer = {}
origins = {}
for (const [i, channel] of playlist.channels.entries()) {
bar.tick()
updateDescription(channel, playlist)
normalizeUrl(channel)
const checkOnline = config.status || config.resolution
const skipChannel =
channel.status &&
ignoreStatus.map(i => i.toLowerCase()).includes(channel.status.toLowerCase())
if (checkOnline && !skipChannel) {
await checker
.checkStream(channel.data)
.then(result => {
const status = parseStatus(result.status)
if (config.status) {
updateStatus(channel, status)
}
if (config.resolution && status === 'online') {
updateResolution(channel, result.status.metadata)
}
if (config.debug && status === 'offline') {
log.print(` ERR: ${channel.url} (${result.status.reason})\n`)
}
})
.catch(err => {
if (config.debug) log.print(` ERR: ${channel.url} (${err.message})\n`)
})
await utils.sleep(config.delay)
if (ignoreStatus.includes(channel.status)) {
continue
}
if (!config.debug) bar.tick()
await checker
.checkStream(channel.data)
.then(parseResult)
.then(result => {
updateStatus(channel, result.status)
if (result.status === 'online') {
buffer[i] = result
updateOrigins(channel, result.requests)
updateResolution(channel, result.resolution)
} else {
buffer[i] = null
if (config.debug) {
if (!bar.complete) log.print(`\n`)
log.print(` INFO: ${channel.url} (${result.error})\n`)
}
}
})
.catch(err => {
buffer[i] = null
if (config.debug) {
if (!bar.complete) log.print(`\n`)
log.print(` ERR: ${channel.data.url} (${err.message})\n`)
}
})
}
for (const [i, channel] of playlist.channels.entries()) {
if (!buffer[i]) continue
const { requests } = buffer[i]
updateUrl(channel, requests)
}
return playlist
}
function updateOrigins(channel, requests) {
if (!requests) return
const origin = new URL(channel.url)
const target = new URL(requests[0])
const type = origin.host === target.host ? 'origin' : 'redirect'
requests.forEach(url => {
const key = utils.removeProtocol(url)
if (!origins[key] && type === 'origin') {
origins[key] = channel.url
}
})
}
function updateStatus(channel, status) {
switch (status) {
case 'online':
channel.status = null
break
case 'offline':
channel.status = 'Offline'
break
}
}
function updateResolution(channel, resolution) {
if (!channel.resolution.height && resolution) {
channel.resolution = resolution
}
}
function updateUrl(channel, requests) {
for (const request of requests) {
let key = utils.removeProtocol(channel.url)
if (origins[key]) {
channel.updateUrl(origins[key])
break
}
key = utils.removeProtocol(request)
if (origins[key]) {
channel.updateUrl(origins[key])
break
}
}
}
function parseResult(result) {
return {
status: parseStatus(result.status),
resolution: result.status.ok ? parseResolution(result.status.metadata.streams) : null,
requests: result.status.ok ? parseRequests(result.status.metadata.requests) : [],
error: !result.status.ok ? result.status.reason : null
}
}
function parseStatus(status) {
if (status.ok) {
return 'online'
@ -114,18 +166,28 @@ function parseStatus(status) {
}
}
function updateStatus(channel, status) {
switch (status) {
case 'online':
channel.status = null
break
case 'offline':
channel.status = 'Offline'
break
}
function parseResolution(streams) {
const resolution = streams
.filter(stream => stream.codec_type === 'video')
.reduce(
(acc, curr) => {
if (curr.height > acc.height) return { width: curr.width, height: curr.height }
return acc
},
{ width: 0, height: 0 }
)
return resolution.width > 0 && resolution.height > 0 ? resolution : null
}
function addMissingData(channel, playlist) {
function parseRequests(requests) {
requests = requests.map(r => r.url)
requests.shift()
return requests
}
function updateDescription(channel, playlist) {
const code = playlist.country.code
// tvg-name
if (!channel.tvg.name && channel.name) {
@ -142,9 +204,7 @@ function addMissingData(channel, playlist) {
channel.countries = name ? [{ code, name }] : []
channel.tvg.country = channel.countries.map(c => c.code.toUpperCase()).join(';')
}
}
function updateGroupTitle(channel) {
// group-title
channel.group.title = channel.category
}
@ -154,14 +214,8 @@ function normalizeUrl(channel) {
channel.updateUrl(decoded)
}
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
})
}
function parseNumber(str) {
return parseInt(str)
}
main()