diff --git a/m3u-linter.config.json b/.forgejo/scripts/m3u-linter.config.json similarity index 100% rename from m3u-linter.config.json rename to .forgejo/scripts/m3u-linter.config.json diff --git a/.forgejo/scripts/sort-m3u.js b/.forgejo/scripts/sort-m3u.js new file mode 100644 index 0000000..70d0eef --- /dev/null +++ b/.forgejo/scripts/sort-m3u.js @@ -0,0 +1,67 @@ +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); +} diff --git a/.forgejo/workflows/validate.yaml b/.forgejo/workflows/validate.yaml index 600d83d..44a1a7d 100644 --- a/.forgejo/workflows/validate.yaml +++ b/.forgejo/workflows/validate.yaml @@ -1,4 +1,4 @@ -name: M3U Lint +name: M3U Lint and Sort on: push: @@ -6,7 +6,7 @@ on: - 'mystique.m3u' jobs: - lint: + lint-and-sort: runs-on: docker steps: @@ -20,5 +20,15 @@ jobs: - name: Install m3u-linter run: npm install -g m3u-linter + - name: Sort M3U file + run: node .forgejo/scripts/sort-m3u.js mystique.m3u + - name: Run M3U linter - run: m3u-linter mystique.m3u \ No newline at end of file + run: m3u-linter --config .forgejo/scripts/m3u-linter.config.json mystique.m3u + + - name: Commit changes if file was modified + 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 "Sort M3U by group-title" && git push)