Update helper.js

This commit is contained in:
freearhey 2021-01-29 21:31:00 +03:00
parent 7fae6d1ad6
commit fcbe98fbd2

View file

@ -8,6 +8,10 @@ const urlParser = require('url')
const escapeStringRegexp = require('escape-string-regexp') 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')
const intlDisplayNames = new Intl.DisplayNames(['en'], {
style: 'long',
type: 'region'
})
let helper = {} let helper = {}
@ -105,14 +109,15 @@ helper.getISO6391Code = function (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)
const playlist = new Playlist(result)
playlist.url = filename
return new Playlist(result) return playlist
} }
helper.parseEPG = async function (url) { helper.parseEPG = async function (url) {
return this.getEPG(url).then(content => { return this.getEPG(url).then(content => {
const result = epgParser.parse(content) const result = epgParser.parse(content)
console.log('wo')
const channels = {} const channels = {}
for (let channel of result.channels) { for (let channel of result.channels) {
channels[channel.id] = channel channels[channel.id] = channel
@ -211,8 +216,8 @@ helper.generateTable = function (data, options) {
return output return output
} }
helper.createChannel = function (data) { helper.createChannel = function (data, parent) {
return new Channel(data) return new Channel(data, parent)
} }
helper.writeToLog = function (country, msg, url) { helper.writeToLog = function (country, msg, url) {
@ -282,6 +287,17 @@ helper.sleep = function (ms) {
} }
} }
helper.code2country = function (code) {
code = code ? code.toLowerCase() : ''
if (code === 'int') {
return { code: 'int', name: 'International' }
} else if (code === 'unsorted') {
return null
}
return { code, name: intlDisplayNames.of(code.toUpperCase()) }
}
class Playlist { class Playlist {
constructor(data) { constructor(data) {
this.header = data.header this.header = data.header
@ -302,11 +318,12 @@ class Playlist {
} }
class Channel { class Channel {
constructor(data) { constructor(data, parent) {
this.parseData(data) this.parseData(data, parent)
} }
parseData(data) { parseData(data, parent) {
this.source = helper.getBasename(parent.url)
this.logo = data.tvg.logo this.logo = data.tvg.logo
this.category = helper.filterGroup(data.group.title) this.category = helper.filterGroup(data.group.title)
this.url = data.url this.url = data.url
@ -314,21 +331,34 @@ class Channel {
this.status = this.parseStatus(data.name) this.status = this.parseStatus(data.name)
this.http = data.http this.http = data.http
this.tvg = data.tvg this.tvg = data.tvg
this.country = { this.tvg.url = parent.header.attrs['x-tvg-url'] || ''
code: null, this.countries = this.parseCountries(data.tvg.country)
name: null
}
this.resolution = this.parseResolution(data.name) this.resolution = this.parseResolution(data.name)
this.language = this.parseLanguage(data.tvg.language)
this.setLanguage(data.tvg.language)
} }
get ['language.name']() { get languageName() {
return this.language[0] ? this.language[0].name : null return this.language[0] ? this.language[0].name : null
} }
get ['country.name']() { get countryName() {
return this.country.name || null return this.countries[0] ? this.countries[0].name : null
}
get countryCode() {
return this.countries[0] ? this.countries[0].code : null
}
parseCountries(value) {
if (!value) {
const country = helper.code2country(this.source)
return country ? [country] : []
}
return value
.split(';')
.filter(i => i)
.map(helper.code2country)
} }
parseName(title) { parseName(title) {
@ -359,8 +389,8 @@ class Channel {
} }
} }
setLanguage(lang) { parseLanguage(lang) {
this.language = lang return lang
.split(';') .split(';')
.map(name => { .map(name => {
const code = name ? helper.getISO6391Code(name) : null const code = name ? helper.getISO6391Code(name) : null
@ -374,10 +404,18 @@ class Channel {
.filter(l => l) .filter(l => l)
} }
getCountryAttribute() {
return this.countries.map(c => c.code.toUpperCase()).join(';')
}
getLanguageAttribute() {
return this.language.map(l => l.name).join(';')
}
toString() { toString() {
const country = this.country.code ? this.country.code.toUpperCase() : '' const country = this.getCountryAttribute()
const language = this.getLanguageAttribute()
const tvgUrl = (this.tvg.id || this.tvg.name) && this.tvg.url ? this.tvg.url : '' const tvgUrl = (this.tvg.id || this.tvg.name) && this.tvg.url ? this.tvg.url : ''
const language = this.language.map(l => l.name).join(';')
const resolution = this.resolution.height ? ` (${this.resolution.height}p)` : '' const resolution = this.resolution.height ? ` (${this.resolution.height}p)` : ''
const status = this.status ? ` [${this.status}]` : '' const status = this.status ? ` [${this.status}]` : ''
@ -395,11 +433,12 @@ class Channel {
} }
toShortString() { toShortString() {
const language = this.language.map(l => l.name).join(';') const country = this.getCountryAttribute()
const language = this.getLanguageAttribute()
const resolution = this.resolution.height ? ` (${this.resolution.height}p)` : '' const resolution = this.resolution.height ? ` (${this.resolution.height}p)` : ''
const status = this.status ? ` [${this.status}]` : '' const status = this.status ? ` [${this.status}]` : ''
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.name}${resolution}${status}` let info = `-1 tvg-id="${this.tvg.id}" tvg-name="${this.tvg.name}" tvg-language="${language}" tvg-logo="${this.logo}" tvg-country="${country}" group-title="${this.category}",${this.name}${resolution}${status}`
if (this.http['referrer']) { if (this.http['referrer']) {
info += `\n#EXTVLCOPT:http-referrer=${this.http['referrer']}` info += `\n#EXTVLCOPT:http-referrer=${this.http['referrer']}`
@ -419,7 +458,7 @@ class Channel {
url: this.url, url: this.url,
category: this.category || null, category: this.category || null,
language: this.language, language: this.language,
country: this.country, countries: this.countries,
tvg: { tvg: {
id: this.tvg.id || null, id: this.tvg.id || null,
name: this.tvg.name || null, name: this.tvg.name || null,