iptv/scripts/format.js

152 lines
4.4 KiB
JavaScript
Raw Normal View History

2021-08-10 02:13:05 +02:00
const IPTVChecker = require('iptv-checker')
const { program } = require('commander')
const ProgressBar = require('progress')
2021-08-01 18:52:47 +02:00
const parser = require('./helpers/parser')
const utils = require('./helpers/utils')
2021-08-02 20:12:27 +02:00
const file = require('./helpers/file')
2021-08-01 18:52:47 +02:00
const log = require('./helpers/log')
2019-07-20 09:06:29 +02:00
2021-08-10 02:13:05 +02:00
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
})
2019-11-03 14:03:35 +01:00
async function main() {
2021-08-01 07:25:39 +02:00
log.start()
2021-05-07 11:51:57 +02:00
2021-08-10 02:13:05 +02:00
if (config.debug) log.print(`Debug mode enabled\n`)
2021-08-10 11:50:23 +02:00
if (config.status) log.print(`Status check enabled\n`)
if (config.resolution) log.print(`Resolution detection enabled\n`)
2021-08-10 02:13:05 +02:00
2021-08-01 07:25:39 +02:00
let playlists = parser.parseIndex().filter(i => i.url !== 'channels/unsorted.m3u')
2021-08-10 02:13:05 +02:00
playlists = utils.filterPlaylists(playlists, config.country, config.exclude)
2021-08-10 06:12:25 +02:00
if (!playlists.length) log.print(`No playlist is selected\n`)
2021-01-30 00:26:20 +01:00
for (const playlist of playlists) {
2021-08-01 18:52:47 +02:00
await parser
.parsePlaylist(playlist.url)
2021-08-10 02:13:05 +02:00
.then(updatePlaylist)
2021-08-02 20:12:27 +02:00
.then(playlist => {
if (file.read(playlist.url) !== playlist.toString()) {
2021-08-10 02:13:05 +02:00
log.print(`File '${playlist.url}' has been updated\n`)
2021-08-02 20:12:27 +02:00
playlist.updated = true
}
playlist.save()
})
2019-11-03 14:03:35 +01:00
}
2021-08-01 07:25:39 +02:00
log.finish()
}
2021-08-10 02:13:05 +02:00
async function updatePlaylist(playlist) {
if (!config.debug) {
bar = new ProgressBar(`Processing '${playlist.url}': [:bar] :current/:total (:percent) `, {
total: playlist.channels.length
})
2021-08-10 02:39:05 +02:00
} else {
log.print(`Processing '${playlist.url}'...\n`)
2021-08-10 02:13:05 +02:00
}
2021-08-02 20:12:27 +02:00
for (const channel of playlist.channels) {
2021-08-10 02:13:05 +02:00
addMissingData(channel)
const checkOnline = config.status || config.resolution
2021-08-10 11:52:11 +02:00
const skipChannel =
2021-08-10 02:13:05 +02:00
channel.status &&
ignoreStatus.map(i => i.toLowerCase()).includes(channel.status.toLowerCase())
2021-08-10 11:52:11 +02:00
if (checkOnline && !skipChannel) {
2021-08-10 02:13:05 +02:00
await checker
.checkStream(channel.data)
.then(result => {
2021-08-10 11:50:23 +02:00
const status = parseStatus(result.status)
if (config.status) {
updateStatus(channel, status)
}
if (config.resolution && status === 'online') {
2021-08-10 04:47:12 +02:00
updateResolution(channel, result.status.metadata)
2021-08-10 11:50:23 +02:00
}
if (config.debug && status === 'offline') {
log.print(` ERR: ${channel.url} (${result.status.reason})\n`)
}
2021-08-10 02:13:05 +02:00
})
.catch(err => {
2021-08-10 02:47:11 +02:00
if (config.debug) log.print(` ERR: ${channel.url} (${err.message})\n`)
2021-08-10 02:13:05 +02:00
})
2021-08-02 20:12:27 +02:00
}
2021-08-10 02:13:05 +02:00
if (!config.debug) bar.tick()
2021-08-02 20:12:27 +02:00
}
return playlist
}
2021-08-10 11:50:23 +02:00
function parseStatus(status) {
if (status.ok) {
return 'online'
} else if (status.reason.includes('timed out')) {
return 'timeout'
} else if (status.reason.includes('403')) {
return 'error_403'
} else if (status.reason.includes('not one of 40{0,1,3,4}')) {
return 'error_40x' // 402, 451
} else {
return 'offline'
}
}
function updateStatus(channel, status) {
switch (status) {
case 'online':
channel.status = null
break
case 'offline':
channel.status = 'Offline'
break
}
}
2021-08-10 02:13:05 +02:00
function addMissingData(channel) {
// add tvg-name
if (!channel.tvg.name && channel.name) {
channel.tvg.name = channel.name.replace(/\"/gi, '')
}
// add tvg-id
if (!channel.tvg.id && channel.tvg.name) {
const id = utils.name2id(channel.tvg.name)
channel.tvg.id = id ? `${id}.${code}` : ''
}
// add country
if (!channel.countries.length) {
const name = utils.code2name(code)
channel.countries = name ? [{ code, name }] : []
channel.tvg.country = channel.countries.map(c => c.code.toUpperCase()).join(';')
}
// update group-title
channel.group.title = channel.category
}
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
})
}
}
2019-11-03 08:56:54 +01:00
main()