iptv/scripts/generate.js

191 lines
5.3 KiB
JavaScript
Raw Normal View History

2019-11-02 10:45:09 +01:00
const helper = require('./helper')
2019-11-02 11:26:21 +01:00
const ROOT_DIR = './.gh-pages'
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:27:05 +01:00
function main() {
console.log(`Parsing index...`)
parseIndex()
2019-11-02 11:26:21 +01:00
console.log('Creating public directory...')
createPublicDirectory()
2019-11-01 22:27:05 +01:00
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('Generating /categories...')
generateCategories()
console.log('Generating /languages...')
generateLanguages()
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}.`)
}
2019-11-02 11:26:21 +01:00
function createPublicDirectory() {
helper.createDir(ROOT_DIR)
}
2019-11-01 22:08:27 +01:00
function parseIndex() {
2019-11-02 10:45:09 +01:00
const root = helper.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) {
2019-11-02 10:45:09 +01:00
const playlist = helper.parsePlaylist(rootItem.url)
const countryCode = helper.getBasename(rootItem.url).toLowerCase()
2019-11-01 22:08:27 +01:00
const countryName = rootItem.name
2019-08-07 16:28:35 +02:00
for(let item of playlist.items) {
2019-11-02 10:45:09 +01:00
const channel = helper.createChannel(item)
2019-11-01 22:08:27 +01:00
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
2019-11-02 10:45:09 +01:00
const languageCode = helper.getISO6391Code(channel.language) || 'undefined'
2019-11-01 22:08:27 +01:00
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() {
2019-11-02 11:26:21 +01:00
const filename = `${ROOT_DIR}/index.country.m3u`
2019-11-02 10:45:09 +01:00
helper.createFile(filename, '#EXTM3U\n')
2019-11-01 22:08:27 +01:00
for(let channel of list.all) {
const group = channel.group
channel.group = channel.countryName
2019-11-02 10:45:09 +01:00
helper.appendToFile(filename, channel.toString())
2019-11-01 22:08:27 +01:00
channel.group = group
}
2019-11-01 22:08:27 +01:00
}
2019-11-01 22:08:27 +01:00
function generateLanguageIndex() {
2019-11-02 11:26:21 +01:00
const filename = `${ROOT_DIR}/index.language.m3u`
2019-11-02 10:45:09 +01:00
helper.createFile(filename, '#EXTM3U\n')
2019-11-01 22:08:27 +01:00
const channels = list.all.sort((a, b) => {
if(a.language < b.language) { return -1 }
if(a.language > b.language) { return 1 }
2019-11-02 10:53:59 +01:00
if(a.title.toLowerCase() < b.title.toLowerCase()) { return -1 }
if(a.title.toLowerCase() > b.title.toLowerCase()) { return 1 }
if(a.url < b.url) { return -1 }
if(a.url > b.url) { return 1 }
2019-11-01 22:08:27 +01:00
return 0
})
for(let channel of channels) {
const group = channel.group
channel.group = channel.language
2019-11-02 10:45:09 +01:00
helper.appendToFile(filename, channel.toString())
2019-11-01 22:08:27 +01:00
channel.group = group
}
2019-11-01 22:08:27 +01:00
}
2019-11-01 22:08:27 +01:00
function generateContentIndex() {
2019-11-02 11:26:21 +01:00
const filename = `${ROOT_DIR}/index.content.m3u`
2019-11-02 10:45:09 +01:00
helper.createFile(filename, '#EXTM3U\n')
2019-11-01 22:08:27 +01:00
const channels = list.all.sort((a, b) => {
if(a.group < b.group) { return -1 }
if(a.group > b.group) { return 1 }
2019-11-02 10:53:59 +01:00
if(a.title.toLowerCase() < b.title.toLowerCase()) { return -1 }
if(a.title.toLowerCase() > b.title.toLowerCase()) { return 1 }
if(a.url < b.url) { return -1 }
if(a.url > b.url) { return 1 }
2019-11-01 22:08:27 +01:00
return 0
})
for(let channel of channels) {
2019-11-02 10:45:09 +01:00
helper.appendToFile(filename, channel.toString())
}
}
2019-11-01 22:08:27 +01:00
function generateFullIndex() {
2019-11-02 11:26:21 +01:00
const filename = `${ROOT_DIR}/index.full.m3u`
2019-11-02 10:45:09 +01:00
helper.createFile(filename, '#EXTM3U\n')
2019-11-01 22:08:27 +01:00
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 }
2019-11-02 10:53:59 +01:00
if(a.title.toLowerCase() < b.title.toLowerCase()) { return -1 }
if(a.title.toLowerCase() > b.title.toLowerCase()) { return 1 }
if(a.url < b.url) { return -1 }
if(a.url > b.url) { return 1 }
2019-11-01 22:08:27 +01:00
return 0
})
for(let channel of channels) {
const group = channel.group
channel.group = [ channel.countryName, channel.group ].filter(i => i).join(';')
2019-11-02 10:45:09 +01:00
helper.appendToFile(filename, channel.toString())
2019-11-01 22:08:27 +01:00
channel.group = group
}
}
2019-11-01 22:21:08 +01:00
function generateCategories() {
2019-11-02 11:26:21 +01:00
const outputDir = `${ROOT_DIR}/categories`
helper.createDir(outputDir)
2019-11-01 22:21:08 +01:00
for(let cid in list.categories) {
let category = list.categories[cid]
2019-11-02 11:26:21 +01:00
const filename = `${outputDir}/${cid}.m3u`
2019-11-02 10:45:09 +01:00
helper.createFile(filename, '#EXTM3U\n')
2019-11-01 22:21:08 +01:00
for(let channel of category) {
2019-11-02 10:45:09 +01:00
helper.appendToFile(filename, channel.toString())
2019-11-01 22:21:08 +01:00
}
}
}
function generateLanguages() {
2019-11-02 11:26:21 +01:00
const outputDir = `${ROOT_DIR}/languages`
helper.createDir(outputDir)
2019-11-01 22:21:08 +01:00
for(let lid in list.languages) {
let language = list.languages[lid]
2019-11-02 11:26:21 +01:00
const filename = `${outputDir}/${lid}.m3u`
2019-11-02 10:45:09 +01:00
helper.createFile(filename, '#EXTM3U\n')
2019-11-01 22:21:08 +01:00
for(let channel of language) {
2019-11-02 10:45:09 +01:00
helper.appendToFile(filename, channel.toString())
2019-11-01 22:21:08 +01:00
}
}
}
2019-11-01 22:08:27 +01:00
main()