From c69654f38fc5b5967a2abedd0f2cac424e364b09 Mon Sep 17 00:00:00 2001 From: balki <3070606-balki@users.noreply.gitlab.com> Date: Tue, 28 Jun 2022 23:02:53 -0400 Subject: [PATCH] pt pubsub working --- cmd/dummytdl/main.go | 32 +++++++++++++++++++++ go.mod | 2 ++ go.sum | 2 ++ main.go | 67 +++++++++++++++++++++++++++++++++++++++++++- pubsub/pt.go | 3 +- templates/index.html | 2 ++ 6 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 cmd/dummytdl/main.go create mode 100644 go.sum diff --git a/cmd/dummytdl/main.go b/cmd/dummytdl/main.go new file mode 100644 index 0000000..ac87d3a --- /dev/null +++ b/cmd/dummytdl/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "flag" + "fmt" + "os" + "time" +) + +func main() { + var op string + var title string + flag.StringVar(&op, "output", "", "path to save file") + flag.StringVar(&title, "get-title", "", "title") + flag.Bool("newline", false, "sdlkfjdslkfj") + flag.Parse() + + if title != "" { + fmt.Println("Dummy title") + return + } + + os.WriteFile(op, []byte("slkfjsdlkfdjkfj"), 0644) + i := 0 + for range time.Tick(1 * time.Second) { + fmt.Printf("Hello msg %d\n", i) + i++ + if i == 7 { + break + } + } +} diff --git a/go.mod b/go.mod index baff51b..b4896a7 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module gitlab.com/balki/ytui go 1.18 + +require golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5e34f23 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e h1:TsQ7F31D3bUCLeqPT0u+yjp1guoArKaNKmCr22PYgTQ= +golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= diff --git a/main.go b/main.go index 210859e..a446bd8 100644 --- a/main.go +++ b/main.go @@ -12,11 +12,13 @@ import ( "os" "os/exec" "path" + "strconv" "strings" "time" "gitlab.com/balki/ytui/db" "gitlab.com/balki/ytui/pubsub" + "golang.org/x/net/websocket" ) //go:embed templates/index.html @@ -94,6 +96,51 @@ func main() { } http.Redirect(w, r, "/", http.StatusSeeOther) }) + http.HandleFunc("/ws/", func(w http.ResponseWriter, r *http.Request) { + p := r.URL.Path + idStr := strings.TrimPrefix(p, "/ws/") + id, err := strconv.Atoi(idStr) + if err != nil { + log.Printf("Invalid id, %q, err:%v\n", idStr, err) + w.WriteHeader(http.StatusInternalServerError) + return + } + fmt.Println("path is", p, "id is", id) + var wh websocket.Handler = func(c *websocket.Conn) { + defer c.Close() + log.Println("Are you here?") + var sc <-chan string + d.Run(func(d *db.Jdb) { + pt := d.Items[id].Pt + sc = pt.Subscribe() + }) + if sc != nil { + for update := range sc { + _, err := c.Write([]byte(update)) + if err != nil { + log.Printf("err: %v\n", err) + return + } + } + } + var status db.DownloadStatus + var fname string + d.Run(func(d *db.Jdb) { + i := &d.Items[id] + status = i.Status + fname = i.FileName + }) + if status != db.Done { + _, err := c.Write([]byte(status)) + if err != nil { + log.Printf("err: %v\n", err) + } + return + } + c.Write([]byte(fname)) + } + wh.ServeHTTP(w, r) + }) log.Panic(http.ListenAndServe(fmt.Sprintf(":%v", port), nil)) } @@ -123,9 +170,26 @@ func download(id int, yturl string) { if err != nil { log.Panic(err) } + + go func() { + var sc <-chan string + d.Run(func(d *db.Jdb) { + pt := d.Items[id].Pt + sc = pt.Subscribe() + }) + log.Println("before watching") + if sc != nil { + for update := range sc { + log.Println(update) + } + } + log.Println("after watching") + }() + var status db.DownloadStatus var fname string if fname, err = downloadYt(id, yturl, pc); err != nil { + log.Printf("err: %v\n", err) status = db.Error } else { status = db.Done @@ -137,7 +201,8 @@ func download(id int, yturl string) { } func downloadYt(id int, yturl string, pc chan<- string) (string, error) { - pathTmpl := fmt.Sprintf("%s/video_%d.%%(ext)s", cachePath, id) + // pathTmpl := fmt.Sprintf("%s/video_%d.%%(ext)s", cachePath, id) + pathTmpl := fmt.Sprintf("%s/video_%d.mp4", cachePath, id) args := append(ytdlCmd, "--newline", "--output", pathTmpl, yturl) cmd := exec.Command(args[0], args[1:]...) rc, err := cmd.StdoutPipe() diff --git a/pubsub/pt.go b/pubsub/pt.go index 74f225b..c7a1bc3 100644 --- a/pubsub/pt.go +++ b/pubsub/pt.go @@ -98,7 +98,8 @@ func (pt *progressTracker) Publish() (chan<- string, error) { } return } - for _, subChan := range scs { + for i, _ := range scs { + subChan := &scs[i] if subChan.lastUpdateIndex != lastUpdateIndex { select { case subChan.c <- update: diff --git a/templates/index.html b/templates/index.html index 72a43fe..4f4707a 100644 --- a/templates/index.html +++ b/templates/index.html @@ -29,6 +29,8 @@ {{ if eq .Status "Done" }} Watch + {{ else if eq .Status "InProgress" }} + In Progress {{ else }} {{ .Status }} {{ end }}