This commit is contained in:
freearhey 2019-11-02 00:08:27 +03:00
parent 043ac262eb
commit 5412372c05
1 changed files with 168 additions and 100 deletions

View File

@ -1,124 +1,192 @@
const util = require('./util') const util = require('./util')
const ISO6391 = require('iso-639-1') const ISO6391 = require('iso-639-1')
const debug = false let list = {
const types = ['full', 'country', 'content', 'language'] all: [],
const categories = util.supportedCategories.map(c => c.toLowerCase()) countries: {},
let stats = { languages: {},
countries: 0, categories: {}
channels: 0
} }
let languageBuffer = { function parseIndex() {
undefined: [] const root = util.parsePlaylist('index.m3u')
}
let categoryBuffer = {} let countries = {}
categories.push('other') let languages = {}
categories.forEach(category => { let categories = {}
categoryBuffer[category] = []
})
function main() { for(let rootItem of root.items) {
console.log(`Parsing 'index.m3u'...`) const playlist = util.parsePlaylist(rootItem.url)
const playlist = util.parsePlaylist('index.m3u') const countryCode = util.getBasename(rootItem.url).toLowerCase()
let countries = playlist.items const countryName = rootItem.name
if(debug) {
console.log('Debug mode is turn on')
countries = countries.slice(0, 1)
}
for(let type of types) {
const filename = `index.${type}.m3u`
console.log(`Creating '${filename}'...`)
util.createFile(filename, '#EXTM3U\n')
}
for(let category of categories) {
const filename = `categories/${category}.m3u`
console.log(`Creating '${filename}'...`)
util.createFile(filename, '#EXTM3U\n')
const categoryName = util.supportedCategories.find(c => c.toLowerCase() === category) || 'Other'
}
for(let country of countries) {
console.log(`Parsing '${country.url}'...`)
const playlist = util.parsePlaylist(country.url)
const c = {
name: country.name,
code: util.getBasename(country.url).toUpperCase()
}
for(let item of playlist.items) { for(let item of playlist.items) {
const channel = util.createChannel(item)
channel.countryCode = countryCode
channel.countryName = countryName
let channel = util.createChannel(item) // all
list.all.push(channel)
let category = channel.group.toLowerCase() // country
if(categoryBuffer[category]) { if(!countries[countryCode]) {
categoryBuffer[category].push(channel) countries[countryCode] = []
} else {
categoryBuffer['other'].push(channel)
} }
countries[countryCode].push(channel)
let languageCode = ISO6391.getCode(channel.language) // language
if(languageCode) { const languageCode = ISO6391.getCode(channel.language) || 'undefined'
if(!languageBuffer[languageCode]) { if(!languages[languageCode]) {
languageBuffer[languageCode] = [] languages[languageCode] = []
}
languageBuffer[languageCode].push(channel)
} else {
languageBuffer['undefined'].push(channel)
} }
languages[languageCode].push(channel)
let group = channel.group // category
for(const type of types) { const categoryCode = channel.group.toLowerCase() || 'other'
if(type === 'full') { if(!categories[categoryCode]) {
channel.group = [ c.name, channel.group ].filter(i => i).join(';') categories[categoryCode] = []
} else if(type === 'country') {
channel.group = c.name
} else if(type === 'language') {
channel.group = channel.language
} else {
channel.group = group
}
util.appendToFile(`index.${type}.m3u`, channel.toString())
}
stats.channels++
}
stats.countries++
}
util.clearCache()
for(const languageCode in languageBuffer) {
const filename = `languages/${languageCode}.m3u`
util.createFile(filename, '#EXTM3U\n')
let channels = util.sortByTitleAndUrl(languageBuffer[languageCode])
for(const channel of channels) {
if(!util.checkCache(channel.url)) {
util.appendToFile(filename, channel.toString())
util.addToCache(channel.url)
} }
categories[categoryCode].push(channel)
} }
} }
util.clearCache() list.countries = countries
for(const category in categoryBuffer) { list.languages = languages
let channels = util.sortByTitleAndUrl(categoryBuffer[category]) list.categories = categories
for(const channel of channels) { }
if(!util.checkCache(channel.url)) {
util.appendToFile(`categories/${category}.m3u`, channel.toString()) function generateCountryIndex() {
util.addToCache(channel.url) const filename = `index.country.m3u`
} util.createFile(filename, '#EXTM3U\n')
}
for(let channel of list.all) {
const group = channel.group
channel.group = channel.countryName
util.appendToFile(filename, channel.toString())
channel.group = group
} }
} }
function generateLanguageIndex() {
const filename = `index.language.m3u`
util.createFile(filename, '#EXTM3U\n')
const channels = list.all.sort((a, b) => {
if(a.language < b.language) { return -1 }
if(a.language > b.language) { return 1 }
return 0
})
for(let channel of channels) {
const group = channel.group
channel.group = channel.language
util.appendToFile(filename, channel.toString())
channel.group = group
}
}
function generateContentIndex() {
const filename = `index.content.m3u`
util.createFile(filename, '#EXTM3U\n')
const channels = list.all.sort((a, b) => {
if(a.group < b.group) { return -1 }
if(a.group > b.group) { return 1 }
return 0
})
for(let channel of channels) {
util.appendToFile(filename, channel.toString())
}
}
function generateFullIndex() {
const filename = `index.full.m3u`
util.createFile(filename, '#EXTM3U\n')
const channels = list.all.sort((a, b) => {
if(a.countryName < b.countryName) { return -1 }
if(a.countryName > b.countryName) { return 1 }
if(a.group < b.group) { return -1 }
if(a.group > b.group) { return 1 }
return 0
})
for(let channel of channels) {
const group = channel.group
channel.group = [ channel.countryName, channel.group ].filter(i => i).join(';')
util.appendToFile(filename, channel.toString())
channel.group = group
}
}
function main() {
console.log(`Parsing index...`)
parseIndex()
console.log('Generating index.country.m3u...')
generateCountryIndex()
console.log('Generating index.language.m3u...')
generateLanguageIndex()
console.log('Generating index.content.m3u...')
generateContentIndex()
console.log('Generating index.full.m3u...')
generateFullIndex()
// console.log(output)
// let group = channel.group
// for(const type of types) {
// if(type === 'full') {
// channel.group = [ c.name, channel.group ].filter(i => i).join(';')
// } else if(type === 'country') {
// channel.group = c.name
// } else if(type === 'language') {
// channel.group = channel.language
// } else {
// channel.group = group
// }
// util.appendToFile(`index.${type}.m3u`, channel.toString())
// }
// for(let type of types) {
// const filename = `index.${type}.m3u`
// console.log(`Creating '${filename}'...`)
// util.createFile(filename, '#EXTM3U\n')
// }
// const categories = util.supportedCategories
// for(let category of categories) {
// const categoryCode = category.toLowerCase()
// util.createFile(`categories/${categoryCode}.m3u`, '#EXTM3U\n')
// }
// util.clearCache()
// for(const languageCode in languageBuffer) {
// const filename = `languages/${languageCode}.m3u`
// util.createFile(filename, '#EXTM3U\n')
// let channels = util.sortByTitleAndUrl(languageBuffer[languageCode])
// for(const channel of channels) {
// if(!util.checkCache(channel.url)) {
// util.appendToFile(filename, channel.toString())
// util.addToCache(channel.url)
// }
// }
// }
// util.clearCache()
// for(const category in categoryBuffer) {
// let channels = util.sortByTitleAndUrl(categoryBuffer[category])
// for(const channel of channels) {
// if(!util.checkCache(channel.url)) {
// util.appendToFile(`categories/${category}.m3u`, channel.toString())
// util.addToCache(channel.url)
// }
// }
// }
//
// console.log(`Countries: ${stats.countries}. Languages: ${stats.channels}. Categories: ${stats.channels}. Channels: ${stats.channels}.`)
}
main() main()
console.log(`Countries: ${stats.countries}. Channels: ${stats.channels}.`)