Update update-readme.js

This commit is contained in:
freearhey 2021-01-30 06:26:15 +03:00
parent a61288c310
commit ea761db9d5
1 changed files with 94 additions and 96 deletions

View File

@ -1,99 +1,120 @@
const helper = require('./helper') const utils = require('./utils')
const parser = require('./parser')
let output = { const list = {
countries: [], countries: [],
languages: [], languages: [],
categories: [] categories: []
} }
function main() { function main() {
console.log(`Parsing index...`)
parseIndex() parseIndex()
console.log(`Generating countries table...`)
generateCountriesTable() generateCountriesTable()
console.log(`Generating languages table...`)
generateLanguagesTable() generateLanguagesTable()
console.log(`Generating categories table...`)
generateCategoriesTable() generateCategoriesTable()
console.log(`Generating README.md...`)
generateReadme() generateReadme()
console.log(`Done.`) finish()
} }
function parseIndex() { function parseIndex() {
const root = helper.parsePlaylist('index.m3u') console.log(`Parsing index...`)
const items = parser.parseIndex()
let countries = {} const countries = {}
let languages = {} const languages = {}
let categories = {} const categories = {}
for (let categoryCode in helper.supportedCategories) { countries['undefined'] = {
categories[categoryCode] = { country: 'Undefined',
category: helper.supportedCategories[categoryCode],
channels: 0, channels: 0,
playlist: `<code>https://iptv-org.github.io/iptv/categories/${categoryCode}.m3u</code>` playlist: `<code>https://iptv-org.github.io/iptv/countries/undefined.m3u</code>`,
} epg: '',
name: 'Undefined'
} }
for (let rootItem of root.items) { languages['undefined'] = {
const playlist = helper.parsePlaylist(rootItem.url) language: 'Undefined',
const countryName = rootItem.name channels: 0,
const countryCode = helper.getBasename(rootItem.url).toLowerCase() playlist: `<code>https://iptv-org.github.io/iptv/languages/undefined.m3u</code>`
const countryEpg = playlist.header.attrs['x-tvg-url'] }
? `<code>${playlist.header.attrs['x-tvg-url']}</code>`
: ''
for (let item of playlist.items) { for (const category of utils.supportedCategories) {
categories[category.id] = {
category: category.name,
channels: 0,
playlist: `<code>https://iptv-org.github.io/iptv/categories/${category.id}.m3u</code>`
}
}
categories['other'] = {
category: 'Other',
channels: 0,
playlist: `<code>https://iptv-org.github.io/iptv/categories/other.m3u</code>`
}
for (const item of items) {
const playlist = parser.parsePlaylist(item.url)
for (let channel of playlist.channels) {
// countries // countries
if (countries[countryCode]) { if (!channel.countries.length) {
countries[countryCode].channels++ countries['undefined'].channels++
} else { } else {
let flag = helper.code2flag(countryCode) for (let country of channel.countries) {
if (countries[country.code]) {
countries[countryCode] = { countries[country.code].channels++
country: flag + '&nbsp;' + countryName, } else {
let flag = utils.code2flag(country.code)
countries[country.code] = {
country: flag + '&nbsp;' + country.name,
channels: 1, channels: 1,
playlist: `<code>https://iptv-org.github.io/iptv/countries/${countryCode}.m3u</code>`, playlist: `<code>https://iptv-org.github.io/iptv/countries/${country.code}.m3u</code>`,
epg: countryEpg epg: playlist.header.attrs['x-tvg-url']
? `<code>${playlist.header.attrs['x-tvg-url']}</code>`
: '',
name: country.name
}
}
} }
} }
// languages // languages
const languageNames = item.tvg.language || 'Undefined' if (!channel.languages.length) {
for (let languageName of languageNames.split(';')) { languages['undefined'].channels++
let languageCode = 'undefined'
if (languageName !== 'Undefined') {
languageCode = helper.getISO6391Code(languageName)
if (!languageCode) continue
}
if (languages[languageCode]) {
languages[languageCode].channels++
} else { } else {
languages[languageCode] = { for (let language of channel.languages) {
language: languageName, if (languages[language.code]) {
languages[language.code].channels++
} else {
languages[language.code] = {
language: language.name,
channels: 1, channels: 1,
playlist: `<code>https://iptv-org.github.io/iptv/languages/${languageCode}.m3u</code>` playlist: `<code>https://iptv-org.github.io/iptv/languages/${language.code}.m3u</code>`
}
} }
} }
} }
// categories // categories
const categoryName = helper.filterGroup(item.group.title) || 'Other' const categoryId = channel.category.toLowerCase()
const categoryCode = categoryName.toLowerCase() if (!categoryId) {
if (categories[categoryCode]) { categories['other'].channels++
categories[categoryCode].channels++ } else if (categories[categoryId]) {
categories[categoryId].channels++
} }
} }
} }
output.countries = Object.values(countries) list.countries = Object.values(countries)
output.languages = Object.values(languages) list.languages = Object.values(languages)
output.categories = Object.values(categories) list.categories = Object.values(categories)
} }
function generateCountriesTable() { function generateCountriesTable() {
const table = helper.generateTable(output.countries, { console.log(`Generating countries table...`)
list.countries = utils.sortBy(list.countries, ['name'])
list.countries.forEach(function (i) {
delete i.name
})
const table = utils.generateTable(list.countries, {
columns: [ columns: [
{ name: 'Country', align: 'left' }, { name: 'Country', align: 'left' },
{ name: 'Channels', align: 'right' }, { name: 'Channels', align: 'right' },
@ -102,27 +123,13 @@ function generateCountriesTable() {
] ]
}) })
helper.createFile('./.readme/_countries.md', table) utils.createFile('./.readme/_countries.md', table)
} }
function generateLanguagesTable() { function generateLanguagesTable() {
output.languages.sort((a, b) => { console.log(`Generating languages table...`)
if (a.language === 'Undefined') { list.languages = utils.sortBy(list.languages, ['language'])
return 1 const table = utils.generateTable(list.languages, {
}
if (b.language === 'Undefined') {
return -1
}
if (a.language < b.language) {
return -1
}
if (a.language > b.language) {
return 1
}
return 0
})
const table = helper.generateTable(output.languages, {
columns: [ columns: [
{ name: 'Language', align: 'left' }, { name: 'Language', align: 'left' },
{ name: 'Channels', align: 'right' }, { name: 'Channels', align: 'right' },
@ -130,27 +137,13 @@ function generateLanguagesTable() {
] ]
}) })
helper.createFile('./.readme/_languages.md', table) utils.createFile('./.readme/_languages.md', table)
} }
function generateCategoriesTable() { function generateCategoriesTable() {
output.categories.sort((a, b) => { console.log(`Generating categories table...`)
if (a.category === 'Other') { list.categories = utils.sortBy(list.categories, ['category'])
return 1 const table = utils.generateTable(list.categories, {
}
if (b.category === 'Other') {
return -1
}
if (a.category < b.category) {
return -1
}
if (a.category > b.category) {
return 1
}
return 0
})
const table = helper.generateTable(output.categories, {
columns: [ columns: [
{ name: 'Category', align: 'left' }, { name: 'Category', align: 'left' },
{ name: 'Channels', align: 'right' }, { name: 'Channels', align: 'right' },
@ -158,11 +151,16 @@ function generateCategoriesTable() {
] ]
}) })
helper.createFile('./.readme/_categories.md', table) utils.createFile('./.readme/_categories.md', table)
} }
function generateReadme() { function generateReadme() {
helper.compileMarkdown('../.readme/config.json') console.log(`Generating README.md...`)
utils.compileMarkdown('../.readme/config.json')
}
function finish() {
console.log(`Done.`)
} }
main() main()