mirror of
https://github.com/iptv-org/epg
synced 2026-05-11 03:47:02 -04:00
epgshare part 3 + fix memory heap issues
This commit is contained in:
@@ -2,6 +2,8 @@ const axios = require('axios')
|
||||
const iconv = require('iconv-lite')
|
||||
const parser = require('epg-parser')
|
||||
const { ungzip } = require('pako')
|
||||
const zlib = require('zlib')
|
||||
const sax = require('sax')
|
||||
|
||||
let cachedContent
|
||||
|
||||
@@ -36,26 +38,72 @@ module.exports = {
|
||||
return programs
|
||||
},
|
||||
async channels({ tag }) {
|
||||
const buffer = await axios
|
||||
.get(`https://epgshare01.online/epgshare01/epg_ripper_${tag}.xml.gz`, {
|
||||
responseType: 'arraybuffer'
|
||||
const url = `https://epgshare01.online/epgshare01/epg_ripper_${tag}.xml.gz`
|
||||
try {
|
||||
const res = await axios.get(url, { responseType: 'stream' })
|
||||
|
||||
const parserStream = sax.createStream(true, { trim: true })
|
||||
const channels = []
|
||||
let current = null
|
||||
let curText = ''
|
||||
let curTag = null
|
||||
|
||||
parserStream.on('opentag', node => {
|
||||
const name = node.name.toLowerCase()
|
||||
if (name === 'channel') {
|
||||
current = { id: node.attributes.id || node.attributes.ID, displayName: [] }
|
||||
} else if (current && (name === 'display-name' || name === 'displayname')) {
|
||||
curTag = 'displayName'
|
||||
curText = ''
|
||||
// capture possible lang attribute (xml:lang or lang)
|
||||
current._lang = node.attributes['xml:lang'] || node.attributes['xml:Lang'] || node.attributes.lang
|
||||
}
|
||||
})
|
||||
.then(r => r.data)
|
||||
.catch(console.error)
|
||||
|
||||
const content = ungzip(buffer)
|
||||
const encoded = iconv.decode(content, 'utf8')
|
||||
const { channels } = parser.parse(encoded)
|
||||
parserStream.on('text', text => {
|
||||
if (curTag === 'displayName') curText += text
|
||||
})
|
||||
|
||||
return channels.map(channel => {
|
||||
const displayName = channel.displayName[0]
|
||||
parserStream.on('cdata', text => {
|
||||
if (curTag === 'displayName') curText += text
|
||||
})
|
||||
|
||||
return {
|
||||
lang: displayName.lang || 'en',
|
||||
site_id: `${tag}#${channel.id}`,
|
||||
name: displayName.value
|
||||
}
|
||||
})
|
||||
parserStream.on('closetag', nameRaw => {
|
||||
const name = nameRaw.toLowerCase()
|
||||
if (current && (name === 'display-name' || name === 'displayname')) {
|
||||
current.displayName.push({
|
||||
lang: current._lang || 'en',
|
||||
value: curText.trim()
|
||||
})
|
||||
curTag = null
|
||||
curText = ''
|
||||
current._lang = undefined
|
||||
} else if (name === 'channel' && current) {
|
||||
channels.push({ id: current.id, displayName: current.displayName })
|
||||
current = null
|
||||
}
|
||||
})
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
res.data
|
||||
.pipe(zlib.createGunzip())
|
||||
.pipe(parserStream)
|
||||
.on('end', resolve)
|
||||
.on('error', reject)
|
||||
})
|
||||
|
||||
return channels.map(channel => {
|
||||
const displayName = (channel.displayName && channel.displayName[0]) || { lang: 'en', value: channel.id }
|
||||
return {
|
||||
lang: displayName.lang || 'en',
|
||||
site_id: `${tag}#${channel.id}`,
|
||||
name: displayName.value
|
||||
}
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user