Update .forgejo/scripts/cleanup-m3u.js

This commit is contained in:
SpaceMonkey 2025-03-02 19:11:58 -03:00
parent f0244a685a
commit 69e15a2d20

View file

@ -1,50 +1,55 @@
const fs = require('fs'); const fs = require('fs');
function cleanM3u(filePath) { function cleanM3u(filePath) {
let content = fs.readFileSync(filePath, 'utf8'); let content = fs.readFileSync(filePath, 'utf8');
// Split into lines and process each line // Split into lines and process each line
content = content.split('\n') content = content.split('\n')
.map(line => { .map(line => {
// Trim any whitespace // Trim any whitespace
line = line.trim(); line = line.trim();
if (line.startsWith('#EXTINF')) { if (line.startsWith('#EXTINF')) {
// Replace multiple spaces with single space // Replace multiple spaces with single space
line = line.replace(/\s+/g, ' '); line = line.replace(/\s+/g, ' ');
// Remove space before the title (after the comma) // Instead of splitting by comma, find the position of the first comma
line = line.replace(/,\s+/, ','); // This preserves the channel name and any commas it might contain
const firstCommaPos = line.indexOf(',');
// Remove any extra commas in the line if (firstCommaPos !== -1) {
const [extinf, title] = line.split(','); const extinf = line.substring(0, firstCommaPos);
return `${extinf},${title}`; const title = line.substring(firstCommaPos + 1).trim(); // Trim space after comma
} else {
// For non-EXTINF lines, just replace multiple spaces with single space
line = line.replace(/\s+/g, ' ');
}
return line; return `${extinf},${title}`;
}) }
.filter(line => line) // Remove empty lines } else {
.join('\n'); // For non-EXTINF lines, just replace multiple spaces with single space
line = line.replace(/\s+/g, ' ');
}
// Ensure single newline at end of file return line;
content = content.trim() + '\n'; })
.filter(line => line) // Remove empty lines
.join('\n');
fs.writeFileSync(filePath, content); // Ensure single newline at end of file
console.log('Basic cleanup of mystique.m3u completed'); content = content.trim() + '\n';
fs.writeFileSync(filePath, content);
console.log('Basic cleanup of mystique.m3u completed');
} }
const filePath = process.argv[2]; const filePath = process.argv[2];
if (!filePath) { if (!filePath) {
console.error('Please provide the path to mystique.m3u'); console.error('Please provide the path to mystique.m3u');
process.exit(1); process.exit(1);
} }
try { try {
cleanM3u(filePath); cleanM3u(filePath);
} catch (error) { } catch (error) {
console.error('Error cleaning mystique.m3u:', error.message); console.error('Error cleaning mystique.m3u:', error.message);
process.exit(1); process.exit(1);
} }