From 834abac5e4d8700fdd82ed608a6899b15f275ded Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Sat, 2 May 2026 12:08:00 +0300 Subject: [PATCH] wip --- scripts/commands/playlist/update.ts | 74 +++++++++++-------- .../playlist_update/playlist_update.log | 1 + .../{ => streams}/br_example.m3u | 0 .../playlist_update/{ => streams}/bz.m3u | 0 .../playlist_update/{ => streams}/cy.m3u | 0 .../playlist_update/{ => streams}/fr.m3u | 0 .../playlist_update/{ => streams}/uk.m3u | 0 .../playlist_update/{ => streams}/us.m3u | 0 tests/commands/playlist/update.test.ts | 22 +++--- 9 files changed, 56 insertions(+), 41 deletions(-) create mode 100644 tests/__data__/expected/playlist_update/playlist_update.log rename tests/__data__/expected/playlist_update/{ => streams}/br_example.m3u (100%) rename tests/__data__/expected/playlist_update/{ => streams}/bz.m3u (100%) rename tests/__data__/expected/playlist_update/{ => streams}/cy.m3u (100%) rename tests/__data__/expected/playlist_update/{ => streams}/fr.m3u (100%) rename tests/__data__/expected/playlist_update/{ => streams}/uk.m3u (100%) rename tests/__data__/expected/playlist_update/{ => streams}/us.m3u (100%) diff --git a/scripts/commands/playlist/update.ts b/scripts/commands/playlist/update.ts index 5165b0b2b0..5fb8c23a79 100644 --- a/scripts/commands/playlist/update.ts +++ b/scripts/commands/playlist/update.ts @@ -1,9 +1,9 @@ import { isURI, getStreamInfo, loadIssues } from '../../utils' +import { STREAMS_DIR, LOGS_DIR } from '../../constants' import { Playlist, Issue, Stream } from '../../models' import { loadData, data as apiData } from '../../api' import { Logger, Collection } from '@freearhey/core' import { Storage } from '@freearhey/storage-js' -import { STREAMS_DIR } from '../../constants' import { PlaylistParser } from '../../core' import * as sdk from '@iptv-org/sdk' @@ -12,51 +12,61 @@ const processedIssues = new Collection() async function main() { const logger = new Logger({ level: -999 }) - logger.info('loading issues...') - const issues = await loadIssues() - logger.info('loading data from api...') await loadData() + logger.info('loading issues...') + const issues = await loadIssues() + logger.info('loading streams...') - const streamsStorage = new Storage(STREAMS_DIR) - const parser = new PlaylistParser({ - storage: streamsStorage - }) - const files = await streamsStorage.list('**/*.m3u') - const streams = await parser.parse(files) + const streams = await loadStreams() logger.info('removing streams...') await removeStreams({ streams, issues }) logger.info('edit stream description...') - await editStreams({ - streams, - issues - }) + await editStreams({ streams, issues }) logger.info('add new streams...') - await addStreams({ - streams, - issues - }) + await addStreams({ streams, issues }) - logger.info('saving...') - const groupedStreams = streams.groupBy((stream: Stream) => stream.getFilepath()) - for (const filepath of groupedStreams.keys()) { - let streams = new Collection(groupedStreams.get(filepath)) - streams = streams.filter((stream: Stream) => stream.removed === false) + logger.info('saving streams...') + await saveStreams({ streams }) - const playlist = new Playlist(streams, { public: false }) - await streamsStorage.save(filepath, playlist.toString()) - } - - const output = processedIssues.map(issue_number => `closes #${issue_number}`).join(', ') - console.log(`OUTPUT=${output}`) + logger.info('saving logs...') + await saveLogs() } main() +async function saveLogs() { + const logStorage = new Storage(LOGS_DIR) + const output = processedIssues.map((issue: Issue) => `closes #${issue.number}`).join(', ') + await logStorage.save('playlist_update.log', output) +} + +async function saveStreams({ streams }) { + const streamsStorage = new Storage(STREAMS_DIR) + const groupedStreams = streams.groupBy((stream: Stream) => stream.getFilepath()) + for (const filepath of groupedStreams.keys()) { + let filteredStreams = new Collection(groupedStreams.get(filepath)) + filteredStreams = filteredStreams.filter((stream: Stream) => stream.removed === false) + + const playlist = new Playlist(filteredStreams, { public: false }) + await streamsStorage.save(filepath, playlist.toString()) + } +} + +async function loadStreams() { + const streamsStorage = new Storage(STREAMS_DIR) + const parser = new PlaylistParser({ + storage: streamsStorage + }) + const files = await streamsStorage.list('**/*.m3u') + + return await parser.parse(files) +} + async function removeStreams({ streams, issues @@ -86,7 +96,7 @@ async function removeStreams({ } }) - if (changed) processedIssues.add(issue.number) + if (changed) processedIssues.add(issue) }) } @@ -121,7 +131,7 @@ async function editStreams({ stream.updateWithIssue(data) - processedIssues.add(issue.number) + processedIssues.add(issue) }) } @@ -181,6 +191,6 @@ async function addStreams({ stream.updateTitle().updateFilepath() streams.add(stream) - processedIssues.add(issue.number) + processedIssues.add(issue) } } diff --git a/tests/__data__/expected/playlist_update/playlist_update.log b/tests/__data__/expected/playlist_update/playlist_update.log new file mode 100644 index 0000000000..a15e8a49d8 --- /dev/null +++ b/tests/__data__/expected/playlist_update/playlist_update.log @@ -0,0 +1 @@ +closes #14151, closes #14150, closes #14110, closes #14120, closes #14175, closes #14105, closes #14104, closes #14057, closes #14034, closes #13964, closes #13893, closes #13881, closes #13793, closes #13751, closes #13715 \ No newline at end of file diff --git a/tests/__data__/expected/playlist_update/br_example.m3u b/tests/__data__/expected/playlist_update/streams/br_example.m3u similarity index 100% rename from tests/__data__/expected/playlist_update/br_example.m3u rename to tests/__data__/expected/playlist_update/streams/br_example.m3u diff --git a/tests/__data__/expected/playlist_update/bz.m3u b/tests/__data__/expected/playlist_update/streams/bz.m3u similarity index 100% rename from tests/__data__/expected/playlist_update/bz.m3u rename to tests/__data__/expected/playlist_update/streams/bz.m3u diff --git a/tests/__data__/expected/playlist_update/cy.m3u b/tests/__data__/expected/playlist_update/streams/cy.m3u similarity index 100% rename from tests/__data__/expected/playlist_update/cy.m3u rename to tests/__data__/expected/playlist_update/streams/cy.m3u diff --git a/tests/__data__/expected/playlist_update/fr.m3u b/tests/__data__/expected/playlist_update/streams/fr.m3u similarity index 100% rename from tests/__data__/expected/playlist_update/fr.m3u rename to tests/__data__/expected/playlist_update/streams/fr.m3u diff --git a/tests/__data__/expected/playlist_update/uk.m3u b/tests/__data__/expected/playlist_update/streams/uk.m3u similarity index 100% rename from tests/__data__/expected/playlist_update/uk.m3u rename to tests/__data__/expected/playlist_update/streams/uk.m3u diff --git a/tests/__data__/expected/playlist_update/us.m3u b/tests/__data__/expected/playlist_update/streams/us.m3u similarity index 100% rename from tests/__data__/expected/playlist_update/us.m3u rename to tests/__data__/expected/playlist_update/streams/us.m3u diff --git a/tests/commands/playlist/update.test.ts b/tests/commands/playlist/update.test.ts index 8e4cf99e01..afd03cc574 100644 --- a/tests/commands/playlist/update.test.ts +++ b/tests/commands/playlist/update.test.ts @@ -4,7 +4,7 @@ import * as fs from 'fs-extra' import { glob } from 'glob' const ENV_VAR = - 'cross-env DATA_DIR=tests/__data__/input/data STREAMS_DIR=tests/__data__/output/streams' + 'cross-env DATA_DIR=tests/__data__/input/data STREAMS_DIR=tests/__data__/output/streams LOGS_DIR=tests/__data__/output/logs' beforeEach(() => { fs.emptyDirSync('tests/__data__/output') @@ -19,21 +19,25 @@ describe('playlist:update', () => { const stdout = execSync(cmd, { encoding: 'utf8' }) if (process.env.DEBUG === 'true') console.log(cmd, stdout) - const files = glob.sync('tests/__data__/expected/playlist_update/*.m3u').map(filepath => { - const fileUrl = pathToFileURL(filepath).toString() - const pathToRemove = pathToFileURL('tests/__data__/expected/playlist_update/').toString() + const files = glob + .sync('tests/__data__/expected/playlist_update/streams/*.m3u') + .map(filepath => { + const fileUrl = pathToFileURL(filepath).toString() + const pathToRemove = pathToFileURL( + 'tests/__data__/expected/playlist_update/streams/' + ).toString() - return fileUrl.replace(pathToRemove, '') - }) + return fileUrl.replace(pathToRemove, '') + }) files.forEach(filepath => { expect(content(`tests/__data__/output/streams/${filepath}`)).toBe( - content(`tests/__data__/expected/playlist_update/${filepath}`) + content(`tests/__data__/expected/playlist_update/streams/${filepath}`) ) }) - expect(stdout).toBe( - 'OUTPUT=closes #14151, closes #14150, closes #14110, closes #14120, closes #14175, closes #14105, closes #14104, closes #14057, closes #14034, closes #13964, closes #13893, closes #13881, closes #13793, closes #13751, closes #13715\n' + expect(content('tests/__data__/output/logs/playlist_update.log')).toBe( + content('tests/__data__/expected/playlist_update/playlist_update.log') ) done()