MystiquePlus/.forgejo/scripts/sort-m3u.js
2025-01-28 22:50:37 +00:00

67 lines
1.8 KiB
JavaScript

const fs = require('fs');
function sortM3uByGroupTitle(filePath) {
// Read the file
const content = fs.readFileSync(filePath, 'utf8');
const lines = content.split('\n');
// First line should be #EXTM3U
const header = lines[0];
if (!header.startsWith('#EXTM3U')) {
throw new Error('Invalid M3U file: Missing #EXTM3U header');
}
// Group the entries
const entries = [];
let currentEntry = [];
for (let i = 1; i < lines.length; i++) {
const line = lines[i].trim();
if (!line) continue;
if (line.startsWith('#EXTINF')) {
if (currentEntry.length === 2) {
entries.push(currentEntry);
}
currentEntry = [line];
} else if (currentEntry.length === 1) {
currentEntry.push(line);
}
}
// Add the last entry if exists
if (currentEntry.length === 2) {
entries.push(currentEntry);
}
// Sort entries by group-title
entries.sort((a, b) => {
const groupTitleA = (a[0].match(/group-title="([^"]*)"/) || [])[1] || '';
const groupTitleB = (b[0].match(/group-title="([^"]*)"/) || [])[1] || '';
return groupTitleA.localeCompare(groupTitleB);
});
// Rebuild the file content
const sortedContent = [
header,
...entries.flatMap(entry => entry)
].join('\n');
// Write back to file
fs.writeFileSync(filePath, sortedContent);
console.log('M3U file has been sorted by group-title');
}
// Get file path from command line argument
const filePath = process.argv[2];
if (!filePath) {
console.error('Please provide a file path');
process.exit(1);
}
try {
sortM3uByGroupTitle(filePath);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}