forked from Mystique-Play/Mystique
M3U order sorting & house keeping
This commit is contained in:
parent
026b25aa2d
commit
52da7a17be
3 changed files with 80 additions and 3 deletions
67
.forgejo/scripts/sort-m3u.js
Normal file
67
.forgejo/scripts/sort-m3u.js
Normal file
|
@ -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);
|
||||
}
|
|
@ -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
|
||||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue