no message

This commit is contained in:
SpaceMonkey 2025-03-01 23:46:36 +00:00
parent f236e80fd6
commit e07e71d324
2 changed files with 160 additions and 131 deletions

View file

@ -17,7 +17,6 @@ function splitByGroup(filePath) {
.map(file => file.toLowerCase());
const groups = {};
let currentExtinf = null;
// First line should be #EXTM3U
const header = lines[0];
@ -25,28 +24,43 @@ function splitByGroup(filePath) {
throw new Error('Invalid M3U file: Missing #EXTM3U header');
}
// Process each line
lines.forEach(line => {
line = line.trim();
if (!line) return;
// Process the file line by line to handle multi-line entries
let currentGroupTitle = 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 groupMatch = line.match(/group-title="([^"]*)"/);
const groupTitle = groupMatch ? groupMatch[1] : 'Unknown';
// Start of a new entry
isInEntry = true;
currentEntry = [line];
if (!groups[groupTitle]) {
groups[groupTitle] = ['#EXTM3U'];
// Extract the group title
const groupMatch = line.match(/group-title="([^"]*)"/);
currentGroupTitle = groupMatch ? groupMatch[1] : 'Unknown';
// Initialize the group if it doesn't exist
if (!groups[currentGroupTitle]) {
groups[currentGroupTitle] = ['#EXTM3U'];
}
} 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 group
groups[currentGroupTitle].push(...currentEntry);
// Reset for the next entry
isInEntry = false;
currentEntry = [];
}
}
groups[groupTitle].push(line);
} else if (currentExtinf && !line.startsWith('#')) {
// This is a URL line
const groupMatch = currentExtinf.match(/group-title="([^"]*)"/);
const groupTitle = groupMatch ? groupMatch[1] : 'Unknown';
groups[groupTitle].push(line);
currentExtinf = null;
}
});
// Get list of current group files that should exist
const currentGroupFiles = Object.keys(groups).map(groupTitle =>
@ -71,8 +85,9 @@ function splitByGroup(filePath) {
});
// Create a summary of the split
// 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`;
});

View file

@ -20,17 +20,31 @@ function sortM3uByGroupTitle(filePath) {
if (!line) continue;
if (line.startsWith('#EXTINF')) {
if (currentEntry.length === 2) {
// If we have a previous entry that's complete (has a URL), add it
if (currentEntry.length > 0 && !currentEntry[currentEntry.length - 1].startsWith('#')) {
entries.push(currentEntry);
currentEntry = [];
}
// Start a new entry
currentEntry = [line];
} else if (currentEntry.length === 1) {
} else if (line.startsWith('#')) {
// This is another directive line (like #EXTVLCOPT), add it to the current entry
if (currentEntry.length > 0) {
currentEntry.push(line);
}
} else {
// This is a URL line, add it to complete the current entry
if (currentEntry.length > 0) {
currentEntry.push(line);
entries.push(currentEntry);
currentEntry = [];
}
}
}
// Add the last entry if exists
if (currentEntry.length === 2) {
// Add the last entry if complete
if (currentEntry.length > 0 && !currentEntry[currentEntry.length - 1].startsWith('#')) {
entries.push(currentEntry);
}