Add support for multiline streams

This commit is contained in:
SpaceMonkey 2025-03-01 20:51:53 -03:00
parent 37abcc31ec
commit 5d24fc69fc

View file

@ -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[culturalGroup].push(line);
groups[currentCulturalGroup].push(...currentEntry);
}
// 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`;
});