You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iptv/scripts/generate.js

189 lines
5.1 KiB
JavaScript

const helper = require('./helper')
5 years ago
const ROOT_DIR = './.gh-pages'
5 years ago
let list = {
all: [],
countries: {},
languages: {},
categories: {}
}
5 years ago
function main() {
console.log(`Parsing index...`)
parseIndex()
console.log('Creating root directory...')
createRootDirectory()
console.log('Creating .nojekyll...')
createNoJekyllFile()
console.log('Generating index.m3u...')
generateIndex()
5 years ago
console.log('Generating index.country.m3u...')
generateCountryIndex()
console.log('Generating index.language.m3u...')
generateLanguageIndex()
console.log('Generating index.category.m3u...')
generateCategoryIndex()
console.log('Generating /countries...')
generateCountries()
5 years ago
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}.`)
}
function createRootDirectory() {
5 years ago
helper.createDir(ROOT_DIR)
}
function createNoJekyllFile() {
helper.createFile(`${ROOT_DIR}/.nojekyll`)
}
5 years ago
function parseIndex() {
const root = helper.parsePlaylist('index.m3u')
5 years ago
let countries = {}
let languages = {}
let categories = {}
5 years ago
for(let rootItem of root.items) {
const playlist = helper.parsePlaylist(rootItem.url)
const countryCode = helper.getBasename(rootItem.url).toLowerCase()
5 years ago
const countryName = rootItem.name
for(let item of playlist.items) {
const channel = helper.createChannel(item)
5 years ago
channel.countryCode = countryCode
channel.countryName = countryName
channel.epg = playlist.header.attrs['x-tvg-url'] || ''
5 years ago
// all
list.all.push(channel)
5 years ago
// country
if(!countries[countryCode]) {
countries[countryCode] = []
}
5 years ago
countries[countryCode].push(channel)
5 years ago
// language
const languageCode = helper.getISO6391Code(channel.language) || 'undefined'
5 years ago
if(!languages[languageCode]) {
languages[languageCode] = []
}
5 years ago
languages[languageCode].push(channel)
5 years ago
// category
const categoryCode = channel.group.toLowerCase() || 'other'
if(!categories[categoryCode]) {
categories[categoryCode] = []
}
5 years ago
categories[categoryCode].push(channel)
}
5 years ago
}
5 years ago
list.countries = countries
list.languages = languages
list.categories = categories
}
function generateIndex() {
const filename = `${ROOT_DIR}/index.m3u`
helper.createFile(filename, '#EXTM3U\n')
const channels = helper.sortBy(list.all, ['title', 'url'])
for(let channel of channels) {
helper.appendToFile(filename, channel.toString())
}
}
5 years ago
function generateCountryIndex() {
5 years ago
const filename = `${ROOT_DIR}/index.country.m3u`
helper.createFile(filename, '#EXTM3U\n')
5 years ago
const channels = helper.sortBy(list.all, ['countryName', 'title', 'url'])
for(let channel of channels) {
5 years ago
const group = channel.group
channel.group = channel.countryName
helper.appendToFile(filename, channel.toString())
5 years ago
channel.group = group
}
5 years ago
}
5 years ago
function generateLanguageIndex() {
5 years ago
const filename = `${ROOT_DIR}/index.language.m3u`
helper.createFile(filename, '#EXTM3U\n')
5 years ago
const channels = helper.sortBy(list.all, ['language', 'title', 'url'])
5 years ago
for(let channel of channels) {
const group = channel.group
channel.group = channel.language
helper.appendToFile(filename, channel.toString())
5 years ago
channel.group = group
}
5 years ago
}
function generateCategoryIndex() {
const filename = `${ROOT_DIR}/index.category.m3u`
helper.createFile(filename, '#EXTM3U\n')
5 years ago
const channels = helper.sortBy(list.all, ['group', 'title', 'url'])
5 years ago
for(let channel of channels) {
helper.appendToFile(filename, channel.toString())
}
}
function generateCountries() {
const outputDir = `${ROOT_DIR}/countries`
helper.createDir(outputDir)
for(let cid in list.countries) {
let country = list.countries[cid]
const filename = `${outputDir}/${cid}.m3u`
helper.createFile(filename, '#EXTM3U\n')
const channels = helper.sortBy(Object.values(country), ['title', 'url'])
for(let channel of channels) {
helper.appendToFile(filename, channel.toString())
}
}
}
5 years ago
function generateCategories() {
5 years ago
const outputDir = `${ROOT_DIR}/categories`
helper.createDir(outputDir)
5 years ago
for(let cid in list.categories) {
let category = list.categories[cid]
5 years ago
const filename = `${outputDir}/${cid}.m3u`
helper.createFile(filename, '#EXTM3U\n')
const channels = helper.sortBy(Object.values(category), ['title', 'url'])
for(let channel of channels) {
helper.appendToFile(filename, channel.toString())
5 years ago
}
}
}
function generateLanguages() {
5 years ago
const outputDir = `${ROOT_DIR}/languages`
helper.createDir(outputDir)
5 years ago
for(let lid in list.languages) {
let language = list.languages[lid]
5 years ago
const filename = `${outputDir}/${lid}.m3u`
helper.createFile(filename, '#EXTM3U\n')
const channels = helper.sortBy(Object.values(language), ['title', 'url'])
for(let channel of channels) {
helper.appendToFile(filename, channel.toString())
5 years ago
}
}
}
5 years ago
main()