Update Channel.js

This commit is contained in:
Aleksandr Statciuk 2021-08-02 21:15:40 +03:00
parent b48457fe62
commit d13bbd92eb
1 changed files with 48 additions and 104 deletions

View File

@ -6,36 +6,46 @@ const sfwCategories = categories.filter(c => !c.nsfw).map(c => c.name)
const nsfwCategories = categories.filter(c => c.nsfw).map(c => c.name) const nsfwCategories = categories.filter(c => c.nsfw).map(c => c.name)
module.exports = class Channel { module.exports = class Channel {
constructor({ data, header, sourceUrl }) { constructor(data) {
this.parseData(data) this.raw = data.raw
this.filename = file.getBasename(sourceUrl)
if (!this.countries.length) {
const countryName = utils.code2name(this.filename)
this.countries = countryName ? [{ code: this.filename, name: countryName }] : []
this.tvg.country = this.countries.map(c => c.code.toUpperCase()).join(';')
}
}
parseData(data) {
const title = this.parseTitle(data.name)
this.tvg = data.tvg this.tvg = data.tvg
this.http = data.http this.http = data.http
this.url = data.url this.url = data.url
this.logo = data.tvg.logo this.logo = data.tvg.logo
this.name = title.channelName this.category = data.group.title
this.status = title.streamStatus this.name = this.parseName(data.name)
this.resolution = title.streamResolution this.status = this.parseStatus(data.name)
this.resolution = this.parseResolution(data.name)
this.countries = this.parseCountries(data.tvg.country) this.countries = this.parseCountries(data.tvg.country)
this.languages = this.parseLanguages(data.tvg.language) this.languages = this.parseLanguages(data.tvg.language)
this.category = this.parseCategory(data.group.title) }
this.raw = data.raw
parseName(title) {
return title
.trim()
.split(' ')
.map(s => s.trim())
.filter(s => {
return !/\[|\]/i.test(s) && !/\((\d+)P\)/i.test(s)
})
.join(' ')
}
parseStatus(title) {
const match = title.match(/\[(.*)\]/i)
return match ? match[1] : null
}
parseResolution(title) {
const match = title.match(/\((\d+)P\)/i)
const height = match ? parseInt(match[1]) : null
return { width: null, height }
} }
parseCountries(string) { parseCountries(string) {
let arr = string const list = string.split(';')
.split(';') return list
.reduce((acc, curr) => { .reduce((acc, curr) => {
const codes = utils.region2codes(curr) const codes = utils.region2codes(curr)
if (codes.length) { if (codes.length) {
@ -50,95 +60,37 @@ module.exports = class Channel {
return acc return acc
}, []) }, [])
.filter(code => code && utils.code2name(code)) .map(code => {
const name = code ? utils.code2name(code) : null
if (!name) return null
return arr.map(code => { return { code: code.toLowerCase(), name }
return { code: code.toLowerCase(), name: utils.code2name(code) } })
}) .filter(c => c)
} }
parseLanguages(string) { parseLanguages(string) {
return string const list = string.split(';')
.split(';') return list
.map(name => { .map(name => {
const code = name ? utils.language2code(name) : null const code = name ? utils.language2code(name) : null
if (!code) return null if (!code) return null
return { return { code, name }
code,
name
}
}) })
.filter(l => l) .filter(l => l)
} }
parseCategory(string) { isSFW() {
const category = categories.find(c => c.id === string.toLowerCase()) return sfwCategories.includes(this.category)
return category ? category.name : ''
} }
parseTitle(title) { isNSFW() {
const channelName = title return nsfwCategories.includes(this.category)
.trim()
.split(' ')
.map(s => s.trim())
.filter(s => {
return !/\[|\]/i.test(s) && !/\((\d+)P\)/i.test(s)
})
.join(' ')
const streamStatusMatch = title.match(/\[(.*)\]/i)
const streamStatus = streamStatusMatch ? streamStatusMatch[1] : null
const streamResolutionMatch = title.match(/\((\d+)P\)/i)
const streamResolutionHeight = streamResolutionMatch ? parseInt(streamResolutionMatch[1]) : null
const streamResolution = { width: null, height: streamResolutionHeight }
return { channelName, streamStatus, streamResolution }
}
get tvgCountry() {
return this.tvg.country
.split(';')
.map(code => utils.code2name(code))
.join(';')
}
get tvgLanguage() {
return this.tvg.language
}
get tvgUrl() {
return this.tvg.id && this.tvg.url ? this.tvg.url : ''
}
get tvgId() {
if (this.tvg.id) {
return this.tvg.id
} else if (this.filename !== 'unsorted') {
const id = utils.name2id(this.tvgName)
return id ? `${id}.${this.filename}` : ''
}
return ''
}
get tvgName() {
if (this.tvg.name) {
return this.tvg.name
} else if (this.filename !== 'unsorted') {
return this.name.replace(/\"/gi, '')
}
return ''
} }
getInfo() { getInfo() {
this.tvg.country = this.tvg.country.toUpperCase() let info = `-1 tvg-id="${this.tvg.id}" tvg-name="${this.tvg.name}" tvg-country="${this.tvg.country}" tvg-language="${this.tvg.language}" tvg-logo="${this.logo}"`
let info = `-1 tvg-id="${this.tvgId}" tvg-name="${this.tvgName}" tvg-country="${this.tvg.country}" tvg-language="${this.tvg.language}" tvg-logo="${this.logo}"`
info += ` group-title="${this.category}",${this.name}` info += ` group-title="${this.category}",${this.name}`
@ -176,18 +128,10 @@ module.exports = class Channel {
languages: this.languages, languages: this.languages,
countries: this.countries, countries: this.countries,
tvg: { tvg: {
id: this.tvgId || null, id: this.tvg.id || null,
name: this.tvgName || null, name: this.tvg.name || null,
url: this.tvgUrl || null url: this.tvg.url || null
} }
} }
} }
isSFW() {
return sfwCategories.includes(this.category)
}
isNSFW() {
return nsfwCategories.includes(this.category)
}
} }