pt pubsub working

This commit is contained in:
Balakrishnan Balasubramanian 2022-06-28 23:02:53 -04:00
parent e2dc04c233
commit c69654f38f
6 changed files with 106 additions and 2 deletions

32
cmd/dummytdl/main.go Normal file
View File

@ -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
}
}
}

2
go.mod
View File

@ -1,3 +1,5 @@
module gitlab.com/balki/ytui module gitlab.com/balki/ytui
go 1.18 go 1.18
require golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e

2
go.sum Normal file
View File

@ -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=

67
main.go
View File

@ -12,11 +12,13 @@ import (
"os" "os"
"os/exec" "os/exec"
"path" "path"
"strconv"
"strings" "strings"
"time" "time"
"gitlab.com/balki/ytui/db" "gitlab.com/balki/ytui/db"
"gitlab.com/balki/ytui/pubsub" "gitlab.com/balki/ytui/pubsub"
"golang.org/x/net/websocket"
) )
//go:embed templates/index.html //go:embed templates/index.html
@ -94,6 +96,51 @@ func main() {
} }
http.Redirect(w, r, "/", http.StatusSeeOther) 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)) log.Panic(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
} }
@ -123,9 +170,26 @@ func download(id int, yturl string) {
if err != nil { if err != nil {
log.Panic(err) 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 status db.DownloadStatus
var fname string var fname string
if fname, err = downloadYt(id, yturl, pc); err != nil { if fname, err = downloadYt(id, yturl, pc); err != nil {
log.Printf("err: %v\n", err)
status = db.Error status = db.Error
} else { } else {
status = db.Done status = db.Done
@ -137,7 +201,8 @@ func download(id int, yturl string) {
} }
func downloadYt(id int, yturl string, pc chan<- string) (string, error) { 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) args := append(ytdlCmd, "--newline", "--output", pathTmpl, yturl)
cmd := exec.Command(args[0], args[1:]...) cmd := exec.Command(args[0], args[1:]...)
rc, err := cmd.StdoutPipe() rc, err := cmd.StdoutPipe()

View File

@ -98,7 +98,8 @@ func (pt *progressTracker) Publish() (chan<- string, error) {
} }
return return
} }
for _, subChan := range scs { for i, _ := range scs {
subChan := &scs[i]
if subChan.lastUpdateIndex != lastUpdateIndex { if subChan.lastUpdateIndex != lastUpdateIndex {
select { select {
case subChan.c <- update: case subChan.c <- update:

View File

@ -29,6 +29,8 @@
<td> <td>
{{ if eq .Status "Done" }} {{ if eq .Status "Done" }}
<a target="_blank" href="{{ vids_prefix }}/{{ .FileName }}">Watch</a> <a target="_blank" href="{{ vids_prefix }}/{{ .FileName }}">Watch</a>
{{ else if eq .Status "InProgress" }}
<span class="progress" data="{{.Id}}">In Progress</span>
{{ else }} {{ else }}
{{ .Status }} {{ .Status }}
{{ end }} {{ end }}