abortPause #1

Merged
Joren merged 7 commits from abortPause into main 2024-09-13 22:59:31 +02:00
4 changed files with 119 additions and 37 deletions
Showing only changes of commit 916d3004de - Show all commits

View File

@ -2,7 +2,6 @@ package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
@ -12,38 +11,6 @@ import (
"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 {
if len(input) >= 3 && input[0] == 0xEF && input[1] == 0xBB && input[2] == 0xBF {
return input[3:]

View File

@ -51,16 +51,59 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
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() {
err := processInputFile(tempFile.Name())
err := processItems(filename, filteredItems)
if err != nil {
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) {

View File

@ -63,13 +63,20 @@ func main() {
if *inputFile == "" {
startWebServer()
} 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() {
http.HandleFunc("/", handleRoot)
http.HandleFunc("/upload", handleUpload)
http.HandleFunc("/select", handleSelect)
http.HandleFunc("/process", handleProcess)
http.HandleFunc("/progress", handleProgress)
fmt.Println("Starting web server on http://0.0.0.0:8080")

View File

@ -1,8 +1,11 @@
package main
import (
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"regexp"
"strconv"
"strings"
@ -118,3 +121,65 @@ func findOrCreateSegmentTimeline(adaptationSet *etree.Element) *etree.Element {
segmentTemplate := representation.CreateElement("SegmentTemplate")
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
}