Update scripts

This commit is contained in:
freearhey 2025-04-23 20:56:19 +03:00
parent 696fdf5780
commit ec5cdf0864
15 changed files with 144 additions and 118 deletions

View file

@ -1,7 +1,7 @@
import { Storage, Collection, Logger, Dictionary } from '@freearhey/core'
import { DataLoader, DataProcessor, PlaylistParser } from '../../core'
import { Channel, Feed, Playlist, Stream } from '../../models'
import type { ChannelSearchableData } from '../../types/channel'
import { Channel, Feed, Playlist, Stream } from '../../models'
import { DataProcessorData } from '../../types/dataProcessor'
import { DataLoaderData } from '../../types/dataLoader'
import { select, input } from '@inquirer/prompts'

View file

@ -1,27 +1,25 @@
import { Logger, Storage } from '@freearhey/core'
import { PlaylistParser, DataProcessor, DataLoader } from '../../core'
import type { DataProcessorData } from '../../types/dataProcessor'
import { DATA_DIR, LOGS_DIR, STREAMS_DIR } from '../../constants'
import type { DataLoaderData } from '../../types/dataLoader'
import { Logger, Storage, File } from '@freearhey/core'
import { Stream } from '../../models'
import { uniqueId } from 'lodash'
import {
IndexCategoryGenerator,
IndexLanguageGenerator,
IndexCountryGenerator,
IndexRegionGenerator,
CategoriesGenerator,
CountriesGenerator,
LanguagesGenerator,
RegionsGenerator,
IndexGenerator,
IndexCategoryGenerator,
IndexCountryGenerator,
IndexLanguageGenerator,
IndexRegionGenerator
IndexGenerator
} from '../../generators'
import { DATA_DIR, LOGS_DIR, STREAMS_DIR } from '../../constants'
import type { DataProcessorData } from '../../types/dataProcessor'
import type { DataLoaderData } from '../../types/dataLoader'
async function main() {
const logger = new Logger()
const generatorsLogger = new Logger({
stream: await new Storage(LOGS_DIR).createStream(`generators.log`)
})
const logFile = new File('generators.log')
logger.info('loading data from api...')
const processor = new DataProcessor()
@ -29,19 +27,19 @@ async function main() {
const loader = new DataLoader({ storage: dataStorage })
const data: DataLoaderData = await loader.load()
const {
feedsGroupedByChannelId,
channelsKeyById,
categories,
countries,
regions,
channelsKeyById,
feedsGroupedByChannelId
regions
}: DataProcessorData = processor.process(data)
logger.info('loading streams...')
const streamsStorage = new Storage(STREAMS_DIR)
const parser = new PlaylistParser({
storage: streamsStorage,
channelsKeyById,
feedsGroupedByChannelId
feedsGroupedByChannelId,
channelsKeyById
})
const files = await streamsStorage.list('**/*.m3u')
let streams = await parser.parse(files)
@ -62,42 +60,46 @@ async function main() {
)
logger.info('generating categories/...')
await new CategoriesGenerator({ categories, streams, logger: generatorsLogger }).generate()
await new CategoriesGenerator({ categories, streams, logFile }).generate()
logger.info('generating countries/...')
await new CountriesGenerator({
countries,
streams,
logger: generatorsLogger
logFile
}).generate()
logger.info('generating languages/...')
await new LanguagesGenerator({ streams, logger: generatorsLogger }).generate()
await new LanguagesGenerator({ streams, logFile }).generate()
logger.info('generating regions/...')
await new RegionsGenerator({
streams,
regions,
logger: generatorsLogger
logFile
}).generate()
logger.info('generating index.m3u...')
await new IndexGenerator({ streams, logger: generatorsLogger }).generate()
await new IndexGenerator({ streams, logFile }).generate()
logger.info('generating index.category.m3u...')
await new IndexCategoryGenerator({ streams, logger: generatorsLogger }).generate()
await new IndexCategoryGenerator({ streams, logFile }).generate()
logger.info('generating index.country.m3u...')
await new IndexCountryGenerator({
streams,
logger: generatorsLogger
logFile
}).generate()
logger.info('generating index.language.m3u...')
await new IndexLanguageGenerator({ streams, logger: generatorsLogger }).generate()
await new IndexLanguageGenerator({ streams, logFile }).generate()
logger.info('generating index.region.m3u...')
await new IndexRegionGenerator({ streams, regions, logger: generatorsLogger }).generate()
await new IndexRegionGenerator({ streams, regions, logFile }).generate()
logger.info('saving generators.log...')
const logStorage = new Storage(LOGS_DIR)
logStorage.saveFile(logFile)
}
main()

View file

@ -159,7 +159,7 @@ function onFinish() {
drawTable()
logger.error(`\n${errors + warnings} problems (${errors} errors, ${warnings} warnings)`)
console.log(chalk.red(`\n${errors + warnings} problems (${errors} errors, ${warnings} warnings)`))
if (errors > 0) {
process.exit(1)

View file

@ -94,14 +94,14 @@ async function main() {
})
if (log.notEmpty()) {
logger.info(`\n${chalk.underline(filepath)}`)
console.log(`\n${chalk.underline(filepath)}`)
log.forEach((logItem: LogItem) => {
const position = logItem.line.toString().padEnd(6, ' ')
const type = logItem.type.padEnd(9, ' ')
const status = logItem.type === 'error' ? chalk.red(type) : chalk.yellow(type)
logger.info(` ${chalk.gray(position)}${status}${logItem.message}`)
console.log(` ${chalk.gray(position)}${status}${logItem.message}`)
})
errors = errors.concat(log.filter((logItem: LogItem) => logItem.type === 'error'))
@ -109,7 +109,7 @@ async function main() {
}
}
logger.error(
console.log(
chalk.red(
`\n${
errors.count() + warnings.count()

View file

@ -1,25 +1,26 @@
import { Generator } from './generator'
import { Collection, Storage, Logger } from '@freearhey/core'
import { Collection, Storage, Logger, File } from '@freearhey/core'
import { Stream, Category, Playlist } from '../models'
import { PUBLIC_DIR } from '../constants'
import { Generator } from './generator'
import { EOL } from 'node:os'
type CategoriesGeneratorProps = {
streams: Collection
categories: Collection
logger: Logger
logFile: File
}
export class CategoriesGenerator implements Generator {
streams: Collection
categories: Collection
storage: Storage
logger: Logger
logFile: File
constructor({ streams, categories, logger }: CategoriesGeneratorProps) {
constructor({ streams, categories, logFile }: CategoriesGeneratorProps) {
this.streams = streams
this.categories = categories
this.storage = new Storage(PUBLIC_DIR)
this.logger = logger
this.logFile = logFile
}
async generate() {
@ -37,8 +38,8 @@ export class CategoriesGenerator implements Generator {
const playlist = new Playlist(categoryStreams, { public: true })
const filepath = `categories/${category.id}.m3u`
await this.storage.save(filepath, playlist.toString())
this.logger.info(
JSON.stringify({ type: 'category', filepath, count: playlist.streams.count() })
this.logFile.append(
JSON.stringify({ type: 'category', filepath, count: playlist.streams.count() }) + EOL
)
})
@ -46,8 +47,8 @@ export class CategoriesGenerator implements Generator {
const playlist = new Playlist(undefinedStreams, { public: true })
const filepath = 'categories/undefined.m3u'
await this.storage.save(filepath, playlist.toString())
this.logger.info(
JSON.stringify({ type: 'category', filepath, count: playlist.streams.count() })
this.logFile.append(
JSON.stringify({ type: 'category', filepath, count: playlist.streams.count() }) + EOL
)
}
}

View file

@ -1,25 +1,26 @@
import { Generator } from './generator'
import { Collection, Storage, Logger } from '@freearhey/core'
import { Country, Subdivision, Stream, Playlist } from '../models'
import { Collection, Storage, File } from '@freearhey/core'
import { PUBLIC_DIR } from '../constants'
import { Generator } from './generator'
import { EOL } from 'node:os'
type CountriesGeneratorProps = {
streams: Collection
countries: Collection
logger: Logger
logFile: File
}
export class CountriesGenerator implements Generator {
streams: Collection
countries: Collection
storage: Storage
logger: Logger
logFile: File
constructor({ streams, countries, logger }: CountriesGeneratorProps) {
constructor({ streams, countries, logFile }: CountriesGeneratorProps) {
this.streams = streams
this.countries = countries
this.storage = new Storage(PUBLIC_DIR)
this.logger = logger
this.logFile = logFile
}
async generate(): Promise<void> {
@ -36,8 +37,8 @@ export class CountriesGenerator implements Generator {
const playlist = new Playlist(countryStreams, { public: true })
const filepath = `countries/${country.code.toLowerCase()}.m3u`
await this.storage.save(filepath, playlist.toString())
this.logger.info(
JSON.stringify({ type: 'country', filepath, count: playlist.streams.count() })
this.logFile.append(
JSON.stringify({ type: 'country', filepath, count: playlist.streams.count() }) + EOL
)
country.getSubdivisions().forEach(async (subdivision: Subdivision) => {
@ -50,8 +51,8 @@ export class CountriesGenerator implements Generator {
const playlist = new Playlist(subdivisionStreams, { public: true })
const filepath = `subdivisions/${subdivision.code.toLowerCase()}.m3u`
await this.storage.save(filepath, playlist.toString())
this.logger.info(
JSON.stringify({ type: 'subdivision', filepath, count: playlist.streams.count() })
this.logFile.append(
JSON.stringify({ type: 'subdivision', filepath, count: playlist.streams.count() }) + EOL
)
})
})
@ -60,12 +61,12 @@ export class CountriesGenerator implements Generator {
const undefinedPlaylist = new Playlist(undefinedStreams, { public: true })
const undefinedFilepath = 'countries/undefined.m3u'
await this.storage.save(undefinedFilepath, undefinedPlaylist.toString())
this.logger.info(
this.logFile.append(
JSON.stringify({
type: 'country',
filepath: undefinedFilepath,
count: undefinedPlaylist.streams.count()
})
}) + EOL
)
}
}

View file

@ -1,22 +1,23 @@
import { Generator } from './generator'
import { Collection, Storage, Logger } from '@freearhey/core'
import { Collection, Storage, File } from '@freearhey/core'
import { Stream, Playlist, Category } from '../models'
import { PUBLIC_DIR } from '../constants'
import { Generator } from './generator'
import { EOL } from 'node:os'
type IndexCategoryGeneratorProps = {
streams: Collection
logger: Logger
logFile: File
}
export class IndexCategoryGenerator implements Generator {
streams: Collection
storage: Storage
logger: Logger
logFile: File
constructor({ streams, logger }: IndexCategoryGeneratorProps) {
constructor({ streams, logFile }: IndexCategoryGeneratorProps) {
this.streams = streams
this.storage = new Storage(PUBLIC_DIR)
this.logger = logger
this.logFile = logFile
}
async generate(): Promise<void> {
@ -48,6 +49,8 @@ export class IndexCategoryGenerator implements Generator {
const playlist = new Playlist(groupedStreams, { public: true })
const filepath = 'index.category.m3u'
await this.storage.save(filepath, playlist.toString())
this.logger.info(JSON.stringify({ type: 'index', filepath, count: playlist.streams.count() }))
this.logFile.append(
JSON.stringify({ type: 'index', filepath, count: playlist.streams.count() }) + EOL
)
}
}

View file

@ -1,22 +1,23 @@
import { Generator } from './generator'
import { Collection, Storage, Logger } from '@freearhey/core'
import { Collection, Storage, File } from '@freearhey/core'
import { Stream, Playlist, Country } from '../models'
import { PUBLIC_DIR } from '../constants'
import { Generator } from './generator'
import { EOL } from 'node:os'
type IndexCountryGeneratorProps = {
streams: Collection
logger: Logger
logFile: File
}
export class IndexCountryGenerator implements Generator {
streams: Collection
storage: Storage
logger: Logger
logFile: File
constructor({ streams, logger }: IndexCountryGeneratorProps) {
constructor({ streams, logFile }: IndexCountryGeneratorProps) {
this.streams = streams
this.storage = new Storage(PUBLIC_DIR)
this.logger = logger
this.logFile = logFile
}
async generate(): Promise<void> {
@ -56,6 +57,8 @@ export class IndexCountryGenerator implements Generator {
const playlist = new Playlist(groupedStreams, { public: true })
const filepath = 'index.country.m3u'
await this.storage.save(filepath, playlist.toString())
this.logger.info(JSON.stringify({ type: 'index', filepath, count: playlist.streams.count() }))
this.logFile.append(
JSON.stringify({ type: 'index', filepath, count: playlist.streams.count() }) + EOL
)
}
}

View file

@ -1,22 +1,23 @@
import { Collection, Logger, Storage } from '@freearhey/core'
import { Collection, File, Storage } from '@freearhey/core'
import { Stream, Playlist } from '../models'
import { Generator } from './generator'
import { PUBLIC_DIR } from '../constants'
import { Generator } from './generator'
import { EOL } from 'node:os'
type IndexGeneratorProps = {
streams: Collection
logger: Logger
logFile: File
}
export class IndexGenerator implements Generator {
streams: Collection
storage: Storage
logger: Logger
logFile: File
constructor({ streams, logger }: IndexGeneratorProps) {
constructor({ streams, logFile }: IndexGeneratorProps) {
this.streams = streams
this.storage = new Storage(PUBLIC_DIR)
this.logger = logger
this.logFile = logFile
}
async generate(): Promise<void> {
@ -27,6 +28,8 @@ export class IndexGenerator implements Generator {
const playlist = new Playlist(sfwStreams, { public: true })
const filepath = 'index.m3u'
await this.storage.save(filepath, playlist.toString())
this.logger.info(JSON.stringify({ type: 'index', filepath, count: playlist.streams.count() }))
this.logFile.append(
JSON.stringify({ type: 'index', filepath, count: playlist.streams.count() }) + EOL
)
}
}

View file

@ -1,22 +1,23 @@
import { Generator } from './generator'
import { Collection, Storage, Logger } from '@freearhey/core'
import { Collection, Storage, File } from '@freearhey/core'
import { Stream, Playlist, Language } from '../models'
import { PUBLIC_DIR } from '../constants'
import { Generator } from './generator'
import { EOL } from 'node:os'
type IndexLanguageGeneratorProps = {
streams: Collection
logger: Logger
logFile: File
}
export class IndexLanguageGenerator implements Generator {
streams: Collection
storage: Storage
logger: Logger
logFile: File
constructor({ streams, logger }: IndexLanguageGeneratorProps) {
constructor({ streams, logFile }: IndexLanguageGeneratorProps) {
this.streams = streams
this.storage = new Storage(PUBLIC_DIR)
this.logger = logger
this.logFile = logFile
}
async generate(): Promise<void> {
@ -47,6 +48,8 @@ export class IndexLanguageGenerator implements Generator {
const playlist = new Playlist(groupedStreams, { public: true })
const filepath = 'index.language.m3u'
await this.storage.save(filepath, playlist.toString())
this.logger.info(JSON.stringify({ type: 'index', filepath, count: playlist.streams.count() }))
this.logFile.append(
JSON.stringify({ type: 'index', filepath, count: playlist.streams.count() }) + EOL
)
}
}

View file

@ -1,22 +1,23 @@
import { Collection, Logger, Storage } from '@freearhey/core'
import { Collection, File, Storage } from '@freearhey/core'
import { Stream, Playlist } from '../models'
import { Generator } from './generator'
import { PUBLIC_DIR } from '../constants'
import { Generator } from './generator'
import { EOL } from 'node:os'
type IndexNsfwGeneratorProps = {
streams: Collection
logger: Logger
logFile: File
}
export class IndexNsfwGenerator implements Generator {
streams: Collection
storage: Storage
logger: Logger
logFile: File
constructor({ streams, logger }: IndexNsfwGeneratorProps) {
constructor({ streams, logFile }: IndexNsfwGeneratorProps) {
this.streams = streams
this.storage = new Storage(PUBLIC_DIR)
this.logger = logger
this.logFile = logFile
}
async generate(): Promise<void> {
@ -25,6 +26,8 @@ export class IndexNsfwGenerator implements Generator {
const playlist = new Playlist(allStreams, { public: true })
const filepath = 'index.nsfw.m3u'
await this.storage.save(filepath, playlist.toString())
this.logger.info(JSON.stringify({ type: 'index', filepath, count: playlist.streams.count() }))
this.logFile.append(
JSON.stringify({ type: 'index', filepath, count: playlist.streams.count() }) + EOL
)
}
}

View file

@ -1,25 +1,26 @@
import { Generator } from './generator'
import { Collection, Storage, Logger } from '@freearhey/core'
import { Collection, Storage, File } from '@freearhey/core'
import { Stream, Playlist, Region } from '../models'
import { PUBLIC_DIR } from '../constants'
import { Generator } from './generator'
import { EOL } from 'node:os'
type IndexRegionGeneratorProps = {
streams: Collection
regions: Collection
logger: Logger
logFile: File
}
export class IndexRegionGenerator implements Generator {
streams: Collection
regions: Collection
storage: Storage
logger: Logger
logFile: File
constructor({ streams, regions, logger }: IndexRegionGeneratorProps) {
constructor({ streams, regions, logFile }: IndexRegionGeneratorProps) {
this.streams = streams
this.regions = regions
this.storage = new Storage(PUBLIC_DIR)
this.logger = logger
this.logFile = logFile
}
async generate(): Promise<void> {
@ -58,6 +59,8 @@ export class IndexRegionGenerator implements Generator {
const playlist = new Playlist(groupedStreams, { public: true })
const filepath = 'index.region.m3u'
await this.storage.save(filepath, playlist.toString())
this.logger.info(JSON.stringify({ type: 'index', filepath, count: playlist.streams.count() }))
this.logFile.append(
JSON.stringify({ type: 'index', filepath, count: playlist.streams.count() }) + EOL
)
}
}

View file

@ -1,19 +1,20 @@
import { Generator } from './generator'
import { Collection, Storage, Logger } from '@freearhey/core'
import { Collection, Storage, File } from '@freearhey/core'
import { Playlist, Language, Stream } from '../models'
import { PUBLIC_DIR } from '../constants'
import { Generator } from './generator'
import { EOL } from 'node:os'
type LanguagesGeneratorProps = { streams: Collection; logger: Logger }
type LanguagesGeneratorProps = { streams: Collection; logFile: File }
export class LanguagesGenerator implements Generator {
streams: Collection
storage: Storage
logger: Logger
logFile: File
constructor({ streams, logger }: LanguagesGeneratorProps) {
constructor({ streams, logFile }: LanguagesGeneratorProps) {
this.streams = streams
this.storage = new Storage(PUBLIC_DIR)
this.logger = logger
this.logFile = logFile
}
async generate(): Promise<void> {
@ -38,8 +39,8 @@ export class LanguagesGenerator implements Generator {
const playlist = new Playlist(languageStreams, { public: true })
const filepath = `languages/${language.code}.m3u`
await this.storage.save(filepath, playlist.toString())
this.logger.info(
JSON.stringify({ type: 'language', filepath, count: playlist.streams.count() })
this.logFile.append(
JSON.stringify({ type: 'language', filepath, count: playlist.streams.count() }) + EOL
)
})
@ -50,8 +51,8 @@ export class LanguagesGenerator implements Generator {
const playlist = new Playlist(undefinedStreams, { public: true })
const filepath = 'languages/undefined.m3u'
await this.storage.save(filepath, playlist.toString())
this.logger.info(
JSON.stringify({ type: 'language', filepath, count: playlist.streams.count() })
this.logFile.append(
JSON.stringify({ type: 'language', filepath, count: playlist.streams.count() }) + EOL
)
}
}

View file

@ -1,25 +1,26 @@
import { Generator } from './generator'
import { Collection, Storage, Logger } from '@freearhey/core'
import { Collection, Storage, File } from '@freearhey/core'
import { Playlist, Region, Stream } from '../models'
import { PUBLIC_DIR } from '../constants'
import { Generator } from './generator'
import { EOL } from 'node:os'
type RegionsGeneratorProps = {
streams: Collection
regions: Collection
logger: Logger
logFile: File
}
export class RegionsGenerator implements Generator {
streams: Collection
regions: Collection
storage: Storage
logger: Logger
logFile: File
constructor({ streams, regions, logger }: RegionsGeneratorProps) {
constructor({ streams, regions, logFile }: RegionsGeneratorProps) {
this.streams = streams
this.regions = regions
this.storage = new Storage(PUBLIC_DIR)
this.logger = logger
this.logFile = logFile
}
async generate(): Promise<void> {
@ -35,8 +36,8 @@ export class RegionsGenerator implements Generator {
const playlist = new Playlist(regionStreams, { public: true })
const filepath = `regions/${region.code.toLowerCase()}.m3u`
await this.storage.save(filepath, playlist.toString())
this.logger.info(
JSON.stringify({ type: 'region', filepath, count: playlist.streams.count() })
this.logFile.append(
JSON.stringify({ type: 'region', filepath, count: playlist.streams.count() }) + EOL
)
})
@ -44,18 +45,20 @@ export class RegionsGenerator implements Generator {
const internationalPlaylist = new Playlist(internationalStreams, { public: true })
const internationalFilepath = 'regions/int.m3u'
await this.storage.save(internationalFilepath, internationalPlaylist.toString())
this.logger.info(
this.logFile.append(
JSON.stringify({
type: 'region',
filepath: internationalFilepath,
count: internationalPlaylist.streams.count()
})
}) + EOL
)
const undefinedStreams = streams.filter((stream: Stream) => !stream.hasBroadcastArea())
const playlist = new Playlist(undefinedStreams, { public: true })
const filepath = 'regions/undefined.m3u'
await this.storage.save(filepath, playlist.toString())
this.logger.info(JSON.stringify({ type: 'region', filepath, count: playlist.streams.count() }))
this.logFile.append(
JSON.stringify({ type: 'region', filepath, count: playlist.streams.count() }) + EOL
)
}
}

View file

@ -144,8 +144,8 @@ export class Feed {
this.broadcastRegions = regions.filter((region: Region) => {
if (region.code === 'INT') return false
return region.countryCodes.intersects(countriesCodes)
const intersected = region.countryCodes.intersects(countriesCodes)
return intersected.notEmpty()
})
return this