mirror of
https://github.com/iptv-org/iptv.git
synced 2025-05-12 18:10:04 -04:00
Small refactoring
This commit is contained in:
parent
10ff2eac6a
commit
34efa18658
2 changed files with 89 additions and 84 deletions
104
helpers/util.js
104
helpers/util.js
|
@ -52,27 +52,41 @@ class Channel {
|
|||
}
|
||||
}
|
||||
|
||||
function getGzipped(url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
var buffer = []
|
||||
https.get(url, function(res) {
|
||||
var gunzip = zlib.createGunzip()
|
||||
res.pipe(gunzip)
|
||||
gunzip.on('data', function(data) {
|
||||
buffer.push(data.toString())
|
||||
}).on("end", function() {
|
||||
resolve(buffer.join(""))
|
||||
}).on("error", function(e) {
|
||||
reject(e)
|
||||
})
|
||||
}).on('error', function(e) {
|
||||
reject(e)
|
||||
})
|
||||
function parsePlaylist(filename) {
|
||||
const parser = new M3U8FileParser()
|
||||
const content = readFile(filename)
|
||||
parser.read(content)
|
||||
let results = parser.getResult()
|
||||
let contentMatches = content.match(/^.+(?=#|\n|\r)/g)
|
||||
let head = contentMatches.length ? contentMatches[0] : null
|
||||
let attrs = {}
|
||||
if(head) {
|
||||
const parts = head.split(' ').filter(p => p !== '#EXTM3U').filter(p => p)
|
||||
|
||||
for(const attr of parts) {
|
||||
let attrParts = attr.split('=')
|
||||
|
||||
attrs[attrParts[0]] = attrParts[1].replace(/\"/g, '')
|
||||
}
|
||||
}
|
||||
|
||||
results.attrs = attrs
|
||||
|
||||
return new Playlist({
|
||||
attrs: results.attrs,
|
||||
items: results.segments
|
||||
})
|
||||
}
|
||||
|
||||
function readFile(filename) {
|
||||
return fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: "utf8" })
|
||||
function createChannel(data) {
|
||||
return new Channel({
|
||||
id: data.id,
|
||||
name: data.name,
|
||||
logo: data.logo,
|
||||
group: data.group,
|
||||
url: data.url,
|
||||
title: data.title
|
||||
})
|
||||
}
|
||||
|
||||
async function loadEPG(url) {
|
||||
|
@ -105,40 +119,22 @@ async function loadEPG(url) {
|
|||
})
|
||||
}
|
||||
|
||||
function createChannel(data) {
|
||||
return new Channel({
|
||||
id: data.id,
|
||||
name: data.name,
|
||||
logo: data.logo,
|
||||
group: data.group,
|
||||
url: data.url,
|
||||
title: data.title
|
||||
})
|
||||
}
|
||||
|
||||
function parsePlaylist(filename) {
|
||||
const parser = new M3U8FileParser()
|
||||
const content = readFile(filename)
|
||||
parser.read(content)
|
||||
let results = parser.getResult()
|
||||
let contentMatches = content.match(/^.+(?=#|\n|\r)/g)
|
||||
let head = contentMatches.length ? contentMatches[0] : null
|
||||
let attrs = {}
|
||||
if(head) {
|
||||
const parts = head.split(' ').filter(p => p !== '#EXTM3U').filter(p => p)
|
||||
|
||||
for(const attr of parts) {
|
||||
let attrParts = attr.split('=')
|
||||
|
||||
attrs[attrParts[0]] = attrParts[1].replace(/\"/g, '')
|
||||
}
|
||||
}
|
||||
|
||||
results.attrs = attrs
|
||||
|
||||
return new Playlist({
|
||||
attrs: results.attrs,
|
||||
items: results.segments
|
||||
function getGzipped(url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
var buffer = []
|
||||
https.get(url, function(res) {
|
||||
var gunzip = zlib.createGunzip()
|
||||
res.pipe(gunzip)
|
||||
gunzip.on('data', function(data) {
|
||||
buffer.push(data.toString())
|
||||
}).on("end", function() {
|
||||
resolve(buffer.join(""))
|
||||
}).on("error", function(e) {
|
||||
reject(e)
|
||||
})
|
||||
}).on('error', function(e) {
|
||||
reject(e)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -159,6 +155,10 @@ function sortByTitle(arr) {
|
|||
return arr.sort(byTitle)
|
||||
}
|
||||
|
||||
function readFile(filename) {
|
||||
return fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: "utf8" })
|
||||
}
|
||||
|
||||
function appendToFile(filename, data) {
|
||||
fs.appendFileSync(path.resolve(__dirname) + '/../' + filename, data)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue