From 223e67123bf27bbcf08564a748e2811ee44dc664 Mon Sep 17 00:00:00 2001 From: SpaceMonkey <> Date: Wed, 29 Jan 2025 22:59:35 +0000 Subject: [PATCH] Country specific playlists --- .forgejo/scripts/split-m3u.js | 75 ++++++++++++++++++++++++++++++++ .forgejo/workflows/validate.yaml | 15 ++++++- 2 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 .forgejo/scripts/split-m3u.js diff --git a/.forgejo/scripts/split-m3u.js b/.forgejo/scripts/split-m3u.js new file mode 100644 index 0000000..d691837 --- /dev/null +++ b/.forgejo/scripts/split-m3u.js @@ -0,0 +1,75 @@ +const fs = require('fs'); +const path = require('path'); + +function splitByGroup(filePath) { + const content = fs.readFileSync(filePath, 'utf8'); + const lines = content.split('\n'); + + // Create groups directory if it doesn't exist + const groupsDir = path.join(path.dirname(filePath), 'countries'); + if (!fs.existsSync(groupsDir)) { + fs.mkdirSync(groupsDir); + } + + const groups = {}; + let currentExtinf = null; + + // First line should be #EXTM3U + const header = lines[0]; + if (!header.startsWith('#EXTM3U')) { + throw new Error('Invalid M3U file: Missing #EXTM3U header'); + } + + // Process each line + lines.forEach(line => { + line = line.trim(); + if (!line) return; + + if (line.startsWith('#EXTINF')) { + currentExtinf = line; + const groupMatch = line.match(/group-title="([^"]*)"/); + const groupTitle = groupMatch ? groupMatch[1] : 'Unknown'; + + if (!groups[groupTitle]) { + groups[groupTitle] = ['#EXTM3U']; + } + 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; + } + }); + + // Write each group to a separate file + Object.entries(groups).forEach(([groupTitle, groupLines]) => { + const safeGroupTitle = groupTitle.replace(/[^a-z0-9]/gi, '_').toLowerCase(); + const groupFilePath = path.join(groupsDir, `${safeGroupTitle}.m3u`); + fs.writeFileSync(groupFilePath, groupLines.join('\n') + '\n'); + console.log(`Created group playlist: ${groupFilePath}`); + }); + + // Create a summary of the split + const summary = Object.entries(groups).map(([group, lines]) => { + const channelCount = (lines.length - 1) / 2; // Subtract header and divide by 2 (EXTINF + URL) + return `${group}: ${channelCount} channels`; + }); + + console.log('\nPlaylist split summary:'); + console.log(summary.join('\n')); +} + +const filePath = process.argv[2]; +if (!filePath) { + console.error('Please provide the path to mystique.m3u'); + process.exit(1); +} + +try { + splitByGroup(filePath); +} catch (error) { + console.error('Error splitting mystique.m3u:', error.message); + process.exit(1); +} \ No newline at end of file diff --git a/.forgejo/workflows/validate.yaml b/.forgejo/workflows/validate.yaml index 8792c01..0014c8d 100644 --- a/.forgejo/workflows/validate.yaml +++ b/.forgejo/workflows/validate.yaml @@ -28,10 +28,21 @@ jobs: - name: Run M3U linter run: m3u-linter --config .forgejo/scripts/m3u-linter.config.json mystique.m3u + + - name: Split into group playlists + run: node .forgejo/scripts/split-m3u.js mystique.m3u - - name: Commit changes if file was modified + - name: Commit changes to main playlist file run: | git config --global user.name 'Forgejo Actions Bot' git config --global user.email 'forgejo-actions[bot]@noreply.forgejo.org' git add mystique.m3u - git diff --quiet && git diff --staged --quiet || (git commit -m "Auto-format and sort M3U file" && git push) + git diff --quiet && git diff --staged --quiet || (git commit -m "Auto-format and sort M3U file") + + - name: Commit changes to specific country files + run: | + git config --global user.name 'Forgejo Actions Bot' + git config --global user.email 'forgejo-actions[bot]@noreply.forgejo.org' + git add countries + git diff --quiet && git diff --staged --quiet || (git commit -m "Update of country specific playlists" && git push) +