From 69e15a2d209fb542edd41f0b9bad6e8f76b990e2 Mon Sep 17 00:00:00 2001 From: SpaceMonkey Date: Sun, 2 Mar 2025 19:11:58 -0300 Subject: [PATCH] Update .forgejo/scripts/cleanup-m3u.js --- .forgejo/scripts/cleanup-m3u.js | 79 ++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/.forgejo/scripts/cleanup-m3u.js b/.forgejo/scripts/cleanup-m3u.js index a5c4fce..e3023ba 100644 --- a/.forgejo/scripts/cleanup-m3u.js +++ b/.forgejo/scripts/cleanup-m3u.js @@ -1,50 +1,55 @@ const fs = require('fs'); function cleanM3u(filePath) { - let content = fs.readFileSync(filePath, 'utf8'); + let content = fs.readFileSync(filePath, 'utf8'); + + // Split into lines and process each line + content = content.split('\n') + .map(line => { + // Trim any whitespace + line = line.trim(); + + if (line.startsWith('#EXTINF')) { + // Replace multiple spaces with single space + line = line.replace(/\s+/g, ' '); + + // Instead of splitting by comma, find the position of the first comma + // This preserves the channel name and any commas it might contain + const firstCommaPos = line.indexOf(','); + + if (firstCommaPos !== -1) { + const extinf = line.substring(0, firstCommaPos); + const title = line.substring(firstCommaPos + 1).trim(); // Trim space after comma + + return `${extinf},${title}`; + } + } else { + // For non-EXTINF lines, just replace multiple spaces with single space + line = line.replace(/\s+/g, ' '); + } + + return line; + }) + .filter(line => line) // Remove empty lines + .join('\n'); - // Split into lines and process each line - content = content.split('\n') - .map(line => { - // Trim any whitespace - line = line.trim(); - - if (line.startsWith('#EXTINF')) { - // Replace multiple spaces with single space - line = line.replace(/\s+/g, ' '); - - // Remove space before the title (after the comma) - line = line.replace(/,\s+/, ','); - - // Remove any extra commas in the line - const [extinf, title] = line.split(','); - return `${extinf},${title}`; - } else { - // For non-EXTINF lines, just replace multiple spaces with single space - line = line.replace(/\s+/g, ' '); - } - - return line; - }) - .filter(line => line) // Remove empty lines - .join('\n'); - - // Ensure single newline at end of file - content = content.trim() + '\n'; - - fs.writeFileSync(filePath, content); - console.log('Basic cleanup of mystique.m3u completed'); + // Ensure single newline at end of file + content = content.trim() + '\n'; + + fs.writeFileSync(filePath, content); + console.log('Basic cleanup of mystique.m3u completed'); } const filePath = process.argv[2]; + if (!filePath) { - console.error('Please provide the path to mystique.m3u'); - process.exit(1); + console.error('Please provide the path to mystique.m3u'); + process.exit(1); } try { - cleanM3u(filePath); + cleanM3u(filePath); } catch (error) { - console.error('Error cleaning mystique.m3u:', error.message); - process.exit(1); + console.error('Error cleaning mystique.m3u:', error.message); + process.exit(1); } \ No newline at end of file