2 Commits

Author SHA1 Message Date
8c010665e1 Selectp age 2024-09-13 22:09:00 +02:00
916d3004de Episode Selector 2024-09-13 22:00:44 +02:00
5 changed files with 221 additions and 37 deletions

View File

@@ -2,7 +2,6 @@ package main
import ( import (
"encoding/base64" "encoding/base64"
"encoding/json"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@@ -12,38 +11,6 @@ import (
"strings" "strings"
) )
func processInputFile(inputFile string) error {
jsonFile, err := os.Open(inputFile)
if err != nil {
return fmt.Errorf("error opening file %s: %v", inputFile, err)
}
defer jsonFile.Close()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
return fmt.Errorf("error reading file %s: %v", inputFile, err)
}
byteValue = removeBOM(byteValue)
var items Items
err = json.Unmarshal(byteValue, &items)
if err != nil {
return fmt.Errorf("error unmarshaling JSON: %v", err)
}
for i, item := range items.Items {
updateProgress(filepath.Base(inputFile), float64(i)/float64(len(items.Items))*100, item.Filename)
err := downloadFile(item)
if err != nil {
fmt.Printf("Error downloading file: %v\n", err)
}
}
updateProgress(filepath.Base(inputFile), 100, "")
return nil
}
func removeBOM(input []byte) []byte { func removeBOM(input []byte) []byte {
if len(input) >= 3 && input[0] == 0xEF && input[1] == 0xBB && input[2] == 0xBF { if len(input) >= 3 && input[0] == 0xEF && input[1] == 0xBB && input[2] == 0xBF {
return input[3:] return input[3:]

View File

@@ -51,16 +51,59 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
tempFilename := filepath.Base(tempFile.Name()) tempFilename := filepath.Base(tempFile.Name())
_, err = parseInputFile(tempFile.Name())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/select?filename="+tempFilename, http.StatusSeeOther)
}
func handleSelect(w http.ResponseWriter, r *http.Request) {
filename := r.URL.Query().Get("filename")
items, err := parseInputFile(filepath.Join(uploadDir, filename))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
groupedItems := groupItemsBySeason(items)
err = templates.ExecuteTemplate(w, "select", struct {
Filename string
Items map[string][]Item
}{
Filename: filename,
Items: groupedItems,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func handleProcess(w http.ResponseWriter, r *http.Request) {
filename := r.FormValue("filename")
selectedItems := r.Form["items"]
items, err := parseInputFile(filepath.Join(uploadDir, filename))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
filteredItems := filterSelectedItems(items, selectedItems)
go func() { go func() {
err := processInputFile(tempFile.Name()) err := processItems(filename, filteredItems)
if err != nil { if err != nil {
fmt.Printf("Error processing file: %v\n", err) fmt.Printf("Error processing file: %v\n", err)
} }
os.Remove(tempFile.Name()) os.Remove(filepath.Join(uploadDir, filename))
}() }()
http.Redirect(w, r, "/progress?filename="+tempFilename, http.StatusSeeOther) http.Redirect(w, r, "/progress?filename="+filename, http.StatusSeeOther)
} }
func handleProgress(w http.ResponseWriter, r *http.Request) { func handleProgress(w http.ResponseWriter, r *http.Request) {

View File

@@ -63,13 +63,20 @@ func main() {
if *inputFile == "" { if *inputFile == "" {
startWebServer() startWebServer()
} else { } else {
processInputFile(*inputFile) items, err := parseInputFile(*inputFile)
if err != nil {
fmt.Printf("Error parsing input file: %v\n", err)
return
}
processItems(*inputFile, items)
} }
} }
func startWebServer() { func startWebServer() {
http.HandleFunc("/", handleRoot) http.HandleFunc("/", handleRoot)
http.HandleFunc("/upload", handleUpload) http.HandleFunc("/upload", handleUpload)
http.HandleFunc("/select", handleSelect)
http.HandleFunc("/process", handleProcess)
http.HandleFunc("/progress", handleProgress) http.HandleFunc("/progress", handleProgress)
fmt.Println("Starting web server on http://0.0.0.0:8080") fmt.Println("Starting web server on http://0.0.0.0:8080")

102
templates/select Normal file
View File

@@ -0,0 +1,102 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Select Items to Download</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
background-color: #1e1e1e;
color: #d4d4d4;
line-height: 1.6;
padding: 20px;
max-width: 800px;
margin: 0 auto;
box-sizing: border-box;
}
h1 {
border-bottom: 1px solid #333;
padding-bottom: 10px;
}
.season {
margin-bottom: 20px;
background-color: #2d2d2d;
padding: 10px;
border-radius: 4px;
}
.season-title {
font-size: 1.2em;
font-weight: bold;
margin-bottom: 10px;
display: flex;
align-items: center;
}
.season-checkbox {
margin-right: 10px;
}
.item {
margin-left: 20px;
}
button, input[type="submit"] {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 15px;
margin: 5px;
border-radius: 4px;
cursor: pointer;
}
button:hover, input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>Select Items to Download</h1>
<form action="/process" method="post">
<input type="hidden" name="filename" value="{{.Filename}}">
{{range $season, $items := .Items}}
<div class="season">
<div class="season-title">
<input type="checkbox" class="season-checkbox" id="season-{{$season}}" checked onchange="toggleSeason('{{$season}}')">
<label for="season-{{$season}}">{{$season}}</label>
</div>
{{range $item := $items}}
<div class="item">
<label>
<input type="checkbox" name="items" value="{{$item.Filename}}" checked class="episode-{{$season}}">
{{$item.Filename}}
</label>
</div>
{{end}}
</div>
{{end}}
<div>
<button type="button" onclick="selectAll(true)">Select All</button>
<button type="button" onclick="selectAll(false)">Select None</button>
<input type="submit" value="Start Download">
</div>
</form>
<script>
function selectAll(checked) {
var checkboxes = document.getElementsByName('items');
for (var i = 0; i < checkboxes.length; i++) {
checkboxes[i].checked = checked;
}
var seasonCheckboxes = document.getElementsByClassName('season-checkbox');
for (var i = 0; i < seasonCheckboxes.length; i++) {
seasonCheckboxes[i].checked = checked;
}
}
function toggleSeason(season) {
var seasonCheckbox = document.getElementById('season-' + season);
var episodeCheckboxes = document.getElementsByClassName('episode-' + season);
for (var i = 0; i < episodeCheckboxes.length; i++) {
episodeCheckboxes[i].checked = seasonCheckbox.checked;
}
}
</script>
</body>
</html>

View File

@@ -1,8 +1,11 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"io"
"net/url" "net/url"
"os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@@ -118,3 +121,65 @@ func findOrCreateSegmentTimeline(adaptationSet *etree.Element) *etree.Element {
segmentTemplate := representation.CreateElement("SegmentTemplate") segmentTemplate := representation.CreateElement("SegmentTemplate")
return segmentTemplate.CreateElement("SegmentTimeline") return segmentTemplate.CreateElement("SegmentTimeline")
} }
func parseInputFile(inputFile string) ([]Item, error) {
jsonFile, err := os.Open(inputFile)
if err != nil {
return nil, fmt.Errorf("error opening file %s: %v", inputFile, err)
}
defer jsonFile.Close()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
return nil, fmt.Errorf("error reading file %s: %v", inputFile, err)
}
byteValue = removeBOM(byteValue)
var items Items
err = json.Unmarshal(byteValue, &items)
if err != nil {
return nil, fmt.Errorf("error unmarshaling JSON: %v", err)
}
return items.Items, nil
}
func groupItemsBySeason(items []Item) map[string][]Item {
grouped := make(map[string][]Item)
for _, item := range items {
metadata := parseMetadata(item.Metadata)
if metadata.Type == "serie" {
key := fmt.Sprintf("%s - %s", metadata.Title, metadata.Season)
grouped[key] = append(grouped[key], item)
} else {
grouped["Movies"] = append(grouped["Movies"], item)
}
}
return grouped
}
func filterSelectedItems(items []Item, selectedItems []string) []Item {
var filtered []Item
for _, item := range items {
for _, selected := range selectedItems {
if item.Filename == selected {
filtered = append(filtered, item)
break
}
}
}
return filtered
}
func processItems(filename string, items []Item) error {
for i, item := range items {
updateProgress(filename, float64(i)/float64(len(items))*100, item.Filename)
err := downloadFile(item)
if err != nil {
fmt.Printf("Error downloading file: %v\n", err)
}
}
updateProgress(filename, 100, "")
return nil
}