const fs = require('fs') const path = require('path') const axios = require('axios') const zlib = require('zlib') const urlParser = require('url') const escapeStringRegexp = require('escape-string-regexp') const markdownInclude = require('markdown-include') const iso6393 = require('iso-639-3') const regions = require('./regions') const categories = require('./categories') const intlDisplayNames = new Intl.DisplayNames(['en'], { style: 'narrow', type: 'region' }) const utils = {} utils.code2flag = function (code) { code = code.toUpperCase() switch (code) { case 'UK': return '🇬🇧' case 'INT': return '🌍' case 'UNSORTED': return '' default: return code.replace(/./g, char => String.fromCodePoint(char.charCodeAt(0) + 127397)) } } utils.region2codes = function (region) { region = region.toUpperCase() return regions[region] ? regions[region].codes : [] } utils.code2name = function (code) { try { code = code.toUpperCase() if (regions[code]) return regions[code].name if (code === 'US') return 'United States' if (code === 'INT') return 'International' return intlDisplayNames.of(code) } catch (e) { return null } } utils.language2code = function (name) { const lang = iso6393.find(l => l.name === name) return lang && lang.iso6393 ? lang.iso6393 : null } utils.sortBy = function (arr, fields) { return arr.sort((a, b) => { for (let field of fields) { let propA = a[field] ? a[field].toLowerCase() : '' let propB = b[field] ? b[field].toLowerCase() : '' if (propA === 'undefined') { return 1 } if (propB === 'undefined') { return -1 } if (propA === 'other') { return 1 } if (propB === 'other') { return -1 } if (propA < propB) { return -1 } if (propA > propB) { return 1 } } return 0 }) } utils.loadEPG = function (url) { return new Promise((resolve, reject) => { var buffer = [] axios({ method: 'get', 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) { reject(e) }) }) .catch(e => { reject(e) }) }) } utils.getBasename = function (filename) { return path.basename(filename, path.extname(filename)) } utils.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) { const excluded = exclude.split(',').map(filename => `channels/${filename}.m3u`) return arr.filter(i => excluded.indexOf(i.url) === -1) } return arr } utils.generateTable = function (data, options) { let output = '
${column.name} | ` } output += '
---|
${item[prop]} | ` i++ } output += '