iptv/scripts/helpers/Channel.js

157 lines
3.7 KiB
JavaScript
Raw Normal View History

2021-08-30 19:27:12 +02:00
const categories = require('../data/categories')
2021-08-01 18:45:23 +02:00
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) {
2021-08-08 12:19:11 +02:00
this.data = data
2021-08-02 20:15:40 +02:00
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-12 17:42:25 +02:00
this.hash = this.generateHash()
}
generateHash() {
return `${this.tvg.id}:${this.tvg.name}:${this.tvg.country}:${this.tvg.language}:${this.logo}:${this.group.title}:${this.name}`.toLowerCase()
2021-08-02 20:15:40 +02:00
}
updateUrl(url) {
this.url = url
this.data.url = url
}
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-09-08 01:47:56 +02:00
let info = `-1 tvg-id="${this.tvg.id}" 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
}
}
}
}