Make code prettier

This commit is contained in:
freearhey 2020-04-11 04:34:50 +03:00
parent d9bef97c88
commit 97f0454d20
3 changed files with 115 additions and 72 deletions

View file

@ -13,27 +13,37 @@ async function main() {
console.log(`Parsing index...`)
const index = parseIndex()
for(let item of index.items) {
for (let item of index.items) {
console.log(`Processing '${item.url}'...`)
let playlist = parsePlaylist(item.url)
if(config.debug) { console.log(`Sorting channels...`) }
if (config.debug) {
console.log(`Sorting channels...`)
}
playlist = sortChannels(playlist)
if(config.debug) { console.log(`Removing duplicates...`) }
if (config.debug) {
console.log(`Removing duplicates...`)
}
playlist = removeDuplicates(playlist)
if(config.epg) {
if (config.epg) {
const tvgUrl = playlist.header.attrs['x-tvg-url']
if(tvgUrl) {
if(config.debug) { console.log(`Loading EPG from '${tvgUrl}'...`) }
if (tvgUrl) {
if (config.debug) {
console.log(`Loading EPG from '${tvgUrl}'...`)
}
const epg = await loadEPG(tvgUrl)
if(config.debug) { console.log(`Adding the missing data from EPG...`) }
if (config.debug) {
console.log(`Adding the missing data from EPG...`)
}
playlist = addDataFromEPG(playlist, epg)
} else {
if(config.debug) { console.log(`EPG source is not found`) }
if (config.debug) {
console.log(`EPG source is not found`)
}
}
}
if(playlist.changed) {
if (playlist.changed) {
updatePlaylist(item.url, playlist)
updated++
} else {
@ -68,7 +78,9 @@ function parsePlaylist(url) {
function sortChannels(playlist) {
const channels = JSON.stringify(playlist.items)
playlist.items = helper.sortBy(playlist.items, ['title', 'url'])
if(channels !== JSON.stringify(playlist.items)) { playlist.changed = true }
if (channels !== JSON.stringify(playlist.items)) {
playlist.changed = true
}
return playlist
}
@ -78,17 +90,21 @@ function removeDuplicates(playlist) {
const channels = JSON.stringify(playlist.items)
playlist.items = playlist.items.filter(i => {
let result = typeof buffer[i.url] === 'undefined'
if(result) {
if (result) {
buffer[i.url] = true
} else {
if(config.debug) { console.log(`Duplicate of '${i.title}' has been removed`) }
if (config.debug) {
console.log(`Duplicate of '${i.title}' has been removed`)
}
}
return result
})
if(channels !== JSON.stringify(playlist.items)) { playlist.changed = true }
if (channels !== JSON.stringify(playlist.items)) {
playlist.changed = true
}
return playlist
}
@ -96,38 +112,44 @@ function removeDuplicates(playlist) {
async function loadEPG(url) {
try {
return await helper.parseEPG(url)
} catch(err) {
} catch (err) {
console.error(`Error: could not load '${url}'`)
return
}
}
function addDataFromEPG(playlist, epg) {
if(!epg) return playlist
if (!epg) return playlist
for (let item of playlist.items) {
if (!item.id) continue
for(let item of playlist.items) {
if(!item.id) continue
const channel = epg.channels[item.id]
if(!channel) continue
if (!channel) continue
if(!item.name && channel.name.length) {
if (!item.name && channel.name.length) {
item.name = channel.name[0].value
playlist.changed = true
if(config.debug) { console.log(`Added tvg-name '${item.name}' to '${item.title}'`) }
if (config.debug) {
console.log(`Added tvg-name '${item.name}' to '${item.title}'`)
}
}
if(!item.language && channel.name.length && channel.name[0].lang) {
if (!item.language && channel.name.length && channel.name[0].lang) {
item.language = channel.name[0].lang
playlist.changed = true
if(config.debug) { console.log(`Added tvg-language '${item.language}' to '${item.title}'`) }
if (config.debug) {
console.log(`Added tvg-language '${item.language}' to '${item.title}'`)
}
}
if(!item.logo && channel.icon.length) {
if (!item.logo && channel.icon.length) {
item.logo = channel.icon[0]
playlist.changed = true
if(config.debug) { console.log(`Added tvg-logo '${item.logo}' to '${item.title}'`) }
if (config.debug) {
console.log(`Added tvg-logo '${item.logo}' to '${item.title}'`)
}
}
}
@ -136,7 +158,7 @@ function addDataFromEPG(playlist, epg) {
function updatePlaylist(filepath, playlist) {
helper.createFile(filepath, playlist.getHeader())
for(let channel of playlist.items) {
for (let channel of playlist.items) {
helper.appendToFile(filepath, channel.toShortString())
}