diff --git a/scripts/update-readme.js b/scripts/update-readme.js index 18b259896a..77d26c5312 100644 --- a/scripts/update-readme.js +++ b/scripts/update-readme.js @@ -1,6 +1,9 @@ const utils = require('./utils') const parser = require('./parser') const categories = require('./categories') +const db = require('./db') + +db.load() const list = { countries: {}, @@ -34,19 +37,6 @@ function parseIndex() { playlist: `https://iptv-org.github.io/iptv/languages/undefined.m3u` } - for (const category of categories) { - list.categories[category.id] = { - category: category.name, - channels: 0, - playlist: `https://iptv-org.github.io/iptv/categories/${category.id}.m3u` - } - } - list.categories['other'] = { - category: 'Other', - channels: 0, - playlist: `https://iptv-org.github.io/iptv/categories/other.m3u` - } - for (const item of items) { const playlist = parser.parsePlaylist(item.url) for (let channel of playlist.channels) { @@ -85,20 +75,40 @@ function parseIndex() { } } } - - // categories - const categoryId = channel.category.toLowerCase() - if (!categoryId) { - list.categories['other'].channels++ - } else if (list.categories[categoryId]) { - list.categories[categoryId].channels++ - } } } list.countries = Object.values(list.countries) list.languages = Object.values(list.languages) - list.categories = Object.values(list.categories) +} + +function generateCategoriesTable() { + console.log(`Generating categories table...`) + const categories = [] + + for (const category of db.categories.all()) { + categories.push({ + category: category.name, + channels: db.channels.forCategory(category).count(), + playlist: `https://iptv-org.github.io/iptv/categories/${category.id}.m3u` + }) + } + + categories.push({ + category: 'Other', + channels: db.channels.forCategory({ id: null }).count(), + playlist: `https://iptv-org.github.io/iptv/categories/other.m3u` + }) + + const table = utils.generateTable(categories, { + columns: [ + { name: 'Category', align: 'left' }, + { name: 'Channels', align: 'right' }, + { name: 'Playlist', align: 'left' } + ] + }) + + utils.createFile('./.readme/_categories.md', table) } function generateCountriesTable() { @@ -132,20 +142,6 @@ function generateLanguagesTable() { utils.createFile('./.readme/_languages.md', table) } -function generateCategoriesTable() { - console.log(`Generating categories table...`) - list.categories = utils.sortBy(list.categories, ['category']) - const table = utils.generateTable(list.categories, { - columns: [ - { name: 'Category', align: 'left' }, - { name: 'Channels', align: 'right' }, - { name: 'Playlist', align: 'left' } - ] - }) - - utils.createFile('./.readme/_categories.md', table) -} - function generateReadme() { console.log(`Generating README.md...`) utils.compileMarkdown('../.readme/config.json')