pt pubsub working
This commit is contained in:
parent
e2dc04c233
commit
c69654f38f
32
cmd/dummytdl/main.go
Normal file
32
cmd/dummytdl/main.go
Normal 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
2
go.mod
@ -1,3 +1,5 @@
|
||||
module gitlab.com/balki/ytui
|
||||
|
||||
go 1.18
|
||||
|
||||
require golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e
|
||||
|
2
go.sum
Normal file
2
go.sum
Normal 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
67
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()
|
||||
|
@ -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:
|
||||
|
@ -29,6 +29,8 @@
|
||||
<td>
|
||||
{{ if eq .Status "Done" }}
|
||||
<a target="_blank" href="{{ vids_prefix }}/{{ .FileName }}">Watch</a>
|
||||
{{ else if eq .Status "InProgress" }}
|
||||
<span class="progress" data="{{.Id}}">In Progress</span>
|
||||
{{ else }}
|
||||
{{ .Status }}
|
||||
{{ end }}
|
||||
|
Loading…
Reference in New Issue
Block a user