iptv/helpers/generate.js

193 lines
5.2 KiB
JavaScript
Raw Normal View History

2019-07-20 09:03:31 +02:00
const util = require('./util')
const ISO6391 = require('iso-639-1')
2019-11-01 22:08:27 +01:00
let list = {
all: [],
countries: {},
languages: {},
categories: {}
2019-07-20 09:03:31 +02:00
}
2019-11-01 22:08:27 +01:00
function parseIndex() {
const root = util.parsePlaylist('index.m3u')
2019-11-01 22:08:27 +01:00
let countries = {}
let languages = {}
let categories = {}
2019-11-01 22:08:27 +01:00
for(let rootItem of root.items) {
const playlist = util.parsePlaylist(rootItem.url)
const countryCode = util.getBasename(rootItem.url).toLowerCase()
const countryName = rootItem.name
2019-08-07 16:28:35 +02:00
for(let item of playlist.items) {
2019-11-01 22:08:27 +01:00
const channel = util.createChannel(item)
channel.countryCode = countryCode
channel.countryName = countryName
2019-11-01 22:08:27 +01:00
// all
list.all.push(channel)
2019-11-01 22:08:27 +01:00
// country
if(!countries[countryCode]) {
countries[countryCode] = []
}
2019-11-01 22:08:27 +01:00
countries[countryCode].push(channel)
2019-11-01 22:08:27 +01:00
// language
const languageCode = ISO6391.getCode(channel.language) || 'undefined'
if(!languages[languageCode]) {
languages[languageCode] = []
}
2019-11-01 22:08:27 +01:00
languages[languageCode].push(channel)
2019-11-01 22:08:27 +01:00
// category
const categoryCode = channel.group.toLowerCase() || 'other'
if(!categories[categoryCode]) {
categories[categoryCode] = []
2019-10-31 13:21:08 +01:00
}
2019-11-01 22:08:27 +01:00
categories[categoryCode].push(channel)
}
2019-11-01 22:08:27 +01:00
}
2019-11-01 22:08:27 +01:00
list.countries = countries
list.languages = languages
list.categories = categories
}
function generateCountryIndex() {
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
}
2019-11-01 22:08:27 +01:00
}
2019-11-01 22:08:27 +01:00
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
}
2019-11-01 22:08:27 +01:00
}
2019-11-01 22:08:27 +01:00
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())
}
}
2019-11-01 22:08:27 +01:00
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}.`)
}
2019-04-30 14:45:02 +02:00
2019-11-01 22:08:27 +01:00
main()