Update utils.js

pull/2087/head
freearhey
parent 65471a2c2d
commit 9b317ce5a6

@ -7,9 +7,157 @@ const escapeStringRegexp = require('escape-string-regexp')
const markdownInclude = require('markdown-include') const markdownInclude = require('markdown-include')
const iso6393 = require('iso-639-3') const iso6393 = require('iso-639-3')
let helper = {} const utils = {}
utils.supportedCategories = [
{
name: 'Auto',
id: 'auto',
nsfw: false
},
{
name: 'Business',
id: 'business',
nsfw: false
},
{
name: 'Classic',
id: 'classic',
nsfw: false
},
{
name: 'Comedy',
id: 'comedy',
nsfw: false
},
{
name: 'Documentary',
id: 'documentary',
nsfw: false
},
{
name: 'Education',
id: 'education',
nsfw: false
},
{
name: 'Entertainment',
id: 'entertainment',
nsfw: false
},
{
name: 'Family',
id: 'family',
nsfw: false
},
{
name: 'Fashion',
id: 'fashion',
nsfw: false
},
{
name: 'Food',
id: 'food',
nsfw: false
},
{
name: 'General',
id: 'general',
nsfw: false
},
{
name: 'Health',
id: 'health',
nsfw: false
},
{
name: 'History',
id: 'history',
nsfw: false
},
{
name: 'Hobby',
id: 'hobby',
nsfw: false
},
{
name: 'Kids',
id: 'kids',
nsfw: false
},
{
name: 'Legislative',
id: 'legislative',
nsfw: false
},
{
name: 'Lifestyle',
id: 'lifestyle',
nsfw: false
},
{
name: 'Local',
id: 'local',
nsfw: false
},
{
name: 'Movies',
id: 'movies',
nsfw: false
},
{
name: 'Music',
id: 'music',
nsfw: false
},
{
name: 'News',
id: 'news',
nsfw: false
},
{
name: 'Quiz',
id: 'quiz',
nsfw: false
},
{
name: 'Religious',
id: 'religious',
nsfw: false
},
{
name: 'Sci-Fi',
id: 'sci-fi',
nsfw: false
},
{
name: 'Shop',
id: 'shop',
nsfw: false
},
{
name: 'Sport',
id: 'sport',
nsfw: false
},
{
name: 'Travel',
id: 'travel',
nsfw: false
},
{
name: 'Weather',
id: 'weather',
nsfw: false
},
{
name: 'XXX',
id: 'xxx',
nsfw: true
}
]
helper.code2flag = function (code) { utils.code2flag = function (code) {
switch (code) { switch (code) {
case 'uk': case 'uk':
return '🇬🇧' return '🇬🇧'
@ -24,9 +172,16 @@ helper.code2flag = function (code) {
} }
} }
helper.code2name = function (code) { utils.code2name = function (code) {
switch (code.toLowerCase()) {
case 'int':
return 'International'
case 'us':
return 'United States'
}
const intlDisplayNames = new Intl.DisplayNames(['en'], { const intlDisplayNames = new Intl.DisplayNames(['en'], {
style: 'long', style: 'narrow',
type: 'region' type: 'region'
}) })
@ -37,18 +192,34 @@ helper.code2name = function (code) {
} }
} }
helper.language2code = function (name) { utils.language2code = function (name) {
const lang = iso6393.find(l => l.name === name) const lang = iso6393.find(l => l.name === name)
return lang && lang.iso6393 ? lang.iso6393 : null return lang && lang.iso6393 ? lang.iso6393 : null
} }
helper.sortBy = function (arr, fields) { utils.sortBy = function (arr, fields) {
return arr.sort((a, b) => { return arr.sort((a, b) => {
for (let field of fields) { for (let field of fields) {
let propA = a[field] ? a[field].toLowerCase() : '' let propA = a[field] ? a[field].toLowerCase() : ''
let propB = b[field] ? b[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) { if (propA < propB) {
return -1 return -1
} }
@ -60,7 +231,7 @@ helper.sortBy = function (arr, fields) {
}) })
} }
helper.loadEPG = function (url) { utils.loadEPG = function (url) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var buffer = [] var buffer = []
axios({ axios({
@ -96,11 +267,11 @@ helper.loadEPG = function (url) {
}) })
} }
helper.getBasename = function (filename) { utils.getBasename = function (filename) {
return path.basename(filename, path.extname(filename)) return path.basename(filename, path.extname(filename))
} }
helper.filterPlaylists = function (arr, include = '', exclude = '') { utils.filterPlaylists = function (arr, include = '', exclude = '') {
if (include) { if (include) {
const included = include.split(',').map(filename => `channels/${filename}.m3u`) const included = include.split(',').map(filename => `channels/${filename}.m3u`)
@ -116,7 +287,7 @@ helper.filterPlaylists = function (arr, include = '', exclude = '') {
return arr return arr
} }
helper.generateTable = function (data, options) { utils.generateTable = function (data, options) {
let output = '<table>\n' let output = '<table>\n'
output += '\t<thead>\n\t\t<tr>' output += '\t<thead>\n\t\t<tr>'
@ -145,77 +316,48 @@ helper.generateTable = function (data, options) {
return output return output
} }
helper.createDir = function (dir) { utils.createDir = function (dir) {
if (!fs.existsSync(dir)) { if (!fs.existsSync(dir)) {
fs.mkdirSync(dir) fs.mkdirSync(dir)
} }
} }
helper.readFile = function (filename) { utils.readFile = function (filename) {
return fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: 'utf8' }) return fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: 'utf8' })
} }
helper.appendToFile = function (filename, data) { utils.appendToFile = function (filename, data) {
fs.appendFileSync(path.resolve(__dirname) + '/../' + filename, data) fs.appendFileSync(path.resolve(__dirname) + '/../' + filename, data)
} }
helper.compileMarkdown = function (filepath) { utils.compileMarkdown = function (filepath) {
return markdownInclude.compileFiles(path.resolve(__dirname, filepath)) return markdownInclude.compileFiles(path.resolve(__dirname, filepath))
} }
helper.escapeStringRegexp = function (scring) { utils.escapeStringRegexp = function (scring) {
return escapeStringRegexp(string) return escapeStringRegexp(string)
} }
helper.createFile = function (filename, data = '') { utils.createFile = function (filename, data = '') {
fs.writeFileSync(path.resolve(__dirname) + '/../' + filename, data) fs.writeFileSync(path.resolve(__dirname) + '/../' + filename, data)
} }
helper.writeToLog = function (country, msg, url) { utils.writeToLog = function (country, msg, url) {
var now = new Date() var now = new Date()
var line = `${country}: ${msg} '${url}'` var line = `${country}: ${msg} '${url}'`
this.appendToFile('error.log', now.toISOString() + ' ' + line + '\n') this.appendToFile('error.log', now.toISOString() + ' ' + line + '\n')
} }
helper.filterNSFW = function (arr) { utils.filterNSFW = function (arr) {
const sfwCategories = [ const sfwCategories = utils.supportedCategories.filter(c => !c.nsfw).map(c => c.name)
'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'
]
return arr.filter(i => sfwCategories.includes(i.category)) return arr.filter(i => sfwCategories.includes(i.category))
} }
helper.sleep = function (ms) { utils.sleep = function (ms) {
return function (x) { return function (x) {
return new Promise(resolve => setTimeout(() => resolve(x), ms)) return new Promise(resolve => setTimeout(() => resolve(x), ms))
} }
} }
module.exports = helper module.exports = utils

Loading…
Cancel
Save