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...`) console.log(`Parsing index...`)
const index = parseIndex() const index = parseIndex()
for(let item of index.items) { for (let item of index.items) {
console.log(`Processing '${item.url}'...`) console.log(`Processing '${item.url}'...`)
let playlist = parsePlaylist(item.url) let playlist = parsePlaylist(item.url)
if(config.debug) { console.log(`Sorting channels...`) } if (config.debug) {
console.log(`Sorting channels...`)
}
playlist = sortChannels(playlist) playlist = sortChannels(playlist)
if(config.debug) { console.log(`Removing duplicates...`) } if (config.debug) {
console.log(`Removing duplicates...`)
}
playlist = removeDuplicates(playlist) playlist = removeDuplicates(playlist)
if(config.epg) { if (config.epg) {
const tvgUrl = playlist.header.attrs['x-tvg-url'] const tvgUrl = playlist.header.attrs['x-tvg-url']
if(tvgUrl) { if (tvgUrl) {
if(config.debug) { console.log(`Loading EPG from '${tvgUrl}'...`) } if (config.debug) {
console.log(`Loading EPG from '${tvgUrl}'...`)
}
const epg = await loadEPG(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) playlist = addDataFromEPG(playlist, epg)
} else { } 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) updatePlaylist(item.url, playlist)
updated++ updated++
} else { } else {
@ -68,7 +78,9 @@ function parsePlaylist(url) {
function sortChannels(playlist) { function sortChannels(playlist) {
const channels = JSON.stringify(playlist.items) const channels = JSON.stringify(playlist.items)
playlist.items = helper.sortBy(playlist.items, ['title', 'url']) 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 return playlist
} }
@ -78,17 +90,21 @@ function removeDuplicates(playlist) {
const channels = JSON.stringify(playlist.items) const channels = JSON.stringify(playlist.items)
playlist.items = playlist.items.filter(i => { playlist.items = playlist.items.filter(i => {
let result = typeof buffer[i.url] === 'undefined' let result = typeof buffer[i.url] === 'undefined'
if(result) { if (result) {
buffer[i.url] = true buffer[i.url] = true
} else { } 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 return result
}) })
if(channels !== JSON.stringify(playlist.items)) { playlist.changed = true } if (channels !== JSON.stringify(playlist.items)) {
playlist.changed = true
}
return playlist return playlist
} }
@ -96,38 +112,44 @@ function removeDuplicates(playlist) {
async function loadEPG(url) { async function loadEPG(url) {
try { try {
return await helper.parseEPG(url) return await helper.parseEPG(url)
} catch(err) { } catch (err) {
console.error(`Error: could not load '${url}'`) console.error(`Error: could not load '${url}'`)
return return
} }
} }
function addDataFromEPG(playlist, epg) { 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] 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 item.name = channel.name[0].value
playlist.changed = true 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 item.language = channel.name[0].lang
playlist.changed = true 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] item.logo = channel.icon[0]
playlist.changed = true 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) { function updatePlaylist(filepath, playlist) {
helper.createFile(filepath, playlist.getHeader()) helper.createFile(filepath, playlist.getHeader())
for(let channel of playlist.items) { for (let channel of playlist.items) {
helper.appendToFile(filepath, channel.toShortString()) helper.appendToFile(filepath, channel.toShortString())
} }

View File

@ -32,7 +32,11 @@ function main() {
generateLanguages() generateLanguages()
console.log('Done.\n') console.log('Done.\n')
console.log(`Countries: ${Object.values(list.countries).length}. Languages: ${Object.values(list.languages).length}. Categories: ${Object.values(list.categories).length}. Channels: ${list.all.length}.`) console.log(
`Countries: ${Object.values(list.countries).length}. Languages: ${
Object.values(list.languages).length
}. Categories: ${Object.values(list.categories).length}. Channels: ${list.all.length}.`
)
} }
function createRootDirectory() { function createRootDirectory() {
@ -50,12 +54,12 @@ function parseIndex() {
let languages = {} let languages = {}
let categories = {} let categories = {}
for(let rootItem of root.items) { for (let rootItem of root.items) {
const playlist = helper.parsePlaylist(rootItem.url) const playlist = helper.parsePlaylist(rootItem.url)
const countryCode = helper.getBasename(rootItem.url).toLowerCase() const countryCode = helper.getBasename(rootItem.url).toLowerCase()
const countryName = rootItem.name const countryName = rootItem.name
for(let item of playlist.items) { for (let item of playlist.items) {
const channel = helper.createChannel(item) const channel = helper.createChannel(item)
channel.countryCode = countryCode channel.countryCode = countryCode
channel.countryName = countryName channel.countryName = countryName
@ -65,21 +69,21 @@ function parseIndex() {
list.all.push(channel) list.all.push(channel)
// country // country
if(!countries[countryCode]) { if (!countries[countryCode]) {
countries[countryCode] = [] countries[countryCode] = []
} }
countries[countryCode].push(channel) countries[countryCode].push(channel)
// language // language
const languageCode = helper.getISO6391Code(channel.language) || 'undefined' const languageCode = helper.getISO6391Code(channel.language) || 'undefined'
if(!languages[languageCode]) { if (!languages[languageCode]) {
languages[languageCode] = [] languages[languageCode] = []
} }
languages[languageCode].push(channel) languages[languageCode].push(channel)
// category // category
const categoryCode = channel.group.toLowerCase() || 'other' const categoryCode = channel.group.toLowerCase() || 'other'
if(!categories[categoryCode]) { if (!categories[categoryCode]) {
categories[categoryCode] = [] categories[categoryCode] = []
} }
categories[categoryCode].push(channel) categories[categoryCode].push(channel)
@ -96,7 +100,7 @@ function generateIndex() {
helper.createFile(filename, '#EXTM3U\n') helper.createFile(filename, '#EXTM3U\n')
const channels = helper.sortBy(list.all, ['title', 'url']) const channels = helper.sortBy(list.all, ['title', 'url'])
for(let channel of channels) { for (let channel of channels) {
helper.appendToFile(filename, channel.toString()) helper.appendToFile(filename, channel.toString())
} }
} }
@ -106,7 +110,7 @@ function generateCountryIndex() {
helper.createFile(filename, '#EXTM3U\n') helper.createFile(filename, '#EXTM3U\n')
const channels = helper.sortBy(list.all, ['countryName', 'title', 'url']) const channels = helper.sortBy(list.all, ['countryName', 'title', 'url'])
for(let channel of channels) { for (let channel of channels) {
const group = channel.group const group = channel.group
channel.group = channel.countryName channel.group = channel.countryName
helper.appendToFile(filename, channel.toString()) helper.appendToFile(filename, channel.toString())
@ -119,7 +123,7 @@ function generateLanguageIndex() {
helper.createFile(filename, '#EXTM3U\n') helper.createFile(filename, '#EXTM3U\n')
const channels = helper.sortBy(list.all, ['language', 'title', 'url']) const channels = helper.sortBy(list.all, ['language', 'title', 'url'])
for(let channel of channels) { for (let channel of channels) {
const group = channel.group const group = channel.group
channel.group = channel.language channel.group = channel.language
helper.appendToFile(filename, channel.toString()) helper.appendToFile(filename, channel.toString())
@ -132,7 +136,7 @@ function generateCategoryIndex() {
helper.createFile(filename, '#EXTM3U\n') helper.createFile(filename, '#EXTM3U\n')
const channels = helper.sortBy(list.all, ['group', 'title', 'url']) const channels = helper.sortBy(list.all, ['group', 'title', 'url'])
for(let channel of channels) { for (let channel of channels) {
helper.appendToFile(filename, channel.toString()) helper.appendToFile(filename, channel.toString())
} }
} }
@ -141,13 +145,13 @@ function generateCountries() {
const outputDir = `${ROOT_DIR}/countries` const outputDir = `${ROOT_DIR}/countries`
helper.createDir(outputDir) helper.createDir(outputDir)
for(let cid in list.countries) { for (let cid in list.countries) {
let country = list.countries[cid] let country = list.countries[cid]
const filename = `${outputDir}/${cid}.m3u` const filename = `${outputDir}/${cid}.m3u`
helper.createFile(filename, '#EXTM3U\n') helper.createFile(filename, '#EXTM3U\n')
const channels = helper.sortBy(Object.values(country), ['title', 'url']) const channels = helper.sortBy(Object.values(country), ['title', 'url'])
for(let channel of channels) { for (let channel of channels) {
helper.appendToFile(filename, channel.toString()) helper.appendToFile(filename, channel.toString())
} }
} }
@ -157,13 +161,13 @@ function generateCategories() {
const outputDir = `${ROOT_DIR}/categories` const outputDir = `${ROOT_DIR}/categories`
helper.createDir(outputDir) helper.createDir(outputDir)
for(let cid in list.categories) { for (let cid in list.categories) {
let category = list.categories[cid] let category = list.categories[cid]
const filename = `${outputDir}/${cid}.m3u` const filename = `${outputDir}/${cid}.m3u`
helper.createFile(filename, '#EXTM3U\n') helper.createFile(filename, '#EXTM3U\n')
const channels = helper.sortBy(Object.values(category), ['title', 'url']) const channels = helper.sortBy(Object.values(category), ['title', 'url'])
for(let channel of channels) { for (let channel of channels) {
helper.appendToFile(filename, channel.toString()) helper.appendToFile(filename, channel.toString())
} }
} }
@ -173,13 +177,13 @@ function generateLanguages() {
const outputDir = `${ROOT_DIR}/languages` const outputDir = `${ROOT_DIR}/languages`
helper.createDir(outputDir) helper.createDir(outputDir)
for(let lid in list.languages) { for (let lid in list.languages) {
let language = list.languages[lid] let language = list.languages[lid]
const filename = `${outputDir}/${lid}.m3u` const filename = `${outputDir}/${lid}.m3u`
helper.createFile(filename, '#EXTM3U\n') helper.createFile(filename, '#EXTM3U\n')
const channels = helper.sortBy(Object.values(language), ['title', 'url']) const channels = helper.sortBy(Object.values(language), ['title', 'url'])
for(let channel of channels) { for (let channel of channels) {
helper.appendToFile(filename, channel.toString()) helper.appendToFile(filename, channel.toString())
} }
} }

View File

@ -26,22 +26,23 @@ function parseIndex() {
let countries = {} let countries = {}
let languages = {} let languages = {}
let categories = {} let categories = {}
for(let rootItem of root.items) { for (let rootItem of root.items) {
const playlist = helper.parsePlaylist(rootItem.url) const playlist = helper.parsePlaylist(rootItem.url)
const countryName = rootItem.name const countryName = rootItem.name
const countryCode = helper.getBasename(rootItem.url).toLowerCase() const countryCode = helper.getBasename(rootItem.url).toLowerCase()
const countryEpg = playlist.header.attrs['x-tvg-url'] ? `<code>${playlist.header.attrs['x-tvg-url']}</code>` : '' const countryEpg = playlist.header.attrs['x-tvg-url']
? `<code>${playlist.header.attrs['x-tvg-url']}</code>`
: ''
for(let item of playlist.items) { for (let item of playlist.items) {
// countries // countries
if(countries[countryCode]) { if (countries[countryCode]) {
countries[countryCode].channels++ countries[countryCode].channels++
} else { } else {
countries[countryCode] = { countries[countryCode] = {
country: countryName, country: countryName,
channels: 1, channels: 1,
playlist: `<code>https://iptv-org.github.io/iptv/countries/${countryCode}.m3u</code>`, playlist: `<code>https://iptv-org.github.io/iptv/countries/${countryCode}.m3u</code>`,
epg: countryEpg epg: countryEpg
} }
} }
@ -49,26 +50,26 @@ function parseIndex() {
// languages // languages
const languageName = item.tvg.language || 'Undefined' const languageName = item.tvg.language || 'Undefined'
const languageCode = helper.getISO6391Code(languageName) || 'undefined' const languageCode = helper.getISO6391Code(languageName) || 'undefined'
if(languages[languageCode]) { if (languages[languageCode]) {
languages[languageCode].channels++ languages[languageCode].channels++
} else { } else {
languages[languageCode] = { languages[languageCode] = {
language: languageName, language: languageName,
channels: 1, channels: 1,
playlist: `<code>https://iptv-org.github.io/iptv/languages/${languageCode}.m3u</code>` playlist: `<code>https://iptv-org.github.io/iptv/languages/${languageCode}.m3u</code>`
} }
} }
// categories // categories
const categoryName = item.group.title || 'Other' const categoryName = item.group.title || 'Other'
const categoryCode = categoryName.toLowerCase() const categoryCode = categoryName.toLowerCase()
if(categories[categoryCode]) { if (categories[categoryCode]) {
categories[categoryCode].channels++ categories[categoryCode].channels++
} else { } else {
categories[categoryCode] = { categories[categoryCode] = {
category: categoryName, category: categoryName,
channels: 1, channels: 1,
playlist: `<code>https://iptv-org.github.io/iptv/categories/${categoryCode}.m3u</code>` playlist: `<code>https://iptv-org.github.io/iptv/categories/${categoryCode}.m3u</code>`
} }
} }
} }
@ -94,10 +95,18 @@ function generateCountriesTable() {
function generateLanguagesTable() { function generateLanguagesTable() {
output.languages.sort((a, b) => { output.languages.sort((a, b) => {
if(a.language === 'Undefined') { return 1 } if (a.language === 'Undefined') {
if(b.language === 'Undefined') { return -1 } return 1
if(a.language < b.language) { return -1 } }
if(a.language > b.language) { return 1 } if (b.language === 'Undefined') {
return -1
}
if (a.language < b.language) {
return -1
}
if (a.language > b.language) {
return 1
}
return 0 return 0
}) })
@ -114,10 +123,18 @@ function generateLanguagesTable() {
function generateCategoriesTable() { function generateCategoriesTable() {
output.categories.sort((a, b) => { output.categories.sort((a, b) => {
if(a.category === 'Other') { return 1 } if (a.category === 'Other') {
if(b.category === 'Other') { return -1 } return 1
if(a.category < b.category) { return -1 } }
if(a.category > b.category) { return 1 } if (b.category === 'Other') {
return -1
}
if (a.category < b.category) {
return -1
}
if (a.category > b.category) {
return 1
}
return 0 return 0
}) })