Update format.js

This commit is contained in:
Aleksandr Statciuk 2021-08-02 21:12:27 +03:00
parent 8dd15f08f5
commit 573174c79d

View File

@ -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()