const util = require('./util')
const ISO6391 = require('iso-639-1')
const markdownInclude = require('markdown-include')
const path = require('path')
let data = {
countries: [],
languages: [],
categories: []
}
function main() {
console.log(`Parsing index...`)
parseIndex()
console.log(`Generating countries table...`)
generateCountriesTable()
console.log(`Generating languages table...`)
generateLanguagesTable()
console.log(`Generating categories table...`)
generateCategoriesTable()
console.log(`Generating README.md...`)
generateReadme()
console.log(`Done.`)
}
function parseIndex() {
const root = util.parsePlaylist('index.m3u')
let languages = {}
let categories = {}
for(let rootItem of root.items) {
const playlist = util.parsePlaylist(rootItem.url)
const countryCode = util.getBasename(rootItem.url).toUpperCase()
const epg = playlist.header.attrs['x-tvg-url'] ? `${playlist.header.attrs['x-tvg-url']}
` : ''
data.countries.push({
country: rootItem.name,
channels: playlist.items.length,
playlist: `https://iptv-org.github.io/iptv/${rootItem.url}
`,
epg
})
for(let item of playlist.items) {
// language
const languageName = item.group.title || 'Undefined'
const languageCode = ISO6391.getCode(languageName) || 'undefined'
if(languages[languageCode]) {
languages[languageCode].channels++
} else {
languages[languageCode] = {
language: languageName,
channels: 1,
playlist: `https://iptv-org.github.io/iptv/languages/${languageCode}.m3u
`
}
}
// category
const categoryName = util.supportedCategories.find(c => c === item.group.title) || 'Other'
const categoryCode = categoryName.toLowerCase()
if(categories[categoryCode]) {
categories[categoryCode].channels++
} else {
categories[categoryCode] = {
category: categoryName,
channels: 1,
playlist: `https://iptv-org.github.io/iptv/categories/${categoryCode}.m3u
`
}
}
}
}
data.languages = Object.values(languages)
data.categories = Object.values(categories)
}
function generateCountriesTable() {
const table = util.generateTable(data.countries, {
columns: [
{ name: 'Country', align: 'left' },
{ name: 'Channels', align: 'right' },
{ name: 'Playlist', align: 'left', nowrap: true },
{ name: 'EPG', align: 'left' }
]
})
util.createFile('./helpers/countries.md', table)
}
function generateLanguagesTable() {
data.languages.sort((a, b) => {
if(a.language === 'Undefined') { return 1 }
if(b.language === 'Undefined') { return -1 }
if(a.language < b.language) { return -1 }
if(a.language > b.language) { return 1 }
return 0
})
const table = util.generateTable(data.languages, {
columns: [
{ name: 'Language', align: 'left' },
{ name: 'Channels', align: 'right' },
{ name: 'Playlist', align: 'left' }
]
})
util.createFile('./helpers/languages.md', table)
}
function generateCategoriesTable() {
data.categories.sort((a, b) => {
if(a.category === 'Other') { return 1 }
if(b.category === 'Other') { return -1 }
if(a.category < b.category) { return -1 }
if(a.category > b.category) { return 1 }
return 0
})
const table = util.generateTable(data.categories, {
columns: [
{ name: 'Category', align: 'left' },
{ name: 'Channels', align: 'right' },
{ name: 'Playlist', align: 'left' }
]
})
util.createFile('./helpers/categories.md', table)
}
function generateReadme() {
markdownInclude.compileFiles(path.resolve(__dirname, './markdown.json'))
}
main()