iptv/scripts/helpers/Channel.js

146 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-08-01 18:45:23 +02:00
const categories = require('./categories')
const utils = require('./utils')
const file = require('./file')
const sfwCategories = categories.filter(c => !c.nsfw).map(c => c.name)
const nsfwCategories = categories.filter(c => c.nsfw).map(c => c.name)
module.exports = class Channel {
2021-08-02 20:15:40 +02:00
constructor(data) {
this.raw = data.raw
2021-08-01 18:45:23 +02:00
this.tvg = data.tvg
this.http = data.http
this.url = data.url
this.logo = data.tvg.logo
2021-08-02 20:48:07 +02:00
this.group = data.group
2021-08-02 20:15:40 +02:00
this.name = this.parseName(data.name)
this.status = this.parseStatus(data.name)
this.resolution = this.parseResolution(data.name)
2021-08-02 20:48:07 +02:00
this.category = this.parseCategory(data.group.title)
2021-08-01 18:45:23 +02:00
this.countries = this.parseCountries(data.tvg.country)
this.languages = this.parseLanguages(data.tvg.language)
2021-08-02 20:15:40 +02:00
}
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 }
2021-08-01 18:45:23 +02:00
}
2021-08-02 20:48:07 +02:00
parseCategory(string) {
const category = categories.find(c => c.id === string.toLowerCase())
if (!category) return ''
return category.name
}
2021-08-01 18:45:23 +02:00
parseCountries(string) {
2021-08-02 20:15:40 +02:00
const list = string.split(';')
return list
2021-08-01 18:45:23 +02:00
.reduce((acc, curr) => {
const codes = utils.region2codes(curr)
if (codes.length) {
for (let code of codes) {
if (!acc.includes(code)) {
acc.push(code)
}
}
} else {
acc.push(curr)
}
return acc
}, [])
2021-08-02 20:15:40 +02:00
.map(code => {
const name = code ? utils.code2name(code) : null
if (!name) return null
2021-08-01 18:45:23 +02:00
2021-08-02 20:15:40 +02:00
return { code: code.toLowerCase(), name }
})
.filter(c => c)
2021-08-01 18:45:23 +02:00
}
parseLanguages(string) {
2021-08-02 20:15:40 +02:00
const list = string.split(';')
return list
2021-08-01 18:45:23 +02:00
.map(name => {
const code = name ? utils.language2code(name) : null
if (!code) return null
2021-08-02 20:15:40 +02:00
return { code, name }
2021-08-01 18:45:23 +02:00
})
.filter(l => l)
}
2021-08-02 20:15:40 +02:00
isSFW() {
return sfwCategories.includes(this.category)
2021-08-01 18:45:23 +02:00
}
2021-08-02 20:15:40 +02:00
isNSFW() {
return nsfwCategories.includes(this.category)
2021-08-01 18:45:23 +02:00
}
getInfo() {
2021-08-02 20:15:40 +02:00
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}"`
2021-08-01 18:45:23 +02:00
2021-08-02 20:48:07 +02:00
info += ` group-title="${this.group.title}",${this.name}`
2021-08-01 18:45:23 +02:00
if (this.resolution.height) {
info += ` (${this.resolution.height}p)`
}
if (this.status) {
info += ` [${this.status}]`
}
if (this.http['referrer']) {
info += `\n#EXTVLCOPT:http-referrer=${this.http['referrer']}`
}
if (this.http['user-agent']) {
info += `\n#EXTVLCOPT:http-user-agent=${this.http['user-agent']}`
}
return info
}
toString(raw = false) {
if (raw) return this.raw + '\n'
return '#EXTINF:' + this.getInfo() + '\n' + this.url + '\n'
}
toObject() {
return {
name: this.name,
logo: this.logo || null,
url: this.url,
category: this.category || null,
languages: this.languages,
countries: this.countries,
tvg: {
2021-08-02 20:15:40 +02:00
id: this.tvg.id || null,
name: this.tvg.name || null,
url: this.tvg.url || null
2021-08-01 18:45:23 +02:00
}
}
}
}