forked from Mystique-Play/Mystique
no message
This commit is contained in:
parent
f236e80fd6
commit
e07e71d324
2 changed files with 160 additions and 131 deletions
|
@ -17,7 +17,6 @@ function splitByGroup(filePath) {
|
||||||
.map(file => file.toLowerCase());
|
.map(file => file.toLowerCase());
|
||||||
|
|
||||||
const groups = {};
|
const groups = {};
|
||||||
let currentExtinf = null;
|
|
||||||
|
|
||||||
// First line should be #EXTM3U
|
// First line should be #EXTM3U
|
||||||
const header = lines[0];
|
const header = lines[0];
|
||||||
|
@ -25,28 +24,43 @@ function splitByGroup(filePath) {
|
||||||
throw new Error('Invalid M3U file: Missing #EXTM3U header');
|
throw new Error('Invalid M3U file: Missing #EXTM3U header');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process each line
|
// Process the file line by line to handle multi-line entries
|
||||||
lines.forEach(line => {
|
let currentGroupTitle = null;
|
||||||
line = line.trim();
|
let currentEntry = [];
|
||||||
if (!line) return;
|
let isInEntry = false;
|
||||||
|
|
||||||
|
for (let i = 1; i < lines.length; i++) {
|
||||||
|
const line = lines[i].trim();
|
||||||
|
if (!line) continue;
|
||||||
|
|
||||||
if (line.startsWith('#EXTINF')) {
|
if (line.startsWith('#EXTINF')) {
|
||||||
currentExtinf = line;
|
// Start of a new entry
|
||||||
const groupMatch = line.match(/group-title="([^"]*)"/);
|
isInEntry = true;
|
||||||
const groupTitle = groupMatch ? groupMatch[1] : 'Unknown';
|
currentEntry = [line];
|
||||||
|
|
||||||
if (!groups[groupTitle]) {
|
// Extract the group title
|
||||||
groups[groupTitle] = ['#EXTM3U'];
|
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
|
// Get list of current group files that should exist
|
||||||
const currentGroupFiles = Object.keys(groups).map(groupTitle =>
|
const currentGroupFiles = Object.keys(groups).map(groupTitle =>
|
||||||
|
@ -71,8 +85,9 @@ function splitByGroup(filePath) {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create a summary of the split
|
// Create a summary of the split
|
||||||
|
// Count entries properly by counting #EXTINF lines
|
||||||
const summary = Object.entries(groups).map(([group, 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`;
|
return `${group}: ${channelCount} channels`;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -20,17 +20,31 @@ function sortM3uByGroupTitle(filePath) {
|
||||||
if (!line) continue;
|
if (!line) continue;
|
||||||
|
|
||||||
if (line.startsWith('#EXTINF')) {
|
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);
|
entries.push(currentEntry);
|
||||||
|
currentEntry = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start a new entry
|
||||||
currentEntry = [line];
|
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);
|
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
|
// Add the last entry if complete
|
||||||
if (currentEntry.length === 2) {
|
if (currentEntry.length > 0 && !currentEntry[currentEntry.length - 1].startsWith('#')) {
|
||||||
entries.push(currentEntry);
|
entries.push(currentEntry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue