Installed epg-parser package

This commit is contained in:
freearhey 2019-10-30 18:44:26 +03:00
parent bb9e23e738
commit fc39bcebce
4 changed files with 48 additions and 39 deletions

View File

@ -87,14 +87,16 @@ async function main() {
for(let channel of channels) { for(let channel of channels) {
for(let channelId in buffer[epgUrl].channels) { for(let channelId in buffer[epgUrl].channels) {
let c = buffer[epgUrl].channels[channelId] let c = buffer[epgUrl].channels[channelId]
for(let epgName of c.names) { for(let epgName of c.name) {
epgName = escapeStringRegexp(epgName) if(epgName.value) {
channelTitle = channel.title.replace(/(fhd|hd|sd|高清)$/i, '').trim() let escaped = escapeStringRegexp(epgName.value)
let regexp = new RegExp(`^${epgName}$`, 'i') channelTitle = channel.title.replace(/(fhd|hd|sd|高清)$/i, '').trim()
if(regexp.test(channelTitle)) { let regexp = new RegExp(`^${escaped}$`, 'i')
if(!channel.id) { if(regexp.test(channelTitle)) {
channel.id = c.id if(!channel.id) {
continue channel.id = c.id
continue
}
} }
} }
} }
@ -111,16 +113,16 @@ async function main() {
if(!c) continue if(!c) continue
let updated = false let updated = false
if(!channel.name && c.names[0]) { if(!channel.name && c.name.length) {
channel.name = c.names[0] channel.name = c.name[0].value
updated = true updated = true
if(verbose) { 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) { if(!channel.logo && c.icon.length) {
const icon = c.icon.split('|')[0] const icon = c.icon[0].split('|')[0]
channel.logo = icon channel.logo = icon
updated = true updated = true
if(verbose) { if(verbose) {

View File

@ -3,7 +3,7 @@ const path = require('path')
const parser = require('iptv-playlist-parser') const parser = require('iptv-playlist-parser')
const axios = require('axios') const axios = require('axios')
const zlib = require("zlib") const zlib = require("zlib")
const DOMParser = require('xmldom').DOMParser const epgParser = require('epg-parser')
const urlParser = require('url') 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' ] 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) { async function loadEPG(url) {
const data = await getGzipped(url) const content = await getEPGFile(url)
const doc = new DOMParser().parseFromString(data, 'text/xml') const result = epgParser.parse(content)
const channelElements = doc.getElementsByTagName('channel') const channels = {}
let channels = {} for(let channel of result.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')
}
channels[channel.id] = channel channels[channel.id] = channel
} }
return Promise.resolve({ return Promise.resolve({
url, url,
channels channels
}) })
} }
function getGzipped(url) { function getEPGFile(url) {
const supportedTypes = ['application/x-gzip', 'application/octet-stream']
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var buffer = [] var buffer = []
axios({ axios({
@ -118,7 +100,7 @@ function getGzipped(url) {
responseType:'stream' responseType:'stream'
}).then(res => { }).then(res => {
let stream let stream
if(supportedTypes.indexOf(res.headers['content-type']) > -1) { if(/\.gz$/i.test(url)) {
let gunzip = zlib.createGunzip() let gunzip = zlib.createGunzip()
res.data.pipe(gunzip) res.data.pipe(gunzip)
stream = gunzip stream = gunzip

24
package-lock.json generated
View File

@ -53,6 +53,15 @@
"xregexp": "^4.2.4" "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": { "escape-string-regexp": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-2.0.0.tgz", "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==", "integrity": "sha512-naKIZz2GQ8JWh///G7L3X6LaQUAMp2lvb1rvwwsURe/VXwD6VMfr+/1NuNw3ag8v2kY1aQ/go5SNn79O9JU7yw==",
"dev": true "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": { "which": {
"version": "1.3.1", "version": "1.3.1",
"resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz",
@ -142,6 +157,15 @@
"isexe": "^2.0.0" "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": { "xmldom": {
"version": "0.1.27", "version": "0.1.27",
"resolved": "https://registry.npmjs.org/xmldom/-/xmldom-0.1.27.tgz", "resolved": "https://registry.npmjs.org/xmldom/-/xmldom-0.1.27.tgz",

View File

@ -12,6 +12,7 @@
"dependencies": {}, "dependencies": {},
"devDependencies": { "devDependencies": {
"axios": ">=0.18.1", "axios": ">=0.18.1",
"epg-parser": "^0.1.1",
"escape-string-regexp": "^2.0.0", "escape-string-regexp": "^2.0.0",
"fluent-ffmpeg": "^2.1.2", "fluent-ffmpeg": "^2.1.2",
"iptv-playlist-parser": "^0.2.2", "iptv-playlist-parser": "^0.2.2",