You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iptv/scripts/format.js

164 lines
4.7 KiB
JavaScript

const IPTVChecker = require('iptv-checker')
const normalize = require('normalize-url')
const { program } = require('commander')
const ProgressBar = require('progress')
3 years ago
const parser = require('./helpers/parser')
const utils = require('./helpers/utils')
3 years ago
const file = require('./helpers/file')
3 years ago
const log = require('./helpers/log')
6 years ago
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
})
5 years ago
async function main() {
3 years ago
log.start()
4 years ago
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`)
3 years ago
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`)
4 years ago
for (const playlist of playlists) {
3 years ago
await parser
.parsePlaylist(playlist.url)
.then(updatePlaylist)
3 years ago
.then(playlist => {
if (file.read(playlist.url) !== playlist.toString()) {
log.print(`File '${playlist.url}' has been updated\n`)
3 years ago
playlist.updated = true
}
playlist.save()
})
5 years ago
}
3 years ago
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`)
}
3 years ago
for (const channel of playlist.channels) {
addMissingData(channel)
updateGroupTitle(channel)
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`)
})
}
if (!config.debug) bar.tick()
3 years ago
}
return playlist
}
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
}
}
function addMissingData(channel) {
// tvg-name
if (!channel.tvg.name && channel.name) {
channel.tvg.name = channel.name.replace(/\"/gi, '')
}
// tvg-id
if (!channel.tvg.id && channel.tvg.name) {
const id = utils.name2id(channel.tvg.name)
channel.tvg.id = id ? `${id}.${code}` : ''
}
// 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(';')
}
}
function updateGroupTitle(channel) {
channel.group.title = channel.category
}
function normalizeUrl(channel) {
const normalized = normalize(channel.url, { stripWWW: false })
3 years ago
const decoded = decodeURIComponent(normalized).replace(/\s/g, '+')
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
})
}
}
5 years ago
main()