Make code prettier
This commit is contained in:
parent
71ba959695
commit
d9bef97c88
|
@ -1,8 +1,8 @@
|
||||||
const fs = require("fs")
|
const fs = require('fs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const playlistParser = require('iptv-playlist-parser')
|
const playlistParser = require('iptv-playlist-parser')
|
||||||
const axios = require('axios')
|
const axios = require('axios')
|
||||||
const zlib = require("zlib")
|
const zlib = require('zlib')
|
||||||
const epgParser = require('epg-parser')
|
const epgParser = require('epg-parser')
|
||||||
const urlParser = require('url')
|
const urlParser = require('url')
|
||||||
const ISO6391 = require('iso-639-1')
|
const ISO6391 = require('iso-639-1')
|
||||||
|
@ -12,60 +12,64 @@ const markdownInclude = require('markdown-include')
|
||||||
let cache = {}
|
let cache = {}
|
||||||
let helper = {}
|
let helper = {}
|
||||||
|
|
||||||
helper.sortBy = function(arr, fields) {
|
helper.sortBy = function (arr, fields) {
|
||||||
return arr.sort((a, b) => {
|
return arr.sort((a, b) => {
|
||||||
for(let field of fields) {
|
for (let field of fields) {
|
||||||
if(a[field].toLowerCase() < b[field].toLowerCase()) { return -1 }
|
if (a[field].toLowerCase() < b[field].toLowerCase()) {
|
||||||
if(a[field].toLowerCase() > b[field].toLowerCase()) { return 1 }
|
return -1
|
||||||
|
}
|
||||||
|
if (a[field].toLowerCase() > b[field].toLowerCase()) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.createDir = function(dir) {
|
helper.createDir = function (dir) {
|
||||||
if (!fs.existsSync(dir)) {
|
if (!fs.existsSync(dir)) {
|
||||||
fs.mkdirSync(dir)
|
fs.mkdirSync(dir)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.compileMarkdown = function(filepath) {
|
helper.compileMarkdown = function (filepath) {
|
||||||
return markdownInclude.compileFiles(path.resolve(__dirname, filepath))
|
return markdownInclude.compileFiles(path.resolve(__dirname, filepath))
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.escapeStringRegexp = function(scring) {
|
helper.escapeStringRegexp = function (scring) {
|
||||||
return escapeStringRegexp(string)
|
return escapeStringRegexp(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.getISO6391Name = function(code) {
|
helper.getISO6391Name = function (code) {
|
||||||
return ISO6391.getName(code)
|
return ISO6391.getName(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.getISO6391Code = function(name) {
|
helper.getISO6391Code = function (name) {
|
||||||
return ISO6391.getCode(name)
|
return ISO6391.getCode(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.parsePlaylist = function(filename) {
|
helper.parsePlaylist = function (filename) {
|
||||||
const content = this.readFile(filename)
|
const content = this.readFile(filename)
|
||||||
const result = playlistParser.parse(content)
|
const result = playlistParser.parse(content)
|
||||||
|
|
||||||
return new Playlist(result)
|
return new Playlist(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.parseEPG = async function(url) {
|
helper.parseEPG = async function (url) {
|
||||||
const content = await this.getEPG(url)
|
const content = await this.getEPG(url)
|
||||||
const result = epgParser.parse(content)
|
const result = epgParser.parse(content)
|
||||||
const channels = {}
|
const channels = {}
|
||||||
for(let channel of result.channels) {
|
for (let channel of result.channels) {
|
||||||
channels[channel.id] = channel
|
channels[channel.id] = channel
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
url,
|
url,
|
||||||
channels
|
channels
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.getEPG = function(url) {
|
helper.getEPG = function (url) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
var buffer = []
|
var buffer = []
|
||||||
axios({
|
axios({
|
||||||
|
@ -73,46 +77,51 @@ helper.getEPG = function(url) {
|
||||||
url: url,
|
url: url,
|
||||||
responseType: 'stream',
|
responseType: 'stream',
|
||||||
timeout: 60000
|
timeout: 60000
|
||||||
}).then(res => {
|
})
|
||||||
let stream
|
.then(res => {
|
||||||
if(/\.gz$/i.test(url)) {
|
let stream
|
||||||
let gunzip = zlib.createGunzip()
|
if (/\.gz$/i.test(url)) {
|
||||||
res.data.pipe(gunzip)
|
let gunzip = zlib.createGunzip()
|
||||||
stream = gunzip
|
res.data.pipe(gunzip)
|
||||||
} else {
|
stream = gunzip
|
||||||
stream = res.data
|
} else {
|
||||||
}
|
stream = res.data
|
||||||
|
}
|
||||||
|
|
||||||
stream.on('data', function(data) {
|
stream
|
||||||
buffer.push(data.toString())
|
.on('data', function (data) {
|
||||||
}).on("end", function() {
|
buffer.push(data.toString())
|
||||||
resolve(buffer.join(""))
|
})
|
||||||
}).on("error", function(e) {
|
.on('end', function () {
|
||||||
|
resolve(buffer.join(''))
|
||||||
|
})
|
||||||
|
.on('error', function (e) {
|
||||||
|
reject(e)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.catch(e => {
|
||||||
reject(e)
|
reject(e)
|
||||||
})
|
})
|
||||||
}).catch(e => {
|
|
||||||
reject(e)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.readFile = function(filename) {
|
helper.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) {
|
helper.appendToFile = function (filename, data) {
|
||||||
fs.appendFileSync(path.resolve(__dirname) + '/../' + 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)
|
fs.writeFileSync(path.resolve(__dirname) + '/../' + filename, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.getBasename = function(filename) {
|
helper.getBasename = function (filename) {
|
||||||
return path.basename(filename, path.extname(filename))
|
return path.basename(filename, path.extname(filename))
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.getUrlPath = function(u) {
|
helper.getUrlPath = function (u) {
|
||||||
let parsed = urlParser.parse(u)
|
let parsed = urlParser.parse(u)
|
||||||
let searchQuery = parsed.search || ''
|
let searchQuery = parsed.search || ''
|
||||||
let path = parsed.host + parsed.pathname + searchQuery
|
let path = parsed.host + parsed.pathname + searchQuery
|
||||||
|
@ -120,23 +129,23 @@ helper.getUrlPath = function(u) {
|
||||||
return path.toLowerCase()
|
return path.toLowerCase()
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.filterPlaylists = function(arr, include = '', exclude = '') {
|
helper.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`)
|
||||||
|
|
||||||
return arr.filter(i => included.indexOf(i.url) > -1)
|
return arr.filter(i => included.indexOf(i.url) > -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if(exclude) {
|
if (exclude) {
|
||||||
const excluded = exclude.split(',').map(filename => `channels/${filename}.m3u`)
|
const excluded = exclude.split(',').map(filename => `channels/${filename}.m3u`)
|
||||||
|
|
||||||
return arr.filter(i => excluded.indexOf(i.url) === -1)
|
return arr.filter(i => excluded.indexOf(i.url) === -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
return arr
|
return arr
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.generateTable = function(data, options) {
|
helper.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>'
|
||||||
|
@ -165,7 +174,7 @@ helper.generateTable = function(data, options) {
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.createChannel = function(data) {
|
helper.createChannel = function (data) {
|
||||||
return new Channel({
|
return new Channel({
|
||||||
id: data.tvg.id,
|
id: data.tvg.id,
|
||||||
name: data.tvg.name,
|
name: data.tvg.name,
|
||||||
|
@ -177,24 +186,24 @@ helper.createChannel = function(data) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.writeToLog = function(country, msg, url) {
|
helper.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.parseMessage = function(err, u) {
|
helper.parseMessage = function (err, u) {
|
||||||
if(!err || !err.message) return
|
if (!err || !err.message) return
|
||||||
|
|
||||||
const msgArr = err.message.split('\n')
|
const msgArr = err.message.split('\n')
|
||||||
|
|
||||||
if(msgArr.length === 0) return
|
if (msgArr.length === 0) return
|
||||||
|
|
||||||
const line = msgArr.find(line => {
|
const line = msgArr.find(line => {
|
||||||
return line.indexOf(u) === 0
|
return line.indexOf(u) === 0
|
||||||
})
|
})
|
||||||
|
|
||||||
if(!line) return
|
if (!line) return
|
||||||
|
|
||||||
return line.replace(`${u}: `, '')
|
return line.replace(`${u}: `, '')
|
||||||
}
|
}
|
||||||
|
@ -208,9 +217,9 @@ class Playlist {
|
||||||
|
|
||||||
getHeader() {
|
getHeader() {
|
||||||
let parts = ['#EXTM3U']
|
let parts = ['#EXTM3U']
|
||||||
for(let key in this.header.attrs) {
|
for (let key in this.header.attrs) {
|
||||||
let value = this.header.attrs[key]
|
let value = this.header.attrs[key]
|
||||||
if(value) {
|
if (value) {
|
||||||
parts.push(`${key}="${value}"`)
|
parts.push(`${key}="${value}"`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -231,12 +240,44 @@ class Channel {
|
||||||
}
|
}
|
||||||
|
|
||||||
_filterGroup(groupTitle) {
|
_filterGroup(groupTitle) {
|
||||||
if(!groupTitle) return ''
|
if (!groupTitle) return ''
|
||||||
|
|
||||||
const supportedCategories = [ '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', 'XXX' ]
|
|
||||||
const groupIndex = supportedCategories.map(g => g.toLowerCase()).indexOf(groupTitle.toLowerCase())
|
|
||||||
|
|
||||||
if(groupIndex === -1) {
|
const supportedCategories = [
|
||||||
|
'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',
|
||||||
|
'XXX'
|
||||||
|
]
|
||||||
|
const groupIndex = supportedCategories
|
||||||
|
.map(g => g.toLowerCase())
|
||||||
|
.indexOf(groupTitle.toLowerCase())
|
||||||
|
|
||||||
|
if (groupIndex === -1) {
|
||||||
groupTitle = ''
|
groupTitle = ''
|
||||||
} else {
|
} else {
|
||||||
groupTitle = supportedCategories[groupIndex]
|
groupTitle = supportedCategories[groupIndex]
|
||||||
|
@ -246,7 +287,7 @@ class Channel {
|
||||||
}
|
}
|
||||||
|
|
||||||
_filterLanguage(languageName) {
|
_filterLanguage(languageName) {
|
||||||
if(ISO6391.getCode(languageName) !== '') {
|
if (ISO6391.getCode(languageName) !== '') {
|
||||||
return languageName
|
return languageName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -255,7 +296,7 @@ class Channel {
|
||||||
|
|
||||||
toString() {
|
toString() {
|
||||||
const country = this.countryCode.toUpperCase()
|
const country = this.countryCode.toUpperCase()
|
||||||
const epg = (this.id && this.epg) ? this.epg : ''
|
const epg = this.id && this.epg ? this.epg : ''
|
||||||
const info = `-1 tvg-id="${this.id}" tvg-name="${this.name}" tvg-language="${this.language}" tvg-logo="${this.logo}" tvg-country="${country}" tvg-url="${epg}" group-title="${this.group}",${this.title}`
|
const info = `-1 tvg-id="${this.id}" tvg-name="${this.name}" tvg-language="${this.language}" tvg-logo="${this.logo}" tvg-country="${country}" tvg-url="${epg}" group-title="${this.group}",${this.title}`
|
||||||
|
|
||||||
return '#EXTINF:' + info + '\n' + this.url + '\n'
|
return '#EXTINF:' + info + '\n' + this.url + '\n'
|
||||||
|
@ -268,4 +309,4 @@ class Channel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = helper
|
module.exports = helper
|
||||||
|
|
Loading…
Reference in New Issue