Update format.js
This commit is contained in:
parent
572ca9953c
commit
9d74ff3095
|
@ -7,99 +7,151 @@ const utils = require('./helpers/utils')
|
||||||
const file = require('./helpers/file')
|
const file = require('./helpers/file')
|
||||||
const log = require('./helpers/log')
|
const log = require('./helpers/log')
|
||||||
|
|
||||||
|
const ignoreStatus = ['Geo-blocked', 'Not 24/7']
|
||||||
|
|
||||||
program
|
program
|
||||||
.usage('[OPTIONS]...')
|
.usage('[OPTIONS]...')
|
||||||
.option('-d, --debug', 'Enable debug mode')
|
.option('--debug', 'Enable debug mode')
|
||||||
.option('-s, --status', 'Update stream status')
|
.option('-d, --delay <delay>', 'Set delay for each request', parseNumber, 0)
|
||||||
.option('-r, --resolution', 'Detect stream resolution')
|
.option('-t, --timeout <timeout>', 'Set timeout for each request', parseNumber, 5000)
|
||||||
.option('-c, --country <country>', 'Comma-separated list of country codes', '')
|
.option('-c, --country <country>', 'Comma-separated list of country codes', '')
|
||||||
.option('-e, --exclude <exclude>', 'Comma-separated list of country codes to be excluded', '')
|
.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)
|
.parse(process.argv)
|
||||||
|
|
||||||
let bar
|
|
||||||
const ignoreStatus = ['Geo-blocked', 'Not 24/7']
|
|
||||||
const config = program.opts()
|
const config = program.opts()
|
||||||
const checker = new IPTVChecker({
|
const checker = new IPTVChecker({
|
||||||
timeout: config.timeout
|
timeout: config.timeout
|
||||||
})
|
})
|
||||||
|
|
||||||
|
let buffer, origins
|
||||||
async function main() {
|
async function main() {
|
||||||
log.start()
|
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')
|
let playlists = parser.parseIndex().filter(i => i.url !== 'channels/unsorted.m3u')
|
||||||
playlists = utils.filterPlaylists(playlists, config.country, config.exclude)
|
playlists = utils.filterPlaylists(playlists, config.country, config.exclude)
|
||||||
if (!playlists.length) log.print(`No playlist is selected\n`)
|
if (!playlists.length) log.print(`No playlist is selected\n`)
|
||||||
for (const playlist of playlists) {
|
for (const playlist of playlists) {
|
||||||
await parser
|
await parser.parsePlaylist(playlist.url).then(updatePlaylist).then(savePlaylist)
|
||||||
.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()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.finish()
|
log.finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updatePlaylist(playlist) {
|
function savePlaylist(playlist) {
|
||||||
if (!config.debug) {
|
if (file.read(playlist.url) !== playlist.toString()) {
|
||||||
bar = new ProgressBar(`Processing '${playlist.url}': [:bar] :current/:total (:percent) `, {
|
log.print(`File '${playlist.url}' has been updated\n`)
|
||||||
total: playlist.channels.length
|
playlist.updated = true
|
||||||
})
|
|
||||||
} else {
|
|
||||||
log.print(`Processing '${playlist.url}'...\n`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const channel of playlist.channels) {
|
playlist.save()
|
||||||
addMissingData(channel, playlist)
|
}
|
||||||
updateGroupTitle(channel)
|
|
||||||
|
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)
|
normalizeUrl(channel)
|
||||||
|
|
||||||
const checkOnline = config.status || config.resolution
|
if (ignoreStatus.includes(channel.status)) {
|
||||||
const skipChannel =
|
continue
|
||||||
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 (!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
|
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) {
|
function parseStatus(status) {
|
||||||
if (status.ok) {
|
if (status.ok) {
|
||||||
return 'online'
|
return 'online'
|
||||||
|
@ -114,18 +166,28 @@ function parseStatus(status) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateStatus(channel, status) {
|
function parseResolution(streams) {
|
||||||
switch (status) {
|
const resolution = streams
|
||||||
case 'online':
|
.filter(stream => stream.codec_type === 'video')
|
||||||
channel.status = null
|
.reduce(
|
||||||
break
|
(acc, curr) => {
|
||||||
case 'offline':
|
if (curr.height > acc.height) return { width: curr.width, height: curr.height }
|
||||||
channel.status = 'Offline'
|
return acc
|
||||||
break
|
},
|
||||||
}
|
{ 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
|
const code = playlist.country.code
|
||||||
// tvg-name
|
// tvg-name
|
||||||
if (!channel.tvg.name && channel.name) {
|
if (!channel.tvg.name && channel.name) {
|
||||||
|
@ -142,9 +204,7 @@ function addMissingData(channel, playlist) {
|
||||||
channel.countries = name ? [{ code, name }] : []
|
channel.countries = name ? [{ code, name }] : []
|
||||||
channel.tvg.country = channel.countries.map(c => c.code.toUpperCase()).join(';')
|
channel.tvg.country = channel.countries.map(c => c.code.toUpperCase()).join(';')
|
||||||
}
|
}
|
||||||
}
|
// group-title
|
||||||
|
|
||||||
function updateGroupTitle(channel) {
|
|
||||||
channel.group.title = channel.category
|
channel.group.title = channel.category
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,14 +214,8 @@ function normalizeUrl(channel) {
|
||||||
channel.updateUrl(decoded)
|
channel.updateUrl(decoded)
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateResolution(channel, metadata) {
|
function parseNumber(str) {
|
||||||
const streams = metadata ? metadata.streams.filter(stream => stream.codec_type === 'video') : []
|
return parseInt(str)
|
||||||
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()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue