Files
iptv/scripts/models/feed.ts

171 lines
4.2 KiB
TypeScript
Raw Normal View History

2025-08-24 22:43:25 +03:00
import { Country, Language, Region, Channel, Subdivision, BroadcastArea, City } from './index'
2025-05-01 00:51:41 +03:00
import { Collection, Dictionary } from '@freearhey/core'
2025-04-16 20:54:55 +03:00
import type { FeedData } from '../types/feed'
2025-03-29 11:39:46 +03:00
export class Feed {
channelId: string
channel?: Channel
id: string
name: string
isMain: boolean
broadcastAreaCodes: Collection
2025-08-23 17:47:03 +03:00
broadcastArea?: BroadcastArea
2025-03-29 11:39:46 +03:00
languageCodes: Collection
languages?: Collection
timezoneIds: Collection
timezones?: Collection
videoFormat: string
2025-04-16 20:54:55 +03:00
guides?: Collection
streams?: Collection
2025-03-29 11:39:46 +03:00
constructor(data: FeedData) {
this.channelId = data.channel
this.id = data.id
this.name = data.name
this.isMain = data.is_main
this.broadcastAreaCodes = new Collection(data.broadcast_area)
this.languageCodes = new Collection(data.languages)
this.timezoneIds = new Collection(data.timezones)
this.videoFormat = data.video_format
}
2025-04-16 20:54:55 +03:00
withChannel(channelsKeyById: Dictionary): this {
this.channel = channelsKeyById.get(this.channelId)
return this
}
withStreams(streamsGroupedById: Dictionary): this {
this.streams = new Collection(streamsGroupedById.get(`${this.channelId}@${this.id}`))
if (this.isMain) {
this.streams = this.streams.concat(new Collection(streamsGroupedById.get(this.channelId)))
}
return this
}
withGuides(guidesGroupedByStreamId: Dictionary): this {
this.guides = new Collection(guidesGroupedByStreamId.get(`${this.channelId}@${this.id}`))
if (this.isMain) {
this.guides = this.guides.concat(new Collection(guidesGroupedByStreamId.get(this.channelId)))
}
2025-03-29 11:39:46 +03:00
return this
}
2025-04-16 20:54:55 +03:00
withLanguages(languagesKeyByCode: Dictionary): this {
2025-03-29 11:39:46 +03:00
this.languages = this.languageCodes
2025-04-16 20:54:55 +03:00
.map((code: string) => languagesKeyByCode.get(code))
2025-03-29 11:39:46 +03:00
.filter(Boolean)
return this
}
2025-04-16 20:54:55 +03:00
withTimezones(timezonesKeyById: Dictionary): this {
this.timezones = this.timezoneIds.map((id: string) => timezonesKeyById.get(id)).filter(Boolean)
2025-03-29 11:39:46 +03:00
return this
}
2025-08-23 17:47:03 +03:00
withBroadcastArea(
citiesKeyByCode: Dictionary,
subdivisionsKeyByCode: Dictionary,
2025-04-16 20:54:55 +03:00
countriesKeyByCode: Dictionary,
2025-08-23 17:47:03 +03:00
regionsKeyByCode: Dictionary
2025-03-29 11:39:46 +03:00
): this {
2025-08-23 17:47:03 +03:00
this.broadcastArea = new BroadcastArea(this.broadcastAreaCodes).withLocations(
citiesKeyByCode,
subdivisionsKeyByCode,
countriesKeyByCode,
regionsKeyByCode
)
2025-03-29 11:39:46 +03:00
return this
}
hasBroadcastArea(): boolean {
2025-08-23 17:47:03 +03:00
return !!this.broadcastArea
2025-03-29 11:39:46 +03:00
}
getBroadcastCountries(): Collection {
2025-08-23 17:47:03 +03:00
if (!this.broadcastArea) return new Collection()
return this.broadcastArea.getCountries()
2025-03-29 11:39:46 +03:00
}
getBroadcastRegions(): Collection {
2025-08-23 17:47:03 +03:00
if (!this.broadcastArea) return new Collection()
return this.broadcastArea.getRegions()
2025-03-29 11:39:46 +03:00
}
getTimezones(): Collection {
return this.timezones || new Collection()
}
getLanguages(): Collection {
return this.languages || new Collection()
}
hasLanguages(): boolean {
return !!this.languages && this.languages.notEmpty()
}
hasLanguage(language: Language): boolean {
return (
!!this.languages &&
this.languages.includes((_language: Language) => _language.code === language.code)
)
}
2025-08-24 22:43:25 +03:00
isBroadcastInCity(city: City): boolean {
if (!this.broadcastArea) return false
return this.broadcastArea.includesCity(city)
}
2025-03-29 11:39:46 +03:00
isBroadcastInSubdivision(subdivision: Subdivision): boolean {
2025-08-23 17:47:03 +03:00
if (!this.broadcastArea) return false
2025-03-29 11:39:46 +03:00
2025-08-23 17:47:03 +03:00
return this.broadcastArea.includesSubdivision(subdivision)
2025-03-29 11:39:46 +03:00
}
isBroadcastInCountry(country: Country): boolean {
2025-08-23 17:47:03 +03:00
if (!this.broadcastArea) return false
2025-03-29 11:39:46 +03:00
2025-08-24 22:43:25 +03:00
return this.broadcastArea.includesCountry(country)
2025-03-29 11:39:46 +03:00
}
isBroadcastInRegion(region: Region): boolean {
2025-08-23 17:47:03 +03:00
if (!this.broadcastArea) return false
2025-03-29 11:39:46 +03:00
2025-08-24 22:43:25 +03:00
return this.broadcastArea.includesRegion(region)
}
isInternational(): boolean {
if (!this.broadcastArea) return false
return this.broadcastArea.codes.join(',').includes('r/')
2025-03-29 11:39:46 +03:00
}
2025-04-16 20:54:55 +03:00
getGuides(): Collection {
if (!this.guides) return new Collection()
return this.guides
}
getStreams(): Collection {
if (!this.streams) return new Collection()
return this.streams
}
getFullName(): string {
if (!this.channel) return ''
return `${this.channel.name} ${this.name}`
}
2025-03-29 11:39:46 +03:00
}