Update generate.js

This commit is contained in:
freearhey 2021-02-10 11:57:56 +03:00
parent 9d5a031c1f
commit 37845bb0da
1 changed files with 61 additions and 27 deletions

View File

@ -6,9 +6,10 @@ const ROOT_DIR = './.gh-pages'
let list = { let list = {
all: [], all: [],
countries: {}, playlists: {},
languages: {}, languages: {},
categories: {} categories: {},
countries: {}
} }
function main() { function main() {
@ -52,30 +53,47 @@ function parseIndex() {
// all // all
list.all.push(channel) list.all.push(channel)
// country // playlists
if (!channel.countries.length) { if (!channel.countries.length) {
const countryCode = 'undefined' const UNDEFINED = 'undefined'
if (!list.countries[countryCode]) { if (!list.playlists[UNDEFINED]) {
list.countries[countryCode] = [] list.playlists[UNDEFINED] = []
} }
list.countries[countryCode].push(channel) list.playlists[UNDEFINED].push(channel)
} else { } else {
for (let country of channel.countries) { for (let country of channel.countries) {
const countryCode = country.code || 'undefined' const countryCode = country.code || 'undefined'
if (!list.countries[countryCode]) { if (!list.playlists[countryCode]) {
list.countries[countryCode] = [] list.playlists[countryCode] = []
} }
list.countries[countryCode].push(channel) list.playlists[countryCode].push(channel)
} }
} }
// language // countries
if (!channel.languages.length) { if (!channel.countries.length) {
const languageCode = 'undefined' const UNDEFINED = 'undefined'
if (!list.languages[languageCode]) { if (!list.countries[UNDEFINED]) {
list.languages[languageCode] = [] list.countries[UNDEFINED] = []
} }
list.languages[languageCode].push(channel) list.countries[UNDEFINED].push(channel)
} else {
for (let country of channel.countries) {
const countryName = country.name
if (!list.countries[countryName]) {
list.countries[countryName] = []
}
list.countries[countryName].push(channel)
}
}
// languages
if (!channel.languages.length) {
const UNDEFINED = 'undefined'
if (!list.languages[UNDEFINED]) {
list.languages[UNDEFINED] = []
}
list.languages[UNDEFINED].push(channel)
} else { } else {
for (let language of channel.languages) { for (let language of channel.languages) {
const languageCode = language.code || 'undefined' const languageCode = language.code || 'undefined'
@ -86,7 +104,7 @@ function parseIndex() {
} }
} }
// category // categories
const categoryId = channel.category.toLowerCase() const categoryId = channel.category.toLowerCase()
if (!list.categories[categoryId]) { if (!list.categories[categoryId]) {
list.categories['other'].push(channel) list.categories['other'].push(channel)
@ -95,6 +113,20 @@ function parseIndex() {
} }
} }
} }
list.countries = Object.keys(list.countries)
.sort((a, b) => {
if (a === 'undefined') return -1
if (b === 'undefined') return 1
if (a < b) return -1
if (a > b) return 1
return 0
})
.reduce((obj, key) => {
obj[key] = list.countries[key]
return obj
}, {})
} }
function generateIndex() { function generateIndex() {
@ -133,14 +165,16 @@ function generateCountryIndex() {
const filename = `${ROOT_DIR}/index.country.m3u` const filename = `${ROOT_DIR}/index.country.m3u`
utils.createFile(filename, '#EXTM3U\n') utils.createFile(filename, '#EXTM3U\n')
const channels = utils.sortBy(list.all, ['tvgCountry', 'name', 'url']) for (let country in list.countries) {
const channels = utils.sortBy(list.countries[country], ['name', 'url'])
for (let channel of channels) { for (let channel of channels) {
const category = channel.category const category = channel.category
channel.category = channel.tvgCountry channel.category = country !== 'undefined' ? country : ''
utils.appendToFile(filename, channel.toString()) utils.appendToFile(filename, channel.toString())
channel.category = category channel.category = category
} }
} }
}
function generateLanguageIndex() { function generateLanguageIndex() {
console.log('Generating index.language.m3u...') console.log('Generating index.language.m3u...')
@ -172,11 +206,11 @@ function generateCountries() {
const outputDir = `${ROOT_DIR}/countries` const outputDir = `${ROOT_DIR}/countries`
utils.createDir(outputDir) utils.createDir(outputDir)
for (const countryId in list.countries) { for (const code in list.playlists) {
const filename = `${outputDir}/${countryId}.m3u` const filename = `${outputDir}/${code}.m3u`
utils.createFile(filename, '#EXTM3U\n') utils.createFile(filename, '#EXTM3U\n')
let channels = Object.values(list.countries[countryId]) let channels = Object.values(list.playlists[code])
channels = utils.sortBy(channels, ['name', 'url']) channels = utils.sortBy(channels, ['name', 'url'])
for (const channel of channels) { for (const channel of channels) {
utils.appendToFile(filename, channel.toString()) utils.appendToFile(filename, channel.toString())
@ -222,7 +256,7 @@ function finish() {
console.log('Done.\n') console.log('Done.\n')
console.log( console.log(
`Countries: ${Object.values(list.countries).length}. Languages: ${ `Countries: ${list.countries.length}. Languages: ${
Object.values(list.languages).length Object.values(list.languages).length
}. Categories: ${Object.values(list.categories).length}. Channels: ${list.all.length}.` }. Categories: ${Object.values(list.categories).length}. Channels: ${list.all.length}.`
) )