// Copyright 2024 Brian Newman. All rights reserved. package handlers import ( "fmt" "strconv" "strings" "cdmnky.dev/media/patreon-dl/gui/handlers/mods" "cdmnky.dev/media/patreon-dl/gui/local.pkg/src/config" "github.com/roblillack/spot" "github.com/roblillack/spot/ui" ) // NewServer ... func NewServer(config *config.Config) *Server { return &Server{ config: config, } } // Server ... type Server struct { config *config.Config } // Start ... func (x *Server) Start() { ui.Init() height := 260 width := 600 profileIdx := 0 spot.MountFn(func(ctx *spot.RenderContext) spot.Component { hints := map[string]string{} profiles := []string{} for _, t := range x.config.Templates { profiles = append(profiles, t.Mask) hints[t.Mask] = t.Hint } filename, setFilename := spot.UseState[string](ctx, "") lblFilename := &ui.Label{ X: 5, Y: 213, Width: width - 10, Height: 20, Value: filename, } status, showStatus := spot.UseState[string](ctx, fmt.Sprintf("Hint: %s", hints[profiles[profileIdx]])) lblStatus := &ui.Label{ X: 5, Y: 235, Width: width - 10, Height: 20, Value: status, } cmbProfile := &ui.Dropdown{ X: 5, Y: 5, Width: width - 10, Height: 20, Items: profiles, Editable: false, SelectedIndex: profileIdx, OnSelectionDidChange: func(idx int) { profileIdx = idx showStatus(fmt.Sprintf("Hint: %s", hints[profiles[idx]])) }, } lblVideoURL := &ui.Label{ X: 5, Y: 30, Width: width - 10, Height: 20, Value: "Video URL:", } videoURL := "" videoURL, setVideoURL := spot.UseState[string](ctx, "") txtVideeoURL := &ui.TextField{ X: 5, Y: 50, Width: width - 10, Height: 20, Value: videoURL, OnChange: func(value string) { setVideoURL(value) }, } lblVideoTitle := &ui.Label{ X: 5, Y: 80, Width: width - 10, Height: 20, Value: "Video Title:", } videoTitle, setVideoTitle := spot.UseState[string](ctx, "") txtVideeoTitle := &ui.TextField{ X: 5, Y: 100, Width: width - 10, Height: 20, Value: videoTitle, OnChange: func(value string) { setVideoTitle(value) }, } lblThreshhold := &ui.Label{ X: 5, Y: 130, Width: width - 10, Height: 20, Value: "Size in MB to trigger re-encoding (0 for none)", } thresshold, setThresshold := spot.UseState[string](ctx, x.config.Thresshold) txtThresshold := &ui.TextField{ X: 5, Y: 150, Width: width - 10, Height: 20, Value: thresshold, OnChange: func(value string) { setThresshold(value) }, } btnProcess := &ui.Button{ X: 5, Y: 180, Width: width - 10, Height: 25, Title: "Process Download", OnClick: func() { mask := profiles[profileIdx] title := videoTitle url := videoURL threshold, err := strconv.ParseInt(thresshold, 0, 64) if err != nil { showStatus(mods.Format("Error: %v", err)) return } threshold = threshold * (1000 * 1000) mask = strings.Replace(mask, "{title}", title, -1) setFilename(mods.Format("Output file: %s", mask)) //if strings.Contains(url, "akamaized.net") { if strings.Contains(url, "vimeocdn.com") { go mods.ProcVimeo(showStatus, x.config, url, mask, threshold) } else { go mods.ProcPatreon(showStatus, x.config, url, mask, threshold) } }, } return &ui.Window{ Title: x.config.Appname, Width: width, Height: height, Children: []spot.Component{ cmbProfile, lblVideoURL, txtVideeoURL, lblVideoTitle, txtVideeoTitle, lblThreshhold, txtThresshold, btnProcess, lblFilename, lblStatus, }, } }) ui.Run() }