iptv/scripts/format.js

305 lines
7.9 KiB
JavaScript
Raw Normal View History

2021-09-19 09:44:07 +02:00
const axios = require('axios')
const { program } = require('commander')
2021-09-19 09:44:07 +02:00
const normalize = require('normalize-url')
const IPTVChecker = require('iptv-checker')
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')
2021-09-19 10:20:50 +02:00
const epg = require('./helpers/epg')
2019-07-20 09:06:29 +02:00
2021-08-18 15:39:26 +02:00
const ignoreStatus = ['Geo-blocked', 'Not 24/7']
program
.usage('[OPTIONS]...')
2021-08-18 15:39:26 +02:00
.option('--debug', 'Enable debug mode')
2021-08-28 15:09:45 +02:00
.option('--offline', 'Enable offline mode')
2021-08-18 15:39:26 +02:00
.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', '')
.parse(process.argv)
const config = program.opts()
const checker = new IPTVChecker({
timeout: config.timeout
})
2021-08-18 15:39:26 +02:00
let buffer, origins
2021-09-08 19:46:27 +02:00
async function main() {
2021-08-01 07:25:39 +02:00
log.start()
2021-05-07 11:51:57 +02:00
2021-09-08 19:46:27 +02:00
const include = config.country.split(',').filter(i => i)
const exclude = config.exclude.split(',').filter(i => i)
let files = await file.list(include, exclude)
if (!files.length) log.print(`No files is selected\n`)
for (const file of files) {
2021-09-08 19:03:08 +02:00
await parser.parsePlaylist(file).then(updatePlaylist).then(savePlaylist)
2019-11-03 14:03:35 +01:00
}
2021-08-01 07:25:39 +02:00
log.finish()
}
2021-08-18 15:39:26 +02:00
function savePlaylist(playlist) {
if (file.read(playlist.url) !== playlist.toString()) {
log.print(`File '${playlist.url}' has been updated\n`)
playlist.updated = true
}
2021-08-18 15:39:26 +02:00
playlist.save()
}
async function updatePlaylist(playlist) {
const total = playlist.channels.length
log.print(`Processing '${playlist.url}'...\n`)
2021-08-18 15:39:26 +02:00
2021-09-19 10:29:39 +02:00
let channels = {}
let codes = {}
if (!config.offline) {
2021-09-19 14:08:10 +02:00
channels = await loadChannelsJson()
2021-09-19 10:29:39 +02:00
codes = await loadCodes()
}
2021-08-18 15:39:26 +02:00
buffer = {}
origins = {}
for (const [i, channel] of playlist.channels.entries()) {
const curr = i + 1
2021-09-19 09:44:07 +02:00
updateTvgName(channel)
updateTvgId(channel, playlist)
2021-10-23 05:40:30 +02:00
updateTvgCountry(channel)
normalizeUrl(channel)
2021-09-19 09:44:07 +02:00
const data = channels[channel.tvg.id]
2021-09-19 10:20:50 +02:00
const epgData = codes[channel.tvg.id]
updateLogo(channel, data, epgData)
updateGroupTitle(channel, data)
updateTvgLanguage(channel, data)
2021-09-19 09:44:07 +02:00
2021-08-28 15:09:45 +02:00
if (config.offline || ignoreStatus.includes(channel.status)) {
2021-08-18 15:39:26 +02:00
continue
}
2021-08-18 15:39:26 +02:00
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) {
log.print(` INFO: ${channel.url} (${result.error})\n`)
}
2021-08-18 15:39:26 +02:00
}
})
.catch(err => {
buffer[i] = null
if (config.debug) {
log.print(` ERR: ${channel.data.url} (${err.message})\n`)
}
})
}
2021-08-18 15:39:26 +02:00
for (const [i, channel] of playlist.channels.entries()) {
if (!buffer[i]) continue
const { requests } = buffer[i]
updateUrl(channel, requests)
}
return playlist
}
2021-08-16 12:07:27 +02:00
2021-08-18 15:39:26 +02:00
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
2021-08-10 22:45:32 +02:00
}
2021-08-18 15:39:26 +02:00
})
}
function updateStatus(channel, status) {
switch (status) {
case 'online':
2021-08-22 21:26:01 +02:00
channel.status = channel.status === 'Offline' ? 'Not 24/7' : null
2021-08-18 15:39:26 +02:00
break
case 'offline':
2021-09-18 12:28:24 +02:00
case 'error_403':
2021-08-18 15:39:26 +02:00
channel.status = 'Offline'
break
2021-08-02 20:12:27 +02:00
}
2021-08-18 15:39:26 +02:00
}
2021-08-02 20:12:27 +02:00
2021-08-18 15:39:26 +02:00
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
}
2021-08-02 20:12:27 +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'
}
}
2021-08-18 15:39:26 +02:00
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 parseRequests(requests) {
requests = requests.map(r => r.url)
requests.shift()
return requests
}
2021-09-19 09:44:07 +02:00
function updateTvgName(channel) {
if (!channel.tvg.name) {
channel.tvg.name = channel.name.replace(/\"/gi, '')
}
}
function updateTvgId(channel, playlist) {
const code = playlist.country.code
if (!channel.tvg.id && channel.tvg.name) {
const id = utils.name2id(channel.tvg.name)
channel.tvg.id = id ? `${id}.${code}` : ''
}
2021-09-19 09:44:07 +02:00
}
2021-10-23 05:40:30 +02:00
function updateTvgCountry(channel) {
if (!channel.countries.length && channel.tvg.id) {
const code = channel.tvg.id.split('.')[1] || null
const name = utils.code2name(code)
channel.countries = name ? [{ code, name }] : []
channel.tvg.country = channel.countries.map(c => c.code.toUpperCase()).join(';')
}
2021-09-19 09:44:07 +02:00
}
2021-09-19 10:20:50 +02:00
function updateLogo(channel, data, epgData) {
2021-09-19 09:44:07 +02:00
if (!channel.logo) {
2021-10-27 01:58:10 +02:00
if (data && data.logo) {
2021-09-19 10:20:50 +02:00
channel.logo = data.logo
2021-10-27 01:58:10 +02:00
} else if (epgData && epgData.logo) {
2021-09-19 10:20:50 +02:00
channel.logo = epgData.logo
}
2021-09-19 09:44:07 +02:00
}
}
function updateTvgLanguage(channel, data) {
2021-10-23 05:25:53 +02:00
if (!channel.tvg.language) {
2021-10-27 01:58:10 +02:00
if (data && data.languages.length) {
2021-10-23 05:25:53 +02:00
channel.tvg.language = data.languages.map(l => l.name).join(';')
} else if (channel.countries.length) {
const countryCode = channel.countries[0].code
channel.tvg.language = utils.country2language(countryCode)
}
2021-09-19 09:44:07 +02:00
}
}
function updateGroupTitle(channel, data) {
2021-10-27 01:58:10 +02:00
if (!channel.group.title) {
if (channel.category) {
channel.group.title = channel.category
} else if (data && data.category) {
channel.group.title = data.category
}
2021-09-19 09:44:07 +02:00
}
}
function normalizeUrl(channel) {
const normalized = normalize(channel.url, { stripWWW: false })
2021-08-11 18:35:28 +02:00
const decoded = decodeURIComponent(normalized).replace(/\s/g, '+')
channel.updateUrl(decoded)
}
2021-08-18 15:39:26 +02:00
function parseNumber(str) {
return parseInt(str)
}
2021-09-19 10:29:39 +02:00
function loadCodes() {
return epg.codes
.load()
.then(codes => {
let output = {}
codes.forEach(item => {
output[item['tvg_id']] = item
})
return output
})
.catch(console.log)
}
2021-09-19 14:08:10 +02:00
function loadChannelsJson() {
2021-09-19 10:29:39 +02:00
return axios
.get('https://iptv-org.github.io/iptv/channels.json')
.then(r => r.data)
.then(channels => {
let output = {}
channels.forEach(channel => {
const item = output[channel.tvg.id]
if (!item) {
output[channel.tvg.id] = channel
} else {
item.logo = item.logo || channel.logo
item.languages = item.languages.length ? item.languages : channel.languages
item.category = item.category || channel.category
}
})
return output
})
.catch(console.log)
}
2019-11-03 08:56:54 +01:00
main()