From 133682c6b09ad22c3daf6b21fc5f7cbf29a86682 Mon Sep 17 00:00:00 2001 From: SpaceMonkey <> Date: Thu, 30 Jan 2025 22:47:58 +0000 Subject: [PATCH] Workflow improvements --- .forgejo/scripts/readme-m3u.js | 59 ++++++++++++++++++++++++++++++++ .forgejo/scripts/split-m3u.js | 21 +++++++++++- .forgejo/workflows/validate.yaml | 6 +++- 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 .forgejo/scripts/readme-m3u.js diff --git a/.forgejo/scripts/readme-m3u.js b/.forgejo/scripts/readme-m3u.js new file mode 100644 index 0000000..bc60b33 --- /dev/null +++ b/.forgejo/scripts/readme-m3u.js @@ -0,0 +1,59 @@ +const fs = require('fs'); +const path = require('path'); + +function updateReadme(m3uPath) { + // Get list of group files + const groupsDir = path.join(path.dirname(m3uPath), 'countries'); + const groups = {}; + + // Read the main M3U to get original group names and channel counts + const content = fs.readFileSync(m3uPath, 'utf8'); + const lines = content.split('\n'); + let totalChannels = 0; + + lines.forEach(line => { + if (line.startsWith('#EXTINF')) { + totalChannels++; + const groupMatch = line.match(/group-title="([^"]*)"/); + const groupTitle = groupMatch ? groupMatch[1] : 'Unknown'; + groups[groupTitle] = (groups[groupTitle] || 0) + 1; + } + }); + + // Start building README content + let readmeContent = '# Mystique IPTV\n\n'; + readmeContent += '## Available Playlists\n\n'; + readmeContent += '| Playlist | Channels | Link |\n'; + readmeContent += '|----------|-----------|------|\n'; + + // Add main playlist first + const mainPlaylistName = path.basename(m3uPath); + readmeContent += `| **Complete (All countries)** | ${totalChannels} | [${mainPlaylistName}](${mainPlaylistName}) |\n`; + + // Sort groups alphabetically + const sortedGroups = Object.entries(groups).sort((a, b) => a[0].localeCompare(b[0])); + + // Add each group to the table + sortedGroups.forEach(([groupName, channelCount]) => { + const safeGroupName = groupName.replace(/[^a-z0-9]/gi, '_').toLowerCase(); + readmeContent += `| ${groupName} | ${channelCount} | [${safeGroupName}.m3u](countries/${safeGroupName}.m3u) |\n`; + }); + + const readmePath = path.join(path.dirname(m3uPath), 'README.md'); + + fs.writeFileSync(readmePath, readmeContent); + console.log('README.md has been updated with playlist and group information'); +} + +const filePath = process.argv[2]; +if (!filePath) { + console.error('Please provide the path to mystique.m3u'); + process.exit(1); +} + +try { + updateReadme(filePath); +} catch (error) { + console.error('Error updating README:', error.message); + process.exit(1); +} \ No newline at end of file diff --git a/.forgejo/scripts/split-m3u.js b/.forgejo/scripts/split-m3u.js index d691837..ac60a0e 100644 --- a/.forgejo/scripts/split-m3u.js +++ b/.forgejo/scripts/split-m3u.js @@ -11,6 +11,11 @@ function splitByGroup(filePath) { fs.mkdirSync(groupsDir); } + // Get existing country files + const existingFiles = fs.readdirSync(groupsDir) + .filter(file => file.endsWith('.m3u')) + .map(file => file.toLowerCase()); + const groups = {}; let currentExtinf = null; @@ -43,12 +48,26 @@ function splitByGroup(filePath) { } }); + // Get list of current group files that should exist + const currentGroupFiles = Object.keys(groups).map(groupTitle => + `${groupTitle.replace(/[^a-z0-9]/gi, '_').toLowerCase()}.m3u` + ); + + // Remove files for groups that no longer exist + existingFiles.forEach(existingFile => { + if (!currentGroupFiles.includes(existingFile)) { + const fileToRemove = path.join(groupsDir, existingFile); + fs.unlinkSync(fileToRemove); + console.log(`Removed obsolete group playlist: ${existingFile}`); + } + }); + // 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}`); + console.log(`Created/updated group playlist: ${groupFilePath}`); }); // Create a summary of the split diff --git a/.forgejo/workflows/validate.yaml b/.forgejo/workflows/validate.yaml index 8cebef7..4337c9c 100644 --- a/.forgejo/workflows/validate.yaml +++ b/.forgejo/workflows/validate.yaml @@ -32,10 +32,14 @@ jobs: - name: Split into group playlists run: node .forgejo/scripts/split-m3u.js mystique.m3u + - name: Update README + run: node .forgejo/scripts/readme-m3u.js mystique.m3u + + - name: Commit changes to files run: | git config --global user.name 'Forgejo Actions Bot' git config --global user.email 'forgejo-actions[bot]@noreply.forgejo.org' - git add . + git add --all git diff --quiet && git diff --staged --quiet || (git commit -m "Automated updates of M3U Files" && git push)