Update scripts

This commit is contained in:
freearhey 2025-04-16 20:54:55 +03:00
parent d095023da0
commit df365451a9
39 changed files with 1256 additions and 508 deletions

View file

@ -1,26 +1,45 @@
import { URL, Collection, Dictionary } from '@freearhey/core'
import { Feed, Channel, Category, Region, Subdivision, Country, Language } from './index'
import { URL, Collection, Dictionary } from '@freearhey/core'
import type { StreamData } from '../types/stream'
import parser from 'iptv-playlist-parser'
export class Stream {
name: string
name?: string
url: string
id?: string
groupTitle: string
channelId?: string
channel?: Channel
feedId?: string
feed?: Feed
filepath?: string
line: number
line?: number
label?: string
verticalResolution?: number
isInterlaced?: boolean
httpReferrer?: string
httpUserAgent?: string
referrer?: string
userAgent?: string
groupTitle: string = 'Undefined'
removed: boolean = false
constructor(data: parser.PlaylistItem) {
constructor(data?: StreamData) {
if (!data) return
const id = data.channel && data.feed ? [data.channel, data.feed].join('@') : data.channel
const { verticalResolution, isInterlaced } = parseQuality(data.quality)
this.id = id || undefined
this.channelId = data.channel || undefined
this.feedId = data.feed || undefined
this.name = data.name || undefined
this.url = data.url
this.referrer = data.referrer || undefined
this.userAgent = data.user_agent || undefined
this.verticalResolution = verticalResolution || undefined
this.isInterlaced = isInterlaced || undefined
this.label = data.label || undefined
}
fromPlaylistItem(data: parser.PlaylistItem): this {
if (!data.name) throw new Error('"name" property is required')
if (!data.url) throw new Error('"url" property is required')
@ -37,15 +56,16 @@ export class Stream {
this.verticalResolution = verticalResolution || undefined
this.isInterlaced = isInterlaced || undefined
this.url = data.url
this.httpReferrer = data.http.referrer || undefined
this.httpUserAgent = data.http['user-agent'] || undefined
this.groupTitle = 'Undefined'
this.referrer = data.http.referrer || undefined
this.userAgent = data.http['user-agent'] || undefined
return this
}
withChannel(channelsGroupedById: Dictionary): this {
withChannel(channelsKeyById: Dictionary): this {
if (!this.channelId) return this
this.channel = channelsGroupedById.get(this.channelId)
this.channel = channelsKeyById.get(this.channelId)
return this
}
@ -93,18 +113,22 @@ export class Stream {
return this
}
setHttpUserAgent(httpUserAgent: string): this {
this.httpUserAgent = httpUserAgent
setUserAgent(userAgent: string): this {
this.userAgent = userAgent
return this
}
setHttpReferrer(httpReferrer: string): this {
this.httpReferrer = httpReferrer
setReferrer(referrer: string): this {
this.referrer = referrer
return this
}
getLine(): number {
return this.line || -1
}
setFilepath(filepath: string): this {
this.filepath = filepath
@ -133,12 +157,12 @@ export class Stream {
return this.filepath || ''
}
getHttpReferrer(): string {
return this.httpReferrer || ''
getReferrer(): string {
return this.referrer || ''
}
getHttpUserAgent(): string {
return this.httpUserAgent || ''
getUserAgent(): string {
return this.userAgent || ''
}
getQuality(): string {
@ -198,14 +222,6 @@ export class Stream {
return Object.assign(Object.create(Object.getPrototypeOf(this)), this)
}
hasName(): boolean {
return !!this.name
}
noName(): boolean {
return !this.name
}
hasChannel() {
return !!this.channel
}
@ -281,8 +297,12 @@ export class Stream {
return this?.channel?.logo || ''
}
getName(): string {
return this.name || ''
}
getTitle(): string {
let title = `${this.name}`
let title = `${this.getName()}`
if (this.getQuality()) {
title += ` (${this.getQuality()})`
@ -303,30 +323,13 @@ export class Stream {
return this.id || ''
}
data() {
return {
id: this.id,
channel: this.channel,
feed: this.feed,
filepath: this.filepath,
label: this.label,
name: this.name,
verticalResolution: this.verticalResolution,
isInterlaced: this.isInterlaced,
url: this.url,
httpReferrer: this.httpReferrer,
httpUserAgent: this.httpUserAgent,
line: this.line
}
}
toJSON() {
return {
channel: this.channelId || null,
feed: this.feedId || null,
url: this.url,
referrer: this.httpReferrer || null,
user_agent: this.httpUserAgent || null,
referrer: this.referrer || null,
user_agent: this.userAgent || null,
quality: this.getQuality() || null
}
}
@ -338,22 +341,22 @@ export class Stream {
output += ` tvg-logo="${this.getLogo()}" group-title="${this.groupTitle}"`
}
if (this.httpReferrer) {
output += ` http-referrer="${this.httpReferrer}"`
if (this.referrer) {
output += ` http-referrer="${this.referrer}"`
}
if (this.httpUserAgent) {
output += ` http-user-agent="${this.httpUserAgent}"`
if (this.userAgent) {
output += ` http-user-agent="${this.userAgent}"`
}
output += `,${this.getTitle()}`
if (this.httpReferrer) {
output += `\n#EXTVLCOPT:http-referrer=${this.httpReferrer}`
if (this.referrer) {
output += `\n#EXTVLCOPT:http-referrer=${this.referrer}`
}
if (this.httpUserAgent) {
output += `\n#EXTVLCOPT:http-user-agent=${this.httpUserAgent}`
if (this.userAgent) {
output += `\n#EXTVLCOPT:http-user-agent=${this.userAgent}`
}
output += `\n${this.url}`
@ -379,7 +382,11 @@ function escapeRegExp(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
}
function parseQuality(quality: string): { verticalResolution: number; isInterlaced: boolean } {
function parseQuality(quality: string | null): {
verticalResolution: number | null
isInterlaced: boolean | null
} {
if (!quality) return { verticalResolution: null, isInterlaced: null }
let [, verticalResolutionString] = quality.match(/^(\d+)/) || [null, undefined]
const isInterlaced = /i$/i.test(quality)
let verticalResolution = 0