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

212 lines
5.8 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()
console.log('Generating channels.json...')
generateChannels()
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}.`
)
5 years ago
}
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 = {}
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)
channel.country.code = countryCode
channel.country.name = countryName
channel.tvg.url = playlist.header.attrs['x-tvg-url'] || ''
5 years ago
// all
list.all.push(channel)
5 years ago
// country
if (!countries[countryCode]) {
5 years ago
countries[countryCode] = []
}
5 years ago
countries[countryCode].push(channel)
5 years ago
// language
if (!channel.language.length) {
const languageCode = 'undefined'
if (!languages[languageCode]) {
languages[languageCode] = []
}
languages[languageCode].push(channel)
} else {
for (let language of channel.language) {
const languageCode = language.code || 'undefined'
if (!languages[languageCode]) {
languages[languageCode] = []
}
languages[languageCode].push(channel)
}
}
5 years ago
// category
const categoryCode = channel.category ? channel.category.toLowerCase() : 'other'
if (!categories[categoryCode]) {
5 years ago
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, ['name', 'url'])
for (let channel of channels) {
helper.appendToFile(filename, channel.toString())
}
}
function generateChannels() {
const filename = `${ROOT_DIR}/channels.json`
const sorted = helper.sortBy(list.all, ['name', 'url'])
const channels = sorted.map(c => c.toJSON())
helper.createFile(filename, JSON.stringify(channels))
}
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, ['country.name', 'name', 'url'])
for (let channel of channels) {
const category = channel.category
channel.category = channel.country.name
helper.appendToFile(filename, channel.toString())
channel.category = category
}
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.name', 'name', 'url'])
for (let channel of channels) {
const category = channel.category
channel.category = channel.language.map(l => l.name).join(';')
helper.appendToFile(filename, channel.toString())
channel.category = category
}
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, ['category', 'name', 'url'])
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), ['name', '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)
for (let cid in list.categories) {
5 years ago
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), ['name', '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)
for (let lid in list.languages) {
5 years ago
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), ['name', 'url'])
for (let channel of channels) {
helper.appendToFile(filename, channel.toString())
5 years ago
}
}
}
5 years ago
main()