From fa4fd2fa89b2b8485233ef8a4232b0dd5845bddc Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Wed, 13 Oct 2021 05:23:12 +0300 Subject: [PATCH] Update update-codes.js --- scripts/update-codes.js | 61 +++++++++++------------------------------ 1 file changed, 16 insertions(+), 45 deletions(-) diff --git a/scripts/update-codes.js b/scripts/update-codes.js index 4b786afc..dcb8ae9d 100644 --- a/scripts/update-codes.js +++ b/scripts/update-codes.js @@ -1,9 +1,6 @@ -const fs = require('fs') -const path = require('path') -const convert = require('xml-js') -const axios = require('axios') const countries = require('./countries.json') const file = require('./file.js') +const parser = require('epg-parser') async function main() { console.log('Starting...') @@ -13,13 +10,19 @@ async function main() { let codes = {} for (const filename of files) { const url = filename.replace('.gh-pages', 'https://iptv-org.github.io/epg') - const channels = parseChannels(file.read(filename)) - channels.forEach(channel => { - if (!codes[channel.tvg_id]) { + const epg = file.read(filename) + const parsed = parser.parse(epg) + parsed.channels.forEach(channel => { + if (!codes[channel.id]) { channel.guides = [url] - codes[channel.tvg_id] = channel + codes[channel.id] = { + tvg_id: channel.id, + display_name: channel.name[0].value, + logo: channel.icon[0], + country: channel.id.split('.')[1] + } } else { - codes[channel.tvg_id].guides.push(url) + codes[channel.id].guides.push(url) } }) } @@ -29,7 +32,8 @@ async function main() { if (a.display_name.toLowerCase() > b.display_name.toLowerCase()) return 1 return 0 }) - writeToFile('.gh-pages/codes.json', convertToJSON(sorted)) + console.log(`Saving '.gh-pages/codes.json'...`) + file.write('.gh-pages/codes.json', JSON.stringify(sorted)) const _items = {} countries.forEach(country => { @@ -47,43 +51,10 @@ async function main() { item.channels.push(channel) } }) - writeToFile('.gh-pages/items.json', convertToJSON(_items)) + console.log(`Saving '.gh-pages/items.json'...`) + file.write('.gh-pages/items.json', JSON.stringify(_items)) console.log('Done') } -function writeToFile(filename, data) { - console.log(`Saving all codes to '${filename}'...`) - const dir = path.resolve(path.dirname(filename)) - if (!fs.existsSync(dir)) { - fs.mkdirSync(dir, { recursive: true }) - } - - fs.writeFileSync(path.resolve(filename), data) -} - -function convertToJSON(arr) { - return JSON.stringify(arr) -} - -function parseChannels(xml) { - const result = convert.xml2js(xml) - const tv = result.elements.find(el => el.name === 'tv') - - return tv.elements - .filter(el => el.name === 'channel') - .map(channel => { - const tvg_id = (channel.attributes || { id: '' }).id - const logo = (channel.elements.find(el => el.name === 'icon') || { attributes: { src: '' } }) - .attributes.src - const displayName = channel.elements.find(el => el.name === 'display-name') - const display_name = displayName - ? displayName.elements.find(el => el.type === 'text').text - : null - const country = tvg_id.split('.')[1] - - return { tvg_id, display_name, logo, country } - }) -} - main()