Code refactoring

This commit is contained in:
freearhey 2019-11-01 21:13:15 +03:00
parent 816f884d13
commit fb941ac831
1 changed files with 103 additions and 132 deletions

View File

@ -3,142 +3,36 @@ const ISO6391 = require('iso-639-1')
const markdownInclude = require('markdown-include') const markdownInclude = require('markdown-include')
const path = require('path') const path = require('path')
let languageBuffer = {
undefined: []
}
let categories = util.supportedCategories.map(c => c.toLowerCase())
let categoryBuffer = {}
categories.push('other')
categories.forEach(category => {
categoryBuffer[category] = []
})
let repo = {
categories: {},
countries: {},
languages: {}
}
function main() { function main() {
console.log(`Parsing 'index.m3u'...`) console.log(`Generating countries table...`)
const playlist = util.parsePlaylist('index.m3u') generateCountriesTable()
let countries = playlist.items console.log(`Generating languages table...`)
generateLanguagesTable()
for(let category of categories) { console.log(`Generating categories table...`)
const filename = `categories/${category}.m3u` generateCategoriesTable()
const categoryName = util.supportedCategories.find(c => c.toLowerCase() === category) || 'Other' console.log(`Generating README.md...`)
repo.categories[category] = { generateReadme()
category: categoryName, console.log(`Done.`)
channels: 0,
playlist: `<code>https://iptv-org.github.io/iptv/${filename}</code>`
}
} }
for(let country of countries) { function generateCountriesTable() {
console.log(`Parsing '${country.url}'...`) const root = util.parsePlaylist('index.m3u')
const playlist = util.parsePlaylist(country.url)
const countryCode = util.getBasename(country.url).toUpperCase() let countries = []
repo.countries[countryCode] = { for(let item of root.items) {
country: country.name, const playlist = util.parsePlaylist(item.url)
const countryCode = util.getBasename(item.url).toUpperCase()
const epg = playlist.header.attrs['x-tvg-url'] ? `<code>${playlist.header.attrs['x-tvg-url']}</code>` : ''
countries.push({
country: item.name,
channels: playlist.items.length, channels: playlist.items.length,
playlist: `<code>https://iptv-org.github.io/iptv/${country.url}</code>`, playlist: `<code>https://iptv-org.github.io/iptv/${item.url}</code>`,
epg: playlist.header.attrs['x-tvg-url'] ? `<code>${playlist.header.attrs['x-tvg-url']}</code>` : '' epg
}
for(let item of playlist.items) {
let channel = util.createChannel(item)
let category = channel.group.toLowerCase()
if(categoryBuffer[category]) {
categoryBuffer[category].push(channel)
} else {
categoryBuffer['other'].push(channel)
}
let languageCode = ISO6391.getCode(channel.language)
if(languageCode) {
if(!languageBuffer[languageCode]) {
languageBuffer[languageCode] = []
}
languageBuffer[languageCode].push(channel)
} else {
languageBuffer['undefined'].push(channel)
}
}
}
for(const languageCode in languageBuffer) {
let languageName = ISO6391.getName(languageCode)
if(languageName) {
repo.languages[languageCode] = {
language: languageName,
channels: 0,
playlist: `<code>https://iptv-org.github.io/iptv/languages/${languageCode}.m3u</code>`
}
}
}
repo.languages['undefined'] = {
language: 'Undefined',
channels: 0,
playlist: `<code>https://iptv-org.github.io/iptv/languages/undefined.m3u</code>`
}
util.clearCache()
for(const languageCode in languageBuffer) {
let channels = util.sortByTitleAndUrl(languageBuffer[languageCode])
for(const channel of channels) {
if(!util.checkCache(channel.url)) {
util.addToCache(channel.url)
repo.languages[languageCode].channels++
}
}
}
util.clearCache()
for(const category in categoryBuffer) {
let channels = util.sortByTitleAndUrl(categoryBuffer[category])
for(const channel of channels) {
if(!util.checkCache(channel.url)) {
util.addToCache(channel.url)
repo.categories[category].channels++
}
}
}
const languages = Object.values(repo.languages)
const lastRow = languages.splice(languages.length - 1, 1)[0]
languages.sort((a, b) => {
if(a.language < b.language) { return -1 }
if(a.language > b.language) { return 1 }
return 0
}) })
languages.push(lastRow) }
const languagesTable = util.generateTable(languages, { const table = util.generateTable(countries, {
columns: [
{ name: 'Language', align: 'left' },
{ name: 'Channels', align: 'right' },
{ name: 'Playlist', align: 'left' }
]
})
util.createFile('./helpers/languages.md', languagesTable)
const categoriesTable = util.generateTable(Object.values(repo.categories), {
columns: [
{ name: 'Category', align: 'left' },
{ name: 'Channels', align: 'right' },
{ name: 'Playlist', align: 'left' }
]
})
util.createFile('./helpers/categories.md', categoriesTable)
const countriesTable = util.generateTable(Object.values(repo.countries), {
columns: [ columns: [
{ name: 'Country', align: 'left' }, { name: 'Country', align: 'left' },
{ name: 'Channels', align: 'right' }, { name: 'Channels', align: 'right' },
@ -146,11 +40,88 @@ function main() {
{ name: 'EPG', align: 'left' } { name: 'EPG', align: 'left' }
] ]
}) })
util.createFile('./helpers/countries.md', countriesTable)
util.createFile('./helpers/countries.md', table)
}
function generateLanguagesTable() {
const playlist = util.parsePlaylist('index.language.m3u')
let languages = {}
for(let item of playlist.items) {
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: `<code>https://iptv-org.github.io/iptv/languages/${languageCode}.m3u</code>`
}
}
}
languages = Object.values(languages)
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(languages, {
columns: [
{ name: 'Language', align: 'left' },
{ name: 'Channels', align: 'right' },
{ name: 'Playlist', align: 'left' }
]
})
util.createFile('./helpers/languages.md', table)
}
function generateCategoriesTable() {
const playlist = util.parsePlaylist('index.content.m3u')
let categories = {}
for(let item of playlist.items) {
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: `<code>https://iptv-org.github.io/iptv/categories/${categoryCode}.m3u</code>`
}
}
}
categories = Object.values(categories)
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(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')) markdownInclude.compileFiles(path.resolve(__dirname, './markdown.json'))
} }
main() main()
console.log(`Done.`)