From 8d19eb7ecc5457ca638b69f27c360e29efc1bed3 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Mon, 18 Oct 2021 08:09:01 +0300 Subject: [PATCH] Convert update-readme.js to TypeScript --- scripts/update-readme.js | 81 ------------------- scripts/update-readme.ts | 164 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+), 81 deletions(-) delete mode 100644 scripts/update-readme.js create mode 100644 scripts/update-readme.ts diff --git a/scripts/update-readme.js b/scripts/update-readme.js deleted file mode 100644 index 1cf1f9cf..00000000 --- a/scripts/update-readme.js +++ /dev/null @@ -1,81 +0,0 @@ -const parser = require('epg-parser') -const markdownInclude = require('markdown-include') -const countries = require('./countries.json') -const file = require('./file') - -async function main() { - console.log('Starting...') - file - .list('.gh-pages/guides/**/*.xml') - .then(files => { - let data = [] - files.forEach(filename => { - const countryCode = filename.match(/\.gh\-pages\/guides\/(.*)\/.*/i)[1] - const country = countries.find(c => c.code === countryCode) - if (!country) return - const epg = file.read(filename) - const parsed = parser.parse(epg) - let emptyGuides = 0 - parsed.channels.forEach(channel => { - const showCount = parsed.programs.filter(p => p.channel === channel.id).length - if (showCount === 0) emptyGuides++ - }) - data.push({ - countryFlag: country.flag, - countryName: country.name, - guideUrl: filename.replace('.gh-pages', 'https://iptv-org.github.io/epg'), - channelCount: parsed.channels.length, - emptyGuides - }) - }) - - data = data.sort((a, b) => { - var countryNameA = a.countryName.toLowerCase() - var countryNameB = b.countryName.toLowerCase() - if (countryNameA < countryNameB) return -1 - if (countryNameA > countryNameB) return 1 - return b.channelCount - a.channelCount - }) - - console.log('Generating table...') - const table = generateTable(data, ['Country', 'Channels', 'EPG', 'Status']) - file.write('.readme/_table.md', table) - console.log('Updating README.md...') - markdownInclude.compileFiles('.readme/config.json') - }) - .finally(() => { - console.log('Finish') - }) -} - -function generateTable(data, header) { - let output = '\n' - - output += '\t\n\t\t' - for (let column of header) { - output += `` - } - output += '\n\t\n' - - output += '\t\n' - for (let item of data) { - 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 name = item.countryName - let status = '🟢' - if (item.emptyGuides === item.channelCount) status = '🔴' - else if (item.emptyGuides > 0) status = '🟡' - const cell1 = root - ? `` - : '' - output += `\t\t${cell1}\n` - } - output += '\t\n' - - output += '
${column}
${item.countryFlag} ${name}
${item.channelCount}${item.guideUrl}${status}
' - - return output -} - -main() diff --git a/scripts/update-readme.ts b/scripts/update-readme.ts new file mode 100644 index 00000000..161866c6 --- /dev/null +++ b/scripts/update-readme.ts @@ -0,0 +1,164 @@ +const parser = require('epg-parser') +const markdownInclude = require('markdown-include') +const countries = require('./countries.json') +const file = require('./file') + +type EPG = { + channels: Channel[] + programs: Program[] +} + +type Country = { + flag: string + name: string + code: string + states?: State[] +} + +type State = { + name: string + code: string +} + +type Channel = { + id: string +} + +type Program = { + channel: string +} + +type Guide = { + flag: string + name: string + url: string + channelCount: number + emptyGuides: number +} + +async function main() { + console.log('Starting...') + file + .list('.gh-pages/guides/**/*.xml') + .then((files: string[]) => { + let guidesByCountry: Guide[] = [] + let guidesByUSState: Guide[] = [] + let guidesByCanadaProvince: Guide[] = [] + files.forEach((filename: string) => { + const matches: string[] = filename.match(/\.gh\-pages\/guides\/(.*)\/.*/i) || [] + const code: string | undefined = matches[1] + if (code === undefined) return + + const xml = file.read(filename) + let epg: EPG = parser.parse(xml) + let emptyGuides = 0 + epg.channels.forEach((channel: Channel) => { + const showCount = epg.programs.filter( + (program: Program) => program.channel === channel.id + ).length + if (showCount === 0) emptyGuides++ + }) + + const [_, stateCode] = code.split('-') + const country: Country | undefined = countries[code] + const us_state: State | undefined = countries['us'] + ? countries['us'].states[stateCode] + : undefined + const ca_province: State | undefined = countries['ca'] + ? countries['ca'].states[stateCode] + : undefined + + if (country !== undefined) { + guidesByCountry.push({ + flag: country.flag, + name: country.name, + url: filename.replace('.gh-pages', 'https://iptv-org.github.io/epg'), + channelCount: epg.channels.length, + emptyGuides + }) + guidesByCountry = sortGuides(guidesByCountry) + } else if (us_state !== undefined) { + guidesByUSState.push({ + flag: '', + name: us_state.name, + url: filename.replace('.gh-pages', 'https://iptv-org.github.io/epg'), + channelCount: epg.channels.length, + emptyGuides + }) + guidesByUSState = sortGuides(guidesByUSState) + } else if (ca_province !== undefined) { + guidesByCanadaProvince.push({ + flag: '', + name: ca_province.name, + url: filename.replace('.gh-pages', 'https://iptv-org.github.io/epg'), + channelCount: epg.channels.length, + emptyGuides + }) + guidesByCanadaProvince = sortGuides(guidesByCanadaProvince) + } + }) + + console.log('Generating country table...') + const countryTable = generateTable(guidesByCountry, ['Country', 'Channels', 'EPG', 'Status']) + file.write('.readme/_countries.md', countryTable) + + console.log('Generating US states table...') + const usStatesTable = generateTable(guidesByUSState, ['State', 'Channels', 'EPG', 'Status']) + file.write('.readme/_us-states.md', usStatesTable) + + console.log('Generating Canada provinces table...') + const caProvincesTable = generateTable(guidesByCanadaProvince, [ + 'Province', + 'Channels', + 'EPG', + 'Status' + ]) + file.write('.readme/_ca-provinces.md', caProvincesTable) + + console.log('Updating README.md...') + markdownInclude.compileFiles('.readme/config.json') + }) + .finally(() => { + console.log('Finish') + }) +} + +function generateTable(guides: Guide[], header: string[]) { + let output = '\n' + + output += '\t\n\t\t' + for (let column of header) { + output += `` + } + output += '\n\t\n' + + output += '\t\n' + for (let guide of guides) { + const size = guides.filter((g: Guide) => g.name === guide.name).length + let root = output.indexOf(guide.name) === -1 + const rowspan = root && size > 1 ? ` rowspan="${size}"` : '' + const name = `${guide.flag} ${guide.name}` + let status = '🟢' + if (guide.emptyGuides === guide.channelCount) status = '🔴' + else if (guide.emptyGuides > 0) status = '🟡' + const cell1 = root ? `` : '' + output += `\t\t${cell1}\n` + } + output += '\t\n' + + output += '
${column}
${name}
${guide.channelCount}${guide.url}${status}
' + + return output +} + +function sortGuides(guides: Guide[]): Guide[] { + return guides.sort((a, b) => { + var countryNameA = a.name.toLowerCase() + var countryNameB = b.name.toLowerCase() + if (countryNameA < countryNameB) return -1 + if (countryNameA > countryNameB) return 1 + return b.channelCount - a.channelCount + }) +} + +main()