diff --git a/scripts/helper.js b/scripts/helper.js index 2e780bdec..d419a9474 100644 --- a/scripts/helper.js +++ b/scripts/helper.js @@ -1,8 +1,8 @@ -const fs = require("fs") +const fs = require('fs') const path = require('path') const playlistParser = require('iptv-playlist-parser') const axios = require('axios') -const zlib = require("zlib") +const zlib = require('zlib') const epgParser = require('epg-parser') const urlParser = require('url') const ISO6391 = require('iso-639-1') @@ -12,60 +12,64 @@ const markdownInclude = require('markdown-include') let cache = {} let helper = {} -helper.sortBy = function(arr, fields) { +helper.sortBy = function (arr, fields) { return arr.sort((a, b) => { - for(let field of fields) { - if(a[field].toLowerCase() < b[field].toLowerCase()) { return -1 } - if(a[field].toLowerCase() > b[field].toLowerCase()) { return 1 } + for (let field of fields) { + if (a[field].toLowerCase() < b[field].toLowerCase()) { + return -1 + } + if (a[field].toLowerCase() > b[field].toLowerCase()) { + return 1 + } } return 0 }) } -helper.createDir = function(dir) { +helper.createDir = function (dir) { if (!fs.existsSync(dir)) { fs.mkdirSync(dir) } } -helper.compileMarkdown = function(filepath) { +helper.compileMarkdown = function (filepath) { return markdownInclude.compileFiles(path.resolve(__dirname, filepath)) } -helper.escapeStringRegexp = function(scring) { +helper.escapeStringRegexp = function (scring) { return escapeStringRegexp(string) } -helper.getISO6391Name = function(code) { +helper.getISO6391Name = function (code) { return ISO6391.getName(code) } -helper.getISO6391Code = function(name) { +helper.getISO6391Code = function (name) { return ISO6391.getCode(name) } -helper.parsePlaylist = function(filename) { +helper.parsePlaylist = function (filename) { const content = this.readFile(filename) const result = playlistParser.parse(content) return new Playlist(result) } -helper.parseEPG = async function(url) { +helper.parseEPG = async function (url) { const content = await this.getEPG(url) const result = epgParser.parse(content) const channels = {} - for(let channel of result.channels) { + for (let channel of result.channels) { channels[channel.id] = channel } - return Promise.resolve({ - url, + return Promise.resolve({ + url, channels }) } -helper.getEPG = function(url) { +helper.getEPG = function (url) { return new Promise((resolve, reject) => { var buffer = [] axios({ @@ -73,46 +77,51 @@ helper.getEPG = function(url) { url: url, responseType: 'stream', timeout: 60000 - }).then(res => { - let stream - if(/\.gz$/i.test(url)) { - let gunzip = zlib.createGunzip() - res.data.pipe(gunzip) - stream = gunzip - } else { - stream = res.data - } - - stream.on('data', function(data) { - buffer.push(data.toString()) - }).on("end", function() { - resolve(buffer.join("")) - }).on("error", function(e) { + }) + .then(res => { + let stream + if (/\.gz$/i.test(url)) { + let gunzip = zlib.createGunzip() + res.data.pipe(gunzip) + stream = gunzip + } else { + stream = res.data + } + + stream + .on('data', function (data) { + buffer.push(data.toString()) + }) + .on('end', function () { + resolve(buffer.join('')) + }) + .on('error', function (e) { + reject(e) + }) + }) + .catch(e => { reject(e) }) - }).catch(e => { - reject(e) - }) }) } -helper.readFile = function(filename) { - return fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: "utf8" }) +helper.readFile = function (filename) { + return fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: 'utf8' }) } -helper.appendToFile = function(filename, data) { +helper.appendToFile = function (filename, data) { fs.appendFileSync(path.resolve(__dirname) + '/../' + filename, data) } -helper.createFile = function(filename, data = '') { +helper.createFile = function (filename, data = '') { fs.writeFileSync(path.resolve(__dirname) + '/../' + filename, data) } -helper.getBasename = function(filename) { +helper.getBasename = function (filename) { return path.basename(filename, path.extname(filename)) } -helper.getUrlPath = function(u) { +helper.getUrlPath = function (u) { let parsed = urlParser.parse(u) let searchQuery = parsed.search || '' let path = parsed.host + parsed.pathname + searchQuery @@ -120,23 +129,23 @@ helper.getUrlPath = function(u) { return path.toLowerCase() } -helper.filterPlaylists = function(arr, include = '', exclude = '') { - if(include) { +helper.filterPlaylists = function (arr, include = '', exclude = '') { + if (include) { const included = include.split(',').map(filename => `channels/${filename}.m3u`) - + return arr.filter(i => included.indexOf(i.url) > -1) } - if(exclude) { + if (exclude) { const excluded = exclude.split(',').map(filename => `channels/${filename}.m3u`) - + return arr.filter(i => excluded.indexOf(i.url) === -1) } return arr } -helper.generateTable = function(data, options) { +helper.generateTable = function (data, options) { let output = '