iptv/scripts/format.js

66 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-01-17 16:00:52 +01:00
const { program } = require('commander')
2021-01-30 00:26:20 +01:00
const parser = require('./parser')
const utils = require('./utils')
2019-04-30 15:52:38 +02:00
2021-01-17 16:00:52 +01:00
program
.usage('[OPTIONS]...')
.option('-d, --debug', 'Debug mode')
2021-01-19 23:02:40 +01:00
.option('-c, --country <country>', 'Comma-separated list of country codes', '')
.option('-e, --exclude <exclude>', 'Comma-separated list of country codes to be excluded', '')
2021-01-17 16:00:52 +01:00
.parse(process.argv)
const config = program.opts()
2019-07-20 09:06:29 +02:00
2019-11-03 14:03:35 +01:00
async function main() {
2021-08-01 05:25:04 +02:00
utils.log('Starting...\n')
console.time('\nDone in')
2021-05-07 11:51:57 +02:00
2021-01-30 00:26:20 +01:00
const playlists = parseIndex()
for (const playlist of playlists) {
2021-08-01 05:25:04 +02:00
await loadPlaylist(playlist.url).then(sortChannels).then(savePlaylist)
2019-11-03 14:03:35 +01:00
}
finish()
2019-11-03 14:03:35 +01:00
}
2019-11-03 08:56:54 +01:00
function parseIndex() {
2021-08-01 05:25:04 +02:00
utils.log(`Parsing 'index.m3u'...`)
2021-01-30 00:26:20 +01:00
let playlists = parser.parseIndex()
playlists = utils
.filterPlaylists(playlists, config.country, config.exclude)
.filter(i => i.url !== 'channels/unsorted.m3u')
2019-11-03 17:01:04 +01:00
2021-01-30 00:26:20 +01:00
return playlists
2019-11-03 14:03:35 +01:00
}
async function loadPlaylist(url) {
2021-08-01 05:25:04 +02:00
utils.log(`\nProcessing '${url}'...`)
2021-01-30 00:26:20 +01:00
return parser.parsePlaylist(url)
}
async function sortChannels(playlist) {
2021-01-30 00:26:20 +01:00
playlist.channels = utils.sortBy(playlist.channels, ['name', 'url'])
2019-11-03 17:01:04 +01:00
return playlist
}
2019-11-03 14:03:35 +01:00
2021-01-30 05:18:06 +01:00
async function savePlaylist(playlist) {
2021-01-30 00:26:20 +01:00
const original = utils.readFile(playlist.url)
2021-05-06 15:25:00 +02:00
const output = playlist.toString()
if (original === output) {
return false
} else {
2021-01-30 00:26:20 +01:00
utils.createFile(playlist.url, output)
2021-08-01 05:25:04 +02:00
utils.log(`updated`)
2020-04-22 15:41:54 +02:00
}
return true
}
function finish() {
2021-08-01 05:25:04 +02:00
console.timeEnd('\nDone in')
2020-04-22 15:41:54 +02:00
}
2019-11-03 08:56:54 +01:00
main()