iptv/scripts/models/stream.ts

188 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-09-22 04:17:22 +02:00
import { URL, Collection } from '@freearhey/core'
2023-09-15 17:40:35 +02:00
import { Category, Language } from './index'
type StreamProps = {
name: string
url: string
filepath: string
line: number
channel?: string
httpReferrer?: string
label?: string
quality?: string
userAgent?: string
2023-11-01 03:38:07 +01:00
timeshift?: string
2023-09-15 17:40:35 +02:00
}
export class Stream {
channel: string
filepath: string
line: number
httpReferrer: string
label: string
name: string
quality: string
url: string
userAgent: string
logo: string
broadcastArea: Collection
categories: Collection
languages: Collection
isNSFW: boolean
groupTitle: string
2023-09-18 00:51:17 +02:00
removed: boolean = false
2023-11-01 03:38:07 +01:00
timeshift: string
2023-09-15 17:40:35 +02:00
constructor({
channel,
filepath,
line,
httpReferrer,
label,
name,
quality,
url,
2023-11-01 03:38:07 +01:00
userAgent,
timeshift
2023-09-15 17:40:35 +02:00
}: StreamProps) {
this.channel = channel || ''
this.filepath = filepath
this.line = line
this.httpReferrer = httpReferrer || ''
this.label = label || ''
this.name = name
this.quality = quality || ''
this.url = url
this.userAgent = userAgent || ''
this.logo = ''
this.broadcastArea = new Collection()
this.categories = new Collection()
this.languages = new Collection()
this.isNSFW = false
this.groupTitle = 'Undefined'
2023-11-01 03:38:07 +01:00
this.timeshift = timeshift || ''
2023-09-15 17:40:35 +02:00
}
normalizeURL() {
const url = new URL(this.url)
this.url = url.normalize().toString()
}
clone(): Stream {
return Object.assign(Object.create(Object.getPrototypeOf(this)), this)
}
hasName(): boolean {
return !!this.name
}
noName(): boolean {
return !this.name
}
hasChannel() {
return !!this.channel
}
hasCategories(): boolean {
return this.categories.notEmpty()
}
noCategories(): boolean {
2023-09-22 04:17:22 +02:00
return this.categories.isEmpty()
2023-09-15 17:40:35 +02:00
}
hasCategory(category: Category): boolean {
return this.categories.includes((_category: Category) => _category.id === category.id)
}
noLanguages(): boolean {
2023-09-22 04:17:22 +02:00
return this.languages.isEmpty()
2023-09-15 17:40:35 +02:00
}
hasLanguage(language: Language): boolean {
return this.languages.includes((_language: Language) => _language.code === language.code)
}
noBroadcastArea(): boolean {
2023-09-22 04:17:22 +02:00
return this.broadcastArea.isEmpty()
2023-09-15 17:40:35 +02:00
}
isInternational(): boolean {
return this.broadcastArea.includes('r/INT')
}
isSFW(): boolean {
return this.isNSFW === false
}
getTitle(): string {
let title = `${this.name}`
if (this.quality) {
title += ` (${this.quality})`
}
if (this.label) {
title += ` [${this.label}]`
}
return title
}
data() {
return {
channel: this.channel,
filepath: this.filepath,
httpReferrer: this.httpReferrer,
label: this.label,
name: this.name,
quality: this.quality,
url: this.url,
userAgent: this.userAgent,
line: this.line
}
}
toJSON() {
return {
channel: this.channel,
url: this.url,
2023-11-01 03:38:07 +01:00
timeshift: this.timeshift || null,
2023-09-15 17:40:35 +02:00
http_referrer: this.httpReferrer || null,
user_agent: this.userAgent || null
}
}
toString(options: { public: boolean }) {
let output = `#EXTINF:-1 tvg-id="${this.channel}"`
2023-11-01 03:38:07 +01:00
if (this.timeshift) {
output += ` tvg-shift="${this.timeshift}"`
}
2023-09-15 17:40:35 +02:00
if (options.public) {
output += ` tvg-logo="${this.logo}" group-title="${this.groupTitle}"`
}
if (this.userAgent) {
output += ` user-agent="${this.userAgent}"`
}
output += `,${this.getTitle()}`
if (this.httpReferrer) {
output += `\n#EXTVLCOPT:http-referrer=${this.httpReferrer}`
}
if (this.userAgent) {
output += `\n#EXTVLCOPT:http-user-agent=${this.userAgent}`
}
output += `\n${this.url}`
return output
}
}