From 573174c79d732f9222549c80e6042676af70a15d Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Mon, 2 Aug 2021 21:12:27 +0300 Subject: [PATCH] Update format.js --- scripts/format.js | 59 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/scripts/format.js b/scripts/format.js index 0fa233f3f..58f24b4f6 100644 --- a/scripts/format.js +++ b/scripts/format.js @@ -1,5 +1,7 @@ +const categories = require('./helpers/categories') const parser = require('./helpers/parser') const utils = require('./helpers/utils') +const file = require('./helpers/file') const log = require('./helpers/log') async function main() { @@ -11,25 +13,64 @@ async function main() { log.print(`\nProcessing '${playlist.url}'...`) await parser .parsePlaylist(playlist.url) + .then(removeWrongCategories) + .then(addMissingData) .then(sortChannels) - .then(p => p.save()) + .then(playlist => { + if (file.read(playlist.url) !== playlist.toString()) { + log.print('updated') + playlist.updated = true + } + + playlist.save() + }) } log.print('\n') log.finish() } -async function sortChannels(playlist) { - const channels = [...playlist.channels] - utils.sortBy(channels, ['name', 'url']) - - if (JSON.stringify(playlist.channels) !== JSON.stringify(channels)) { - log.print('updated') - playlist.channels = channels - playlist.updated = true +async function removeWrongCategories(playlist) { + for (const channel of playlist.channels) { + if (!channel.category) continue + const valid = categories.find(c => c.name === channel.category) + if (!valid) { + channel.category = '' + } } return playlist } +async function addMissingData(playlist) { + for (const channel of playlist.channels) { + const code = file.getBasename(playlist.url) + // tvg-name + if (!channel.tvg.name && code !== 'unsorted' && channel.name) { + channel.tvg.name = channel.name.replace(/\"/gi, '') + } + // tvg-id + if (!channel.tvg.id && code !== 'unsorted' && 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(';') + } + } + + return playlist +} + +async function sortChannels(playlist) { + const channels = [...playlist.channels] + utils.sortBy(channels, ['name', 'url']) + playlist.channels = channels + + return playlist +} + main()