From 5d24fc69fc44275125a4eaf2a70e0a47cc35fa15 Mon Sep 17 00:00:00 2001 From: SpaceMonkey Date: Sat, 1 Mar 2025 20:51:53 -0300 Subject: [PATCH] Add support for multiline streams --- .forgejo/scripts/cultural-groups.js | 55 +++++++++++++++++------------ 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/.forgejo/scripts/cultural-groups.js b/.forgejo/scripts/cultural-groups.js index a1a0d80..8d6bf7c 100644 --- a/.forgejo/scripts/cultural-groups.js +++ b/.forgejo/scripts/cultural-groups.js @@ -124,7 +124,6 @@ function splitByCulturalGroup(filePath) { .map(file => file.toLowerCase()); const groups = {}; - let currentExtinf = null; // Verify M3U header const header = lines[0]; @@ -132,31 +131,43 @@ function splitByCulturalGroup(filePath) { throw new Error('Invalid M3U file: Missing #EXTM3U header'); } - // Process lines - lines.forEach(line => { - line = line.trim(); - if (!line) return; + // Process the file line by line to handle multi-line entries + let currentCulturalGroup = null; + let currentEntry = []; + let isInEntry = false; + + for (let i = 1; i < lines.length; i++) { + const line = lines[i].trim(); + if (!line) continue; if (line.startsWith('#EXTINF')) { - currentExtinf = line; - const culturalGroup = getCulturalGroup(line); + // Start of a new entry + isInEntry = true; + currentEntry = [line]; - // Only add to a group if there's a match - if (culturalGroup) { - if (!groups[culturalGroup]) { - groups[culturalGroup] = ['#EXTM3U']; + // Determine cultural group + currentCulturalGroup = getCulturalGroup(line); + } else if (isInEntry) { + // Add the line to the current entry + currentEntry.push(line); + + // If this is a URL line (doesn't start with #), this completes the entry + if (!line.startsWith('#')) { + // Add all lines of the entry to the appropriate cultural group + if (currentCulturalGroup) { + if (!groups[currentCulturalGroup]) { + groups[currentCulturalGroup] = ['#EXTM3U']; + } + groups[currentCulturalGroup].push(...currentEntry); } - groups[culturalGroup].push(line); + + // Reset for the next entry + isInEntry = false; + currentEntry = []; + currentCulturalGroup = null; } - } else if (currentExtinf && !line.startsWith('#')) { - const culturalGroup = getCulturalGroup(currentExtinf); - // Only add the URL line if the channel belonged to a group - if (culturalGroup) { - groups[culturalGroup].push(line); - } - currentExtinf = null; } - }); + } // Get list of current cultural group files const currentGroupFiles = Object.keys(groups).map(groupTitle => @@ -179,9 +190,9 @@ function splitByCulturalGroup(filePath) { console.log(`Created/updated cultural group playlist: ${groupFilePath}`); }); - // Generate summary + // Generate summary - count entries properly by counting #EXTINF lines const summary = Object.entries(groups).map(([group, lines]) => { - const channelCount = (lines.length - 1) / 2; // Subtract header and divide by 2 (EXTINF + URL) + const channelCount = lines.filter(line => line.startsWith('#EXTINF')).length; return `${group}: ${channelCount} channels`; });