iptv/scripts/utils.js

380 lines
6.9 KiB
JavaScript
Raw Normal View History

2020-04-11 03:33:53 +02:00
const fs = require('fs')
2019-07-20 09:03:31 +02:00
const path = require('path')
const axios = require('axios')
2020-04-11 03:33:53 +02:00
const zlib = require('zlib')
const urlParser = require('url')
2019-11-02 10:45:09 +01:00
const escapeStringRegexp = require('escape-string-regexp')
const markdownInclude = require('markdown-include')
2020-04-18 16:16:56 +02:00
const iso6393 = require('iso-639-3')
2021-01-30 05:32:31 +01:00
const intlDisplayNames = new Intl.DisplayNames(['en'], {
style: 'narrow',
type: 'region'
})
2021-01-30 04:16:44 +01:00
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
}
]
2021-01-30 04:16:44 +01:00
utils.code2flag = function (code) {
2020-05-06 17:01:36 +02:00
switch (code) {
case 'uk':
return '🇬🇧'
case 'int':
return '🌎'
2020-09-17 22:30:28 +02:00
case 'unsorted':
return ''
2020-05-06 17:01:36 +02:00
default:
return code
.toUpperCase()
.replace(/./g, char => String.fromCodePoint(char.charCodeAt(0) + 127397))
}
}
2021-01-30 04:16:44 +01:00
utils.code2name = function (code) {
switch (code.toLowerCase()) {
case 'int':
return 'International'
case 'us':
return 'United States'
}
2021-01-29 23:58:52 +01:00
try {
return intlDisplayNames.of(code.toUpperCase())
} catch (e) {
return null
}
}
2021-01-30 05:32:31 +01:00
utils.codeIsValid = function (code) {
switch (code.toLowerCase()) {
case 'int':
return true
case 'us':
return true
}
try {
intlDisplayNames.of(code.toUpperCase())
return true
} catch (e) {
return false
}
}
2021-01-30 04:16:44 +01:00
utils.language2code = function (name) {
2021-01-29 23:58:52 +01:00
const lang = iso6393.find(l => l.name === name)
return lang && lang.iso6393 ? lang.iso6393 : null
}
2021-01-30 04:16:44 +01:00
utils.sortBy = function (arr, fields) {
2019-11-02 13:43:41 +01:00
return arr.sort((a, b) => {
2020-04-11 03:33:53 +02:00
for (let field of fields) {
2020-05-04 15:23:03 +02:00
let propA = a[field] ? a[field].toLowerCase() : ''
let propB = b[field] ? b[field].toLowerCase() : ''
2021-01-30 04:16:44 +01:00
if (propA === 'undefined') {
return 1
}
if (propB === 'undefined') {
return -1
}
if (propA === 'other') {
return 1
}
if (propB === 'other') {
return -1
}
2020-05-04 15:23:03 +02:00
if (propA < propB) {
2020-04-11 03:33:53 +02:00
return -1
}
2020-05-04 15:23:03 +02:00
if (propA > propB) {
2020-04-11 03:33:53 +02:00
return 1
}
2019-11-02 13:43:41 +01:00
}
return 0
})
}
2021-01-30 04:16:44 +01:00
utils.loadEPG = function (url) {
2019-08-07 15:51:34 +02:00
return new Promise((resolve, reject) => {
var buffer = []
axios({
method: 'get',
url: url,
2019-11-03 17:00:57 +01:00
responseType: 'stream',
2020-05-04 15:23:03 +02:00
timeout: 60000
2020-04-11 03:33:53 +02:00
})
2020-05-04 15:23:03 +02:00
.then(res => {
2020-04-11 03:33:53 +02:00
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)
})
})
2020-05-04 15:23:03 +02:00
.catch(e => {
2019-08-07 15:51:34 +02:00
reject(e)
})
})
2019-07-20 09:03:31 +02:00
}
2021-01-30 04:16:44 +01:00
utils.getBasename = function (filename) {
2021-01-29 23:58:52 +01:00
return path.basename(filename, path.extname(filename))
2019-08-07 15:51:34 +02:00
}
2021-01-30 04:16:44 +01:00
utils.filterPlaylists = function (arr, include = '', exclude = '') {
2021-01-29 23:58:52 +01:00
if (include) {
const included = include.split(',').map(filename => `channels/${filename}.m3u`)
2019-07-20 09:03:31 +02:00
2021-01-29 23:58:52 +01:00
return arr.filter(i => included.indexOf(i.url) > -1)
}
2019-07-20 09:03:31 +02:00
2021-01-29 23:58:52 +01:00
if (exclude) {
const excluded = exclude.split(',').map(filename => `channels/${filename}.m3u`)
2021-01-29 23:58:52 +01:00
return arr.filter(i => excluded.indexOf(i.url) === -1)
}
2021-01-29 23:58:52 +01:00
return arr
}
2021-01-30 04:16:44 +01:00
utils.generateTable = function (data, options) {
let output = '<table>\n'
2019-10-23 08:38:32 +02:00
output += '\t<thead>\n\t\t<tr>'
2019-10-23 08:38:32 +02:00
for (let column of options.columns) {
output += `<th align="${column.align}">${column.name}</th>`
}
output += '</tr>\n\t</thead>\n'
2019-10-23 08:38:32 +02:00
output += '\t<tbody>\n'
2019-10-23 08:38:32 +02:00
for (let item of data) {
output += '\t\t<tr>'
2019-10-23 08:38:32 +02:00
let i = 0
for (let prop in item) {
const column = options.columns[i]
let nowrap = column.nowrap
let align = column.align
output += `<td align="${align}"${nowrap ? ' nowrap' : ''}>${item[prop]}</td>`
i++
}
output += '</tr>\n'
2019-10-23 08:38:32 +02:00
}
output += '\t</tbody>\n'
2019-10-23 08:38:32 +02:00
output += '</table>'
return output
2019-10-09 04:33:52 +02:00
}
2021-01-30 04:16:44 +01:00
utils.createDir = function (dir) {
2021-01-29 23:58:52 +01:00
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
2019-11-02 15:30:12 +01:00
}
2021-01-30 04:16:44 +01:00
utils.readFile = function (filename) {
2021-01-29 23:58:52 +01:00
return fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: 'utf8' })
2019-11-03 18:21:35 +01:00
}
2021-01-30 04:16:44 +01:00
utils.appendToFile = function (filename, data) {
2021-01-29 23:58:52 +01:00
fs.appendFileSync(path.resolve(__dirname) + '/../' + filename, data)
}
2021-01-30 04:16:44 +01:00
utils.compileMarkdown = function (filepath) {
2021-01-29 23:58:52 +01:00
return markdownInclude.compileFiles(path.resolve(__dirname, filepath))
}
2021-01-30 04:16:44 +01:00
utils.escapeStringRegexp = function (scring) {
2021-01-29 23:58:52 +01:00
return escapeStringRegexp(string)
}
2021-01-30 04:16:44 +01:00
utils.createFile = function (filename, data = '') {
2021-01-29 23:58:52 +01:00
fs.writeFileSync(path.resolve(__dirname) + '/../' + filename, data)
}
2021-01-30 04:16:44 +01:00
utils.writeToLog = function (country, msg, url) {
2021-01-29 23:58:52 +01:00
var now = new Date()
var line = `${country}: ${msg} '${url}'`
this.appendToFile('error.log', now.toISOString() + ' ' + line + '\n')
2020-05-04 15:23:03 +02:00
}
2021-01-30 04:16:44 +01:00
utils.filterNSFW = function (arr) {
const sfwCategories = utils.supportedCategories.filter(c => !c.nsfw).map(c => c.name)
2021-01-27 16:54:22 +01:00
return arr.filter(i => sfwCategories.includes(i.category))
}
2021-01-30 04:16:44 +01:00
utils.sleep = function (ms) {
2021-01-19 23:02:24 +01:00
return function (x) {
return new Promise(resolve => setTimeout(() => resolve(x), ms))
}
}
2021-01-30 04:16:44 +01:00
module.exports = utils