diff --git a/helpers/format.js b/helpers/format.js index bf3c48dd0..421788da3 100644 --- a/helpers/format.js +++ b/helpers/format.js @@ -87,14 +87,16 @@ async function main() { for(let channel of channels) { for(let channelId in buffer[epgUrl].channels) { let c = buffer[epgUrl].channels[channelId] - for(let epgName of c.names) { - epgName = escapeStringRegexp(epgName) - channelTitle = channel.title.replace(/(fhd|hd|sd|高清)$/i, '').trim() - let regexp = new RegExp(`^${epgName}$`, 'i') - if(regexp.test(channelTitle)) { - if(!channel.id) { - channel.id = c.id - continue + for(let epgName of c.name) { + if(epgName.value) { + let escaped = escapeStringRegexp(epgName.value) + channelTitle = channel.title.replace(/(fhd|hd|sd|高清)$/i, '').trim() + let regexp = new RegExp(`^${escaped}$`, 'i') + if(regexp.test(channelTitle)) { + if(!channel.id) { + channel.id = c.id + continue + } } } } @@ -111,16 +113,16 @@ async function main() { if(!c) continue let updated = false - if(!channel.name && c.names[0]) { - channel.name = c.names[0] + if(!channel.name && c.name.length) { + channel.name = c.name[0].value updated = true if(verbose) { - console.log(`Added name '${c.names[0]}' to '${channel.id}'`) + console.log(`Added name '${c.name[0].value}' to '${channel.id}'`) } } - if(!channel.logo && c.icon) { - const icon = c.icon.split('|')[0] + if(!channel.logo && c.icon.length) { + const icon = c.icon[0].split('|')[0] channel.logo = icon updated = true if(verbose) { diff --git a/helpers/util.js b/helpers/util.js index d982de91b..f815a8afd 100644 --- a/helpers/util.js +++ b/helpers/util.js @@ -3,7 +3,7 @@ const path = require('path') const parser = require('iptv-playlist-parser') const axios = require('axios') const zlib = require("zlib") -const DOMParser = require('xmldom').DOMParser +const epgParser = require('epg-parser') const urlParser = require('url') const supportedCategories = [ 'Auto','Business', 'Classic','Comedy','Documentary','Education','Entertainment', 'Family','Fashion','Food', 'General', 'Health', 'History', 'Hobby', 'Kids', 'Legislative','Lifestyle','Local', 'Movies', 'Music', 'News', 'Quiz', 'Religious','Sci-Fi', 'Shop', 'Sport', 'Travel', 'Weather', 'XXX' ] @@ -78,38 +78,20 @@ function createChannel(data) { } async function loadEPG(url) { - const data = await getGzipped(url) - const doc = new DOMParser().parseFromString(data, 'text/xml') - const channelElements = doc.getElementsByTagName('channel') - let channels = {} - for(let i = 0; i < channelElements.length; i++) { - let channel = {} - let channelElement = channelElements[i] - channel.id = channelElement.getAttribute('id') - channel.names = [] - for(let nameElement of Object.values(channelElement.getElementsByTagName('display-name'))) { - if(nameElement.firstChild) { - channel.names.push(nameElement.firstChild.nodeValue) - } - } - channel.names = channel.names.filter(n => n) - const iconElements = channelElement.getElementsByTagName('icon') - if(iconElements.length) { - channel.icon = iconElements[0].getAttribute('src') - } - + const content = await getEPGFile(url) + const result = epgParser.parse(content) + const channels = {} + for(let channel of result.channels) { channels[channel.id] = channel } return Promise.resolve({ url, - channels + channels }) } -function getGzipped(url) { - const supportedTypes = ['application/x-gzip', 'application/octet-stream'] - +function getEPGFile(url) { return new Promise((resolve, reject) => { var buffer = [] axios({ @@ -118,7 +100,7 @@ function getGzipped(url) { responseType:'stream' }).then(res => { let stream - if(supportedTypes.indexOf(res.headers['content-type']) > -1) { + if(/\.gz$/i.test(url)) { let gunzip = zlib.createGunzip() res.data.pipe(gunzip) stream = gunzip diff --git a/package-lock.json b/package-lock.json index a439baefd..dc8562333 100644 --- a/package-lock.json +++ b/package-lock.json @@ -53,6 +53,15 @@ "xregexp": "^4.2.4" } }, + "epg-parser": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/epg-parser/-/epg-parser-0.1.1.tgz", + "integrity": "sha512-gzbrIcpoI+yzX2GK20Whuqp7DjbHTLX/NI9eY7Y82qqreJuZcFRs/SDXyeZ2Tp5CqcTgpFvD/Op3I1mDQG8NGQ==", + "dev": true, + "requires": { + "xml-js": "^1.6.11" + } + }, "escape-string-regexp": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-2.0.0.tgz", @@ -133,6 +142,12 @@ "integrity": "sha512-naKIZz2GQ8JWh///G7L3X6LaQUAMp2lvb1rvwwsURe/VXwD6VMfr+/1NuNw3ag8v2kY1aQ/go5SNn79O9JU7yw==", "dev": true }, + "sax": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", + "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==", + "dev": true + }, "which": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", @@ -142,6 +157,15 @@ "isexe": "^2.0.0" } }, + "xml-js": { + "version": "1.6.11", + "resolved": "https://registry.npmjs.org/xml-js/-/xml-js-1.6.11.tgz", + "integrity": "sha512-7rVi2KMfwfWFl+GpPg6m80IVMWXLRjO+PxTq7V2CDhoGak0wzYzFgUY2m4XJ47OGdXd8eLE8EmwfAmdjw7lC1g==", + "dev": true, + "requires": { + "sax": "^1.2.4" + } + }, "xmldom": { "version": "0.1.27", "resolved": "https://registry.npmjs.org/xmldom/-/xmldom-0.1.27.tgz", diff --git a/package.json b/package.json index d7c905783..9dcfaa3be 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "dependencies": {}, "devDependencies": { "axios": ">=0.18.1", + "epg-parser": "^0.1.1", "escape-string-regexp": "^2.0.0", "fluent-ffmpeg": "^2.1.2", "iptv-playlist-parser": "^0.2.2",