Code refactoring

pull/1331/head
freearhey 5 years ago
parent 3e073889ea
commit 0a8f8ba400

@ -15,10 +15,13 @@ 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()) { let propA = a[field] ? a[field].toLowerCase() : ''
let propB = b[field] ? b[field].toLowerCase() : ''
if (propA < propB) {
return -1 return -1
} }
if (a[field].toLowerCase() > b[field].toLowerCase()) { if (propA > propB) {
return 1 return 1
} }
} }
@ -41,13 +44,13 @@ helper.escapeStringRegexp = function (scring) {
} }
helper.getISO6391Name = function (code) { helper.getISO6391Name = function (code) {
const lang = iso6393.find((l) => l.iso6393 === code.toLowerCase()) const lang = iso6393.find(l => l.iso6393 === code.toLowerCase())
return lang && lang.name ? lang.name : null return lang && lang.name ? lang.name : null
} }
helper.getISO6391Code = function (name) { helper.getISO6391Code = 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
} }
@ -69,7 +72,7 @@ helper.parseEPG = async function (url) {
return Promise.resolve({ return Promise.resolve({
url, url,
channels, channels
}) })
} }
@ -80,9 +83,9 @@ helper.getEPG = function (url) {
method: 'get', method: 'get',
url: url, url: url,
responseType: 'stream', responseType: 'stream',
timeout: 60000, timeout: 60000
}) })
.then((res) => { .then(res => {
let stream let stream
if (/\.gz$/i.test(url)) { if (/\.gz$/i.test(url)) {
let gunzip = zlib.createGunzip() let gunzip = zlib.createGunzip()
@ -103,7 +106,7 @@ helper.getEPG = function (url) {
reject(e) reject(e)
}) })
}) })
.catch((e) => { .catch(e => {
reject(e) reject(e)
}) })
}) })
@ -179,7 +182,7 @@ helper.parseMessage = function (err, u) {
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
}) })
@ -190,20 +193,65 @@ helper.parseMessage = function (err, u) {
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.filterGroup = function (groupTitle) {
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) {
groupTitle = ''
} else {
groupTitle = supportedCategories[groupIndex]
}
return groupTitle
}
class Playlist { class Playlist {
constructor(data) { constructor(data) {
this.header = data.header this.header = data.header
@ -226,97 +274,93 @@ class Playlist {
class Channel { class Channel {
constructor(data) { constructor(data) {
this.id = data.tvg.id this.parseData(data)
this.name = data.tvg.name }
this.language = data.tvg.language
parseData(data) {
const language = data.tvg.language
.split(';') .split(';')
.filter((l) => !!helper.getISO6391Code(l)) .map(name => {
.join(';') const code = name ? helper.getISO6391Code(name) : null
if (!code) return null
return {
code,
name
}
})
.filter(l => l)
this.language = language
this.logo = data.tvg.logo this.logo = data.tvg.logo
this.group = this._filterGroup(data.group.title) this.category = helper.filterGroup(data.group.title)
this.url = data.url this.url = data.url
this.title = data.name.trim() this.name = data.name.trim()
this.userAgent = data.http['user-agent'] this.http = data.http
this.referrer = data.http['referrer'] this.tvg = data.tvg
this.country = {
code: null,
name: null
}
} }
_filterGroup(groupTitle) { get ['language.name']() {
if (!groupTitle) return '' return this.language[0] ? this.language[0].name : null
}
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 = ''
} else {
groupTitle = supportedCategories[groupIndex]
}
return groupTitle get ['country.name']() {
return this.country.name || null
} }
toString() { toString() {
const country = this.countryCode.toUpperCase() const country = this.country.code ? this.country.code.toUpperCase() : ''
const epg = this.id && this.epg ? this.epg : '' const tvgUrl = (this.tvg.id || this.tvg.name) && this.tvg.url ? this.tvg.url : ''
const language = this.language.map(l => l.name).join(';')
let 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}` let info = `-1 tvg-id="${this.tvg.id}" tvg-name="${this.tvg.name}" tvg-language="${language}" tvg-logo="${this.logo}" tvg-country="${country}" tvg-url="${tvgUrl}" group-title="${this.category}",${this.name}`
if (this.referrer) { if (this.http['referrer']) {
info += `\n#EXTVLCOPT:http-referrer=${this.referrer}` info += `\n#EXTVLCOPT:http-referrer=${this.http['referrer']}`
} }
if (this.userAgent) { if (this.http['user-agent']) {
info += `\n#EXTVLCOPT:http-user-agent=${this.userAgent}` info += `\n#EXTVLCOPT:http-user-agent=${this.http['user-agent']}`
} }
return '#EXTINF:' + info + '\n' + this.url + '\n' return '#EXTINF:' + info + '\n' + this.url + '\n'
} }
toShortString() { toShortString() {
let info = `-1 tvg-id="${this.id}" tvg-name="${this.name}" tvg-language="${this.language}" tvg-logo="${this.logo}" group-title="${this.group}",${this.title}` const language = this.language.map(l => l.name).join(';')
let info = `-1 tvg-id="${this.tvg.id}" tvg-name="${this.tvg.name}" tvg-language="${language}" tvg-logo="${this.logo}" group-title="${this.category}",${this.namee}`
if (this.referrer) { if (this.http['referrer']) {
info += `\n#EXTVLCOPT:http-referrer=${this.referrer}` info += `\n#EXTVLCOPT:http-referrer=${this.http['referrer']}`
} }
if (this.userAgent) { if (this.http['user-agent']) {
info += `\n#EXTVLCOPT:http-user-agent=${this.userAgent}` info += `\n#EXTVLCOPT:http-user-agent=${this.http['user-agent']}`
} }
return '#EXTINF:' + info + '\n' + this.url + '\n' return '#EXTINF:' + info + '\n' + this.url + '\n'
} }
toJSON() {
return {
name: this.name,
logo: this.logo || null,
url: this.url,
category: this.category || null,
language: this.language,
country: this.country,
tvg: {
id: this.tvg.id || null,
name: this.tvg.name || null,
url: this.tvg.url || null
}
}
}
} }
module.exports = helper module.exports = helper

Loading…
Cancel
Save