From 45e2cab1e9f7fb392307ab39ea0196f0de51285c Mon Sep 17 00:00:00 2001 From: Zair Leon <73313698+notzairdev@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:31:55 -0600 Subject: [PATCH 1/2] Sort internal playlists after stream updates --- scripts/commands/playlist/format.ts | 12 ++--------- scripts/commands/playlist/update.ts | 8 ++++++++ scripts/core/sortStreams.ts | 17 ++++++++++++++++ .../expected/playlist_update/streams/uk.m3u | 4 ++-- tests/commands/playlist/update.test.ts | 20 +++++++++++++++++++ 5 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 scripts/core/sortStreams.ts diff --git a/scripts/commands/playlist/format.ts b/scripts/commands/playlist/format.ts index 45986c0a01..340d5bf697 100644 --- a/scripts/commands/playlist/format.ts +++ b/scripts/commands/playlist/format.ts @@ -9,6 +9,7 @@ import { loadData, data } from '../../api' import cliProgress from 'cli-progress' import { eachLimit } from 'async' import os from 'node:os' +import { sortStreams } from '../../core/sortStreams' program .argument('[filepath...]', 'Path to file to format') @@ -110,16 +111,7 @@ async function main() { progressBar.stop() logger.info('sorting links...') - streams = streams.sortBy( - [ - (stream: Stream) => stream.title, - (stream: Stream) => (stream.isGeoBlocked ? -1 : 0), - (stream: Stream) => (stream.isNot247 ? -1 : 0), - (stream: Stream) => stream.getVerticalResolution(), - (stream: Stream) => stream.url - ], - ['asc', 'desc', 'desc', 'desc', 'asc'] - ) + streams = sortStreams(streams) logger.info('saving...') const groupedStreams = streams.groupBy((stream: Stream) => stream.getFilepath()) diff --git a/scripts/commands/playlist/update.ts b/scripts/commands/playlist/update.ts index 5fc80007a4..5f7730f92e 100644 --- a/scripts/commands/playlist/update.ts +++ b/scripts/commands/playlist/update.ts @@ -6,9 +6,11 @@ import { Logger, Collection } from '@freearhey/core' import { Storage } from '@freearhey/storage-js' import { PlaylistParser } from '../../core' import * as sdk from '@iptv-org/sdk' +import { sortStreams } from '../../core/sortStreams' const processedIssues = new Collection() const skippedIssues = new Collection() +const changedFiles = new Set() const logger = new Logger({ level: 5 }) let streams = new Collection() @@ -67,6 +69,7 @@ async function saveStreams() { for (const filepath of groupedStreams.keys()) { let filteredStreams = new Collection(groupedStreams.get(filepath)) filteredStreams = filteredStreams.filter((stream: Stream) => stream.removed === false) + if (changedFiles.has(filepath)) filteredStreams = sortStreams(filteredStreams) const playlist = new Playlist(filteredStreams, { public: false, raw: true }) await streamsStorage.save(filepath, playlist.toString()) @@ -126,6 +129,7 @@ async function removeStream(issue: Issue) { const found: Stream = streams.first((_stream: Stream) => _stream.url === link.trim()) if (found) { found.removed = true + changedFiles.add(found.getFilepath()) changed = true log.info(`The stream with the URL "${link}" has been removed from the playlists`) } else { @@ -164,6 +168,7 @@ async function editStream(issue: Issue) { cacheData() + const previousFilepath = stream.getFilepath() stream.updateWithIssue(data) stream.setGuides(apiData.guidesGroupedByStreamId.get(stream.getId())) @@ -182,6 +187,8 @@ async function editStream(issue: Issue) { log.info('The stream description has been updated') + changedFiles.add(previousFilepath) + changedFiles.add(stream.getFilepath()) processedIssues.add(issue) } @@ -290,5 +297,6 @@ async function addStream(issue: Issue) { log.info('The stream has been added to playlists') + changedFiles.add(stream.getFilepath()) processedIssues.add(issue) } diff --git a/scripts/core/sortStreams.ts b/scripts/core/sortStreams.ts new file mode 100644 index 0000000000..d193640d9e --- /dev/null +++ b/scripts/core/sortStreams.ts @@ -0,0 +1,17 @@ +import { Collection } from '@freearhey/core' +import { Stream } from '../models' + +// Keep internal playlists in the same order as playlist:format without +// performing its URL normalization, deduplication or network probing. +export function sortStreams(streams: Collection): Collection { + return streams.sortBy( + [ + (stream: Stream) => stream.title, + (stream: Stream) => (stream.isGeoBlocked ? -1 : 0), + (stream: Stream) => (stream.isNot247 ? -1 : 0), + (stream: Stream) => stream.getVerticalResolution(), + (stream: Stream) => stream.url + ], + ['asc', 'desc', 'desc', 'desc', 'asc'] + ) +} diff --git a/tests/__data__/expected/playlist_update/streams/uk.m3u b/tests/__data__/expected/playlist_update/streams/uk.m3u index 50d842ba32..706682a4ce 100644 --- a/tests/__data__/expected/playlist_update/streams/uk.m3u +++ b/tests/__data__/expected/playlist_update/streams/uk.m3u @@ -3,7 +3,7 @@ http://1111296894.rsc.cdn77.org/LS-ATL-54548-6/index.m3u8 #EXTINF:-1 tvg-id="BBCNews.uk",BBC News HD (480p) [Geo-blocked] http://1111296894.rsc.cdn77.org/LS-ATL-54548-6/playlist.m3u8 -#EXTINF:-1 tvg-id="",Fox Sports (720p) [Not 24/7;Geo-blocked] -https://jmp2.uk/plu-5a74b8e1e22a61737979c6bf.m3u8 #EXTINF:-1 tvg-id="BeanoTV.uk@SD",Beano TV (1080p) https://a5b4bacecd47433dad06d3189fc7422e.mediatailor.us-east-1.amazonaws.com/v1/manifest/04fd913bb278d8775298c26fdca9d9841f37601f/RakutenTV-eu_BeanoTV/b1f233d5-847c-437d-aa4f-f73e67a85323/2.m3u8|Referer="https://referer.xyz/"|User-Agent="Mozilla/5.0 (iPhone; CPU iPhone OS 17_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1"|Origin="https://origin.xyz" +#EXTINF:-1 tvg-id="",Fox Sports (720p) [Not 24/7;Geo-blocked] +https://jmp2.uk/plu-5a74b8e1e22a61737979c6bf.m3u8 diff --git a/tests/commands/playlist/update.test.ts b/tests/commands/playlist/update.test.ts index f02d3ce617..33427b893e 100644 --- a/tests/commands/playlist/update.test.ts +++ b/tests/commands/playlist/update.test.ts @@ -13,6 +13,21 @@ beforeEach(() => { }) describe('playlist:update', () => { + it('leaves the order of unrelated playlists unchanged', () => { + const filepath = 'tests/__data__/output/streams/zz.m3u' + const original = [ + '#EXTM3U', + '#EXTINF:-1 tvg-id="",Zulu (720p)', + 'https://example.com/zulu.m3u8', + '#EXTINF:-1 tvg-id="",Alpha (720p)', + 'https://example.com/alpha.m3u8', + '' + ].join('\r\n') + fs.writeFileSync(filepath, original) + execSync(`${ENV_VAR} npm run playlist:update --silent`, { encoding: 'utf8' }) + expect(content(filepath)).toBe(original) + }) + it('can update playlists', async () => { let cmd = `${ENV_VAR} npm run playlist:update` if (!process.env.DEBUG) cmd += ' --silent' @@ -36,6 +51,11 @@ describe('playlist:update', () => { ) }) + const uk = content('tests/__data__/output/streams/uk.m3u') + expect(uk.indexOf(',BBC News')).toBeLessThan(uk.indexOf(',Beano TV')) + expect(uk.indexOf(',Beano TV')).toBeLessThan(uk.indexOf(',Fox Sports')) + expect(uk).toContain('|Referer="https://referer.xyz/"|User-Agent=') + expect(content('tests/__data__/output/logs/playlist_update.log')).toBe( content('tests/__data__/expected/playlist_update/playlist_update.log') ) From 630ae0c73298faa733cbc89b691809b82a8b834b Mon Sep 17 00:00:00 2001 From: Zair Leon <73313698+notzairdev@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:46:59 -0600 Subject: [PATCH 2/2] Test ordering after edits and removals --- tests/commands/playlist/update.test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/commands/playlist/update.test.ts b/tests/commands/playlist/update.test.ts index 33427b893e..95e65d5e37 100644 --- a/tests/commands/playlist/update.test.ts +++ b/tests/commands/playlist/update.test.ts @@ -13,6 +13,31 @@ beforeEach(() => { }) describe('playlist:update', () => { + it('sorts edited and removed playlists without losing entries or playback options', () => { + const entries = [ + '#EXTINF:-1 tvg-id="",Zulu (720p)', + 'https://example.com/zulu.m3u8', + '#EXTINF:-1 tvg-id="",Alpha (720p)', + '#EXTVLCOPT:http-referrer=https://example.com/', + 'https://example.com/alpha.m3u8', + '' + ].join('\r\n') + for (const country of ['cy', 'br_example']) { + fs.appendFileSync(`tests/__data__/output/streams/${country}.m3u`, entries) + } + + execSync(`${ENV_VAR} npm run playlist:update --silent`, { encoding: 'utf8' }) + for (const country of ['cy', 'br_example']) { + const output = content(`tests/__data__/output/streams/${country}.m3u`) + expect(output.indexOf(',Alpha (720p)')).toBeLessThan(output.indexOf(',Zulu (720p)')) + expect(output).toContain('#EXTVLCOPT:http-referrer=https://example.com/') + expect(output).toContain('https://example.com/alpha.m3u8') + expect(output).toContain('https://example.com/zulu.m3u8') + } + expect(content('tests/__data__/output/streams/cy.m3u')).toContain(',AdaTV') + expect(content('tests/__data__/output/streams/br_example.m3u')).toContain(',BBC America') + }) + it('leaves the order of unrelated playlists unchanged', () => { const filepath = 'tests/__data__/output/streams/zz.m3u' const original = [