Files
iptv/scripts/core/dataProcessor.ts

165 lines
5.8 KiB
TypeScript
Raw Normal View History

2025-08-23 17:47:03 +03:00
import { DataProcessorData } from '../types/dataProcessor'
2025-04-16 20:54:55 +03:00
import { DataLoaderData } from '../types/dataLoader'
import { Collection } from '@freearhey/core'
import {
BlocklistRecord,
Subdivision,
Category,
Language,
Timezone,
Channel,
Country,
Region,
Stream,
Guide,
2025-08-23 17:47:03 +03:00
City,
2025-07-10 21:13:43 +03:00
Feed,
Logo
2025-04-16 20:54:55 +03:00
} from '../models'
export class DataProcessor {
2025-08-23 17:47:03 +03:00
process(data: DataLoaderData): DataProcessorData {
let regions = new Collection(data.regions).map(data => new Region(data))
let regionsKeyByCode = regions.keyBy((region: Region) => region.code)
2025-04-16 20:54:55 +03:00
const categories = new Collection(data.categories).map(data => new Category(data))
const categoriesKeyById = categories.keyBy((category: Category) => category.id)
2025-07-10 21:13:43 +03:00
const languages = new Collection(data.languages).map(data => new Language(data))
const languagesKeyByCode = languages.keyBy((language: Language) => language.code)
2025-07-20 20:28:58 +03:00
let subdivisions = new Collection(data.subdivisions).map(data => new Subdivision(data))
2025-08-23 17:47:03 +03:00
let subdivisionsKeyByCode = subdivisions.keyBy((subdivision: Subdivision) => subdivision.code)
let subdivisionsGroupedByCountryCode = subdivisions.groupBy(
2025-04-16 20:54:55 +03:00
(subdivision: Subdivision) => subdivision.countryCode
)
2025-08-23 17:47:03 +03:00
let countries = new Collection(data.countries).map(data => new Country(data))
let countriesKeyByCode = countries.keyBy((country: Country) => country.code)
2025-04-16 20:54:55 +03:00
2025-08-23 17:47:03 +03:00
const cities = new Collection(data.cities).map(data =>
new City(data)
2025-04-16 20:54:55 +03:00
.withRegions(regions)
2025-08-23 17:47:03 +03:00
.withCountry(countriesKeyByCode)
.withSubdivision(subdivisionsKeyByCode)
2025-07-20 20:28:58 +03:00
)
2025-08-23 17:47:03 +03:00
const citiesKeyByCode = cities.keyBy((city: City) => city.code)
const citiesGroupedByCountryCode = cities.groupBy((city: City) => city.countryCode)
const citiesGroupedBySubdivisionCode = cities.groupBy((city: City) => city.subdivisionCode)
2025-07-20 20:28:58 +03:00
2025-04-16 20:54:55 +03:00
const timezones = new Collection(data.timezones).map(data =>
new Timezone(data).withCountries(countriesKeyByCode)
)
const timezonesKeyById = timezones.keyBy((timezone: Timezone) => timezone.id)
2025-07-10 21:13:43 +03:00
const blocklistRecords = new Collection(data.blocklist).map(data => new BlocklistRecord(data))
const blocklistRecordsGroupedByChannelId = blocklistRecords.groupBy(
(blocklistRecord: BlocklistRecord) => blocklistRecord.channelId
)
2025-08-23 17:47:03 +03:00
let channels = new Collection(data.channels).map(data => new Channel(data))
let channelsKeyById = channels.keyBy((channel: Channel) => channel.id)
2025-07-10 21:13:43 +03:00
2025-08-23 17:47:03 +03:00
let feeds = new Collection(data.feeds).map(data => new Feed(data))
let feedsGroupedByChannelId = feeds.groupBy((feed: Feed) => feed.channelId)
let feedsGroupedById = feeds.groupBy((feed: Feed) => feed.id)
2025-04-16 20:54:55 +03:00
2025-07-29 04:01:22 +03:00
const logos = new Collection(data.logos).map(data => new Logo(data).withFeed(feedsGroupedById))
2025-07-10 21:13:43 +03:00
const logosGroupedByChannelId = logos.groupBy((logo: Logo) => logo.channelId)
const logosGroupedByStreamId = logos.groupBy((logo: Logo) => logo.getStreamId())
const streams = new Collection(data.streams).map(data =>
new Stream(data).withLogos(logosGroupedByStreamId)
)
const streamsGroupedById = streams.groupBy((stream: Stream) => stream.getId())
const guides = new Collection(data.guides).map(data => new Guide(data))
const guidesGroupedByStreamId = guides.groupBy((guide: Guide) => guide.getStreamId())
2025-08-23 17:47:03 +03:00
regions = regions.map((region: Region) =>
region
.withCountries(countriesKeyByCode)
.withRegions(regions)
.withSubdivisions(subdivisions)
.withCities(cities)
)
regionsKeyByCode = regions.keyBy((region: Region) => region.code)
countries = countries.map((country: Country) =>
country
.withCities(citiesGroupedByCountryCode)
.withSubdivisions(subdivisionsGroupedByCountryCode)
.withRegions(regions)
.withLanguage(languagesKeyByCode)
)
countriesKeyByCode = countries.keyBy((country: Country) => country.code)
subdivisions = subdivisions.map((subdivision: Subdivision) =>
subdivision
.withCities(citiesGroupedBySubdivisionCode)
.withCountry(countriesKeyByCode)
.withRegions(regions)
)
subdivisionsKeyByCode = subdivisions.keyBy((subdivision: Subdivision) => subdivision.code)
subdivisionsGroupedByCountryCode = subdivisions.groupBy(
(subdivision: Subdivision) => subdivision.countryCode
)
2025-07-10 21:13:43 +03:00
channels = channels.map((channel: Channel) =>
2025-08-23 17:47:03 +03:00
channel
.withFeeds(feedsGroupedByChannelId)
.withLogos(logosGroupedByChannelId)
.withCategories(categoriesKeyById)
.withCountry(countriesKeyByCode)
.withSubdivision(subdivisionsKeyByCode)
.withCategories(categoriesKeyById)
)
channelsKeyById = channels.keyBy((channel: Channel) => channel.id)
feeds = feeds.map((feed: Feed) =>
feed
.withChannel(channelsKeyById)
.withLanguages(languagesKeyByCode)
.withTimezones(timezonesKeyById)
.withBroadcastArea(
citiesKeyByCode,
subdivisionsKeyByCode,
countriesKeyByCode,
regionsKeyByCode
)
2025-07-10 21:13:43 +03:00
)
2025-08-23 17:47:03 +03:00
feedsGroupedByChannelId = feeds.groupBy((feed: Feed) => feed.channelId)
feedsGroupedById = feeds.groupBy((feed: Feed) => feed.id)
2025-04-16 20:54:55 +03:00
return {
blocklistRecordsGroupedByChannelId,
subdivisionsGroupedByCountryCode,
feedsGroupedByChannelId,
guidesGroupedByStreamId,
2025-07-10 21:13:43 +03:00
logosGroupedByStreamId,
2025-04-16 20:54:55 +03:00
subdivisionsKeyByCode,
countriesKeyByCode,
languagesKeyByCode,
streamsGroupedById,
categoriesKeyById,
timezonesKeyById,
regionsKeyByCode,
blocklistRecords,
channelsKeyById,
2025-08-23 17:47:03 +03:00
citiesKeyByCode,
2025-04-16 20:54:55 +03:00
subdivisions,
categories,
countries,
languages,
timezones,
channels,
regions,
streams,
2025-08-23 17:47:03 +03:00
cities,
2025-04-16 20:54:55 +03:00
guides,
2025-07-10 21:13:43 +03:00
feeds,
logos
2025-04-16 20:54:55 +03:00
}
}
}