Sort episodes in a holy way

This commit is contained in:
Joren 2024-09-15 04:29:48 +02:00
parent bd87baa40a
commit 72889d3083
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55
3 changed files with 61 additions and 4 deletions

View File

@ -132,6 +132,8 @@ func handleSelect(w http.ResponseWriter, r *http.Request) {
continue
}
sortItems(items)
groupedItems := groupItemsBySeason(items)
allItems[filename] = groupedItems
}
@ -184,6 +186,7 @@ func handleProcess(w http.ResponseWriter, r *http.Request) {
}
selectedItems := filterSelectedItems(allItems, items)
sortItems(selectedItems)
go processItems(filename, selectedItems)
}

View File

@ -50,6 +50,18 @@
button:hover, input[type="submit"]:hover {
background-color: #45a049;
}
#fix-order-button {
background-color: #2196F3;
color: white;
border: none;
padding: 10px 15px;
margin: 5px;
border-radius: 4px;
cursor: pointer;
}
#fix-order-button:hover {
background-color: #1976D2;
}
</style>
</head>
<body>
@ -59,11 +71,12 @@
{{range $filename, $fileItems := .AllItems}}
<h2>{{$filename}}</h2>
{{range $season, $items := $fileItems}}
<div class="season">
<div class="season" id="season-{{$filename}}-{{$season}}">
<div class="season-title">
<input type="checkbox" class="season-checkbox" id="season-{{$filename}}-{{$season}}" checked onchange="toggleSeason('{{$filename}}-{{$season}}')">
<label for="season-{{$filename}}-{{$season}}">{{$season}}</label>
<input type="checkbox" class="season-checkbox" id="season-checkbox-{{$filename}}-{{$season}}" checked onchange="toggleSeason('{{$filename}}-{{$season}}')">
<label for="season-checkbox-{{$filename}}-{{$season}}">{{$season}}</label>
</div>
<div class="season-items">
{{range $item := $items}}
<div class="item">
<label>
@ -73,6 +86,7 @@
</div>
{{end}}
</div>
</div>
{{end}}
{{end}}
<div>
@ -94,7 +108,7 @@
}
function toggleSeason(season) {
var seasonCheckbox = document.getElementById('season-' + season);
var seasonCheckbox = document.getElementById('season-checkbox-' + season);
var episodeCheckboxes = document.getElementsByClassName('episode-' + season);
for (var i = 0; i < episodeCheckboxes.length; i++) {
episodeCheckboxes[i].checked = seasonCheckbox.checked;

View File

@ -9,6 +9,7 @@ import (
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"sync"
@ -196,6 +197,43 @@ func filterSelectedItems(items []Item, selectedItems []string) []Item {
return filtered
}
func sortItems(items []Item) {
sort.Slice(items, func(i, j int) bool {
iMeta := parseMetadata(items[i].Metadata)
jMeta := parseMetadata(items[j].Metadata)
if iMeta.Title != jMeta.Title {
return iMeta.Title < jMeta.Title
}
iSeason := extractNumber(iMeta.Season)
jSeason := extractNumber(jMeta.Season)
if iSeason != jSeason {
return iSeason < jSeason
}
iEpisode := extractEpisodeNumber(items[i].Filename)
jEpisode := extractEpisodeNumber(items[j].Filename)
return iEpisode < jEpisode
})
}
func extractNumber(s string) int {
num, _ := strconv.Atoi(strings.TrimLeft(s, "S"))
return num
}
func extractEpisodeNumber(filename string) int {
parts := strings.Split(filename, "E")
if len(parts) > 1 {
num, _ := strconv.Atoi(parts[1])
return num
}
return 0
}
func processItems(filename string, items []Item) error {
jobsMutex.Lock()
jobInfo := &JobInfo{
@ -215,6 +253,8 @@ func processItems(filename string, items []Item) error {
}
}()
sortItems(items)
for i := 0; i < len(items); i++ {
select {
case <-jobInfo.AbortChan: