From 6c00393c6bfcdceaca4d4e724f68ba056db9f0ad Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Tue, 9 Dec 2025 04:19:30 +0300 Subject: [PATCH] Update test.ts --- scripts/commands/playlist/test.ts | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/scripts/commands/playlist/test.ts b/scripts/commands/playlist/test.ts index 9446e8c4ad..d428aca167 100644 --- a/scripts/commands/playlist/test.ts +++ b/scripts/commands/playlist/test.ts @@ -4,13 +4,13 @@ import { ROOT_DIR, STREAMS_DIR } from '../../constants' import { Logger, Collection } from '@freearhey/core' import { program, OptionValues } from 'commander' import { Storage } from '@freearhey/storage-js' -import { Stream } from '../../models' +import { Playlist, Stream } from '../../models' +import { truncate } from '../../utils' import { loadData } from '../../api' import { eachLimit } from 'async' import dns from 'node:dns' import chalk from 'chalk' import os from 'node:os' -import { truncate } from '../../utils' const LIVE_UPDATE_INTERVAL = 5000 const LIVE_UPDATE_MAX_STREAMS = 100 @@ -21,6 +21,7 @@ const results: { [key: string]: string } = {} let interval: string | number | NodeJS.Timeout | undefined let streams = new Collection() let isLiveUpdateEnabled = true +const errorStatusCodes = ['ENOTFOUND', 'HTTP_404_NOT_FOUND', 'HTTP_404_UNKONWN_ERROR'] program .argument('[filepath...]', 'Path to file to test') @@ -37,12 +38,14 @@ program (value: string) => parseInt(value), 30000 ) + .option('--fix', 'Remove all broken links found from files') .parse(process.argv) const options: OptionValues = program.opts() const logger = new Logger() const tester = new StreamTester({ options }) +const rootStorage = new Storage(ROOT_DIR) async function main() { if (await isOffline()) { @@ -54,7 +57,6 @@ async function main() { await loadData() logger.info('loading streams...') - const rootStorage = new Storage(ROOT_DIR) const parser = new PlaylistParser({ storage: rootStorage }) @@ -94,8 +96,9 @@ async function runTest(stream: Stream) { const result: StreamTesterResult = await tester.test(stream) + stream.statusCode = result.status.code + let status = '' - const errorStatusCodes = ['ENOTFOUND', 'HTTP_404_NOT_FOUND', 'HTTP_404_UNKONWN_ERROR'] if (result.status.ok) status = chalk.green('OK') else if (errorStatusCodes.includes(result.status.code)) { status = chalk.red(result.status.code) @@ -144,7 +147,21 @@ function drawTable() { } } -function onFinish(error: Error | null | undefined) { +async function removeBrokenLinks() { + const streamsGrouped = streams.groupBy((stream: Stream) => stream.filepath) + for (const filepath of streamsGrouped.keys()) { + let streams: Collection = new Collection(streamsGrouped.get(filepath)) + + streams = streams.filter((stream: Stream) => + !stream.statusCode ? true : !errorStatusCodes.includes(stream.statusCode) + ) + + const playlist = new Playlist(streams, { public: false }) + await rootStorage.save(filepath, playlist.toString()) + } +} + +async function onFinish(error: Error | null | undefined) { clearInterval(interval) if (error) { @@ -152,6 +169,10 @@ function onFinish(error: Error | null | undefined) { process.exit(1) } + if (options.fix) { + await removeBrokenLinks() + } + drawTable() if (errors > 0 || warnings > 0) {