diff --git a/scripts/update-readme.js b/scripts/update-readme.js index 714b04a4..dc2e5b0f 100644 --- a/scripts/update-readme.js +++ b/scripts/update-readme.js @@ -2,15 +2,39 @@ const markdownInclude = require('markdown-include') const path = require('path') const fs = require('fs') const countries = require('./countries.json') -const file = require('./file.js') +const parser = require('./parser') +const file = require('./file') async function main() { console.log('Starting...') file - .list('channels/**/*.xml', [], ['us-local']) + .list('sites/*.channels.xml') .then(files => { - files = files.map(str => str.match(/channels\/(.*).xml/i)[1]) - generateTable(files) + let data = [] + files.forEach(filename => { + const channelsFile = file.read(filename) + const parsed = parser.parseChannels(channelsFile) + parsed.groups.forEach(group => { + const country = countries.find(c => c.code === group.country.toLowerCase()) + data.push({ + countryFlag: country.flag, + countryName: country.name, + guideUrl: `https://iptv-org.github.io/epg/guides/${country.code}/${parsed.site}.epg.xml`, + channelCount: group.channels.length + }) + }) + }) + + data = data.sort((a, b) => { + var nameA = a.countryName.toLowerCase() + var nameB = b.countryName.toLowerCase() + if (nameA < nameB) return -1 + if (nameA > nameB) return 1 + return b.channelCount - a.channelCount + }) + + const table = generateTable(data, ['Country', 'Channels', 'EPG']) + file.write('./.readme/_table.md', table) generateReadme() }) .finally(() => { @@ -18,50 +42,24 @@ async function main() { }) } -function generateTable(files) { - console.log('Generating countries table...') - - const output = [] - for (const country of countries) { - if (files.includes(country.code)) { - output.push({ - country: `${country.flag} ${country.name}`, - guide: `https://iptv-org.github.io/epg/guides/${country.code}.xml` - }) - } - } - - const table = generateHtmlTable(output, { - columns: [ - { name: 'Country', align: 'left', nowrap: true }, - { name: 'EPG', align: 'left', nowrap: true } - ] - }) - - fs.writeFileSync(path.resolve('./.readme/_table.md'), table) -} - -function generateHtmlTable(data, options) { +function generateTable(data, header) { let output = '\n' output += '\t\n\t\t' - for (let column of options.columns) { - output += `` + for (let column of header) { + output += `` } output += '\n\t\n' output += '\t\n' for (let item of data) { - output += '\t\t' - let i = 0 - for (let prop in item) { - const column = options.columns[i] - let nowrap = column.nowrap - let align = column.align - output += `` - i++ - } - output += '\n' + const size = data.filter(i => i.countryName === item.countryName).length + let root = output.indexOf(item.countryName) === -1 + const rowspan = root && size > 1 ? ` rowspan="${size}"` : '' + const cell1 = root + ? `` + : '' + output += `\t\t${cell1}\n` } output += '\t\n'
${column.name}${column}
${item[prop]}
${item.countryFlag} ${item.countryName}
${item.channelCount}${item.guideUrl}