Merge pull request #4187 from iptv-org/update-format-js
Update format.js
This commit is contained in:
commit
417d119583
2
.github/workflows/auto-update.yml
vendored
2
.github/workflows/auto-update.yml
vendored
@ -197,7 +197,7 @@ jobs:
|
|||||||
- name: Install Dependencies
|
- name: Install Dependencies
|
||||||
run: npm install
|
run: npm install
|
||||||
- name: Format Playlists
|
- name: Format Playlists
|
||||||
run: node scripts/format.js --country=${{ matrix.country }} --status --resolution --debug
|
run: node scripts/format.js --country=${{ matrix.country }} --debug
|
||||||
- name: Upload Artifact
|
- name: Upload Artifact
|
||||||
uses: actions/upload-artifact@v2
|
uses: actions/upload-artifact@v2
|
||||||
with:
|
with:
|
||||||
|
14
package-lock.json
generated
14
package-lock.json
generated
@ -8,7 +8,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@freearhey/iso-639-3": "^1.0.0",
|
"@freearhey/iso-639-3": "^1.0.0",
|
||||||
"commander": "^7.0.0",
|
"commander": "^7.0.0",
|
||||||
"iptv-checker": "^0.20.2",
|
"iptv-checker": "^0.21.0",
|
||||||
"iptv-playlist-parser": "^0.5.4",
|
"iptv-playlist-parser": "^0.5.4",
|
||||||
"m3u-linter": "^0.2.1",
|
"m3u-linter": "^0.2.1",
|
||||||
"markdown-include": "^0.4.3",
|
"markdown-include": "^0.4.3",
|
||||||
@ -1904,9 +1904,9 @@
|
|||||||
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
|
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
|
||||||
},
|
},
|
||||||
"node_modules/iptv-checker": {
|
"node_modules/iptv-checker": {
|
||||||
"version": "0.20.2",
|
"version": "0.21.0",
|
||||||
"resolved": "https://registry.npmjs.org/iptv-checker/-/iptv-checker-0.20.2.tgz",
|
"resolved": "https://registry.npmjs.org/iptv-checker/-/iptv-checker-0.21.0.tgz",
|
||||||
"integrity": "sha512-3lzN3q1l3TApHvsP6Rt4ALrs8PE+Zb2gmuykwPzQNtlfazIUR73Yeq/ypwpqRwYJxSq5WMoEdQHwV4KcR7y5Ig==",
|
"integrity": "sha512-qWqq8mLl+qRgV0/fmBpZm8/Cf7FwSnIA1gt9PFlYbzilRDFtPoTRDm0eP9fCWHVwwZZsImE1aMLx8qkikL5Aog==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^0.21.1",
|
"axios": "^0.21.1",
|
||||||
"colors": "^1.4.0",
|
"colors": "^1.4.0",
|
||||||
@ -5252,9 +5252,9 @@
|
|||||||
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
|
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
|
||||||
},
|
},
|
||||||
"iptv-checker": {
|
"iptv-checker": {
|
||||||
"version": "0.20.2",
|
"version": "0.21.0",
|
||||||
"resolved": "https://registry.npmjs.org/iptv-checker/-/iptv-checker-0.20.2.tgz",
|
"resolved": "https://registry.npmjs.org/iptv-checker/-/iptv-checker-0.21.0.tgz",
|
||||||
"integrity": "sha512-3lzN3q1l3TApHvsP6Rt4ALrs8PE+Zb2gmuykwPzQNtlfazIUR73Yeq/ypwpqRwYJxSq5WMoEdQHwV4KcR7y5Ig==",
|
"integrity": "sha512-qWqq8mLl+qRgV0/fmBpZm8/Cf7FwSnIA1gt9PFlYbzilRDFtPoTRDm0eP9fCWHVwwZZsImE1aMLx8qkikL5Aog==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"axios": "^0.21.1",
|
"axios": "^0.21.1",
|
||||||
"colors": "^1.4.0",
|
"colors": "^1.4.0",
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@freearhey/iso-639-3": "^1.0.0",
|
"@freearhey/iso-639-3": "^1.0.0",
|
||||||
"commander": "^7.0.0",
|
"commander": "^7.0.0",
|
||||||
"iptv-checker": "^0.20.2",
|
"iptv-checker": "^0.21.0",
|
||||||
"iptv-playlist-parser": "^0.5.4",
|
"iptv-playlist-parser": "^0.5.4",
|
||||||
"m3u-linter": "^0.2.1",
|
"m3u-linter": "^0.2.1",
|
||||||
"markdown-include": "^0.4.3",
|
"markdown-include": "^0.4.3",
|
||||||
|
@ -1,105 +1,153 @@
|
|||||||
const IPTVChecker = require('iptv-checker')
|
const IPTVChecker = require('iptv-checker')
|
||||||
const normalize = require('normalize-url')
|
const normalize = require('normalize-url')
|
||||||
const { program } = require('commander')
|
const { program } = require('commander')
|
||||||
const ProgressBar = require('progress')
|
|
||||||
const parser = require('./helpers/parser')
|
const parser = require('./helpers/parser')
|
||||||
const utils = require('./helpers/utils')
|
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 total = playlist.channels.length
|
||||||
|
log.print(`Processing '${playlist.url}'...\n`)
|
||||||
|
|
||||||
|
buffer = {}
|
||||||
|
origins = {}
|
||||||
|
for (const [i, channel] of playlist.channels.entries()) {
|
||||||
|
const curr = i + 1
|
||||||
|
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) {
|
||||||
|
log.print(` INFO: ${channel.url} (${result.error})\n`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
buffer[i] = null
|
||||||
|
if (config.debug) {
|
||||||
|
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 +162,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 +200,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 +210,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()
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
const log = {}
|
const log = {}
|
||||||
|
|
||||||
log.print = function (string) {
|
log.print = function (message) {
|
||||||
process.stdout.write(string)
|
if (typeof message === 'object') message = JSON.stringify(message, null, 2)
|
||||||
|
process.stdout.write(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.start = function () {
|
log.start = function () {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user