title fetch

This commit is contained in:
Balakrishnan Balasubramanian 2022-06-29 17:50:31 -04:00
parent 784b82313c
commit 08e7ecb446
3 changed files with 74 additions and 32 deletions

View File

@ -27,6 +27,7 @@ type Item struct {
Status DownloadStatus `json:"status"` Status DownloadStatus `json:"status"`
FileName string `json:"file_name"` FileName string `json:"file_name"`
Pt pubsub.ProgressTracker `json:"-"` Pt pubsub.ProgressTracker `json:"-"`
TitleChan <-chan struct{} `json:"-"`
} }
type Jdb struct { type Jdb struct {

40
main.go
View File

@ -107,8 +107,8 @@ func main() {
} }
fmt.Println("path is", p, "id is", id) fmt.Println("path is", p, "id is", id)
var sc <-chan string var sc <-chan string
d.Run(func(d *db.Jdb) { d.Update(id, false, func(i *db.Item) {
pt := d.Items[id].Pt pt := i.Pt
if pt != nil { if pt != nil {
sc = pt.Subscribe() sc = pt.Subscribe()
} }
@ -126,8 +126,7 @@ func main() {
} }
var status db.DownloadStatus var status db.DownloadStatus
var fname string var fname string
d.Run(func(d *db.Jdb) { d.Update(id, true, func(i *db.Item) {
i := &d.Items[id]
status = i.Status status = i.Status
fname = i.FileName fname = i.FileName
}) })
@ -143,10 +142,37 @@ func main() {
} }
wh.ServeHTTP(w, r) wh.ServeHTTP(w, r)
}) })
http.HandleFunc("/title/", func(w http.ResponseWriter, r *http.Request) {
p := r.URL.Path
idStr := strings.TrimPrefix(p, "/title/")
id, err := strconv.Atoi(idStr)
if err != nil {
log.Printf("Invalid id, %q, err:%v\n", idStr, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
var tc <-chan struct{}
d.Update(id, false, func(i *db.Item) {
tc = i.TitleChan
})
<-tc
var title string
d.Update(id, false, func(i *db.Item) {
title = i.Title
})
w.Write([]byte(title))
})
log.Panic(http.ListenAndServe(fmt.Sprintf(":%v", port), nil)) log.Panic(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
} }
func getTitle(id int, yturl string) { func getTitle(id int, yturl string) {
tc := make(chan struct{})
defer close(tc)
d.Update(id, false, func(i *db.Item) {
i.TitleChan = tc
})
args := append(ytdlCmd, "--get-title", yturl) args := append(ytdlCmd, "--get-title", yturl)
cmd := exec.Command(args[0], args[1:]...) cmd := exec.Command(args[0], args[1:]...)
var title string var title string
@ -175,11 +201,7 @@ func download(id int, yturl string) {
//Log progress //Log progress
go func() { go func() {
var sc <-chan string sc := pt.Subscribe()
d.Run(func(d *db.Jdb) {
pt := d.Items[id].Pt
sc = pt.Subscribe()
})
log.Println("Watching download progress for id: ", id) log.Println("Watching download progress for id: ", id)
if sc != nil { if sc != nil {
for update := range sc { for update := range sc {

View File

@ -21,6 +21,19 @@
socket.onmessage = function(event) { socket.onmessage = function(event) {
item.innerHTML = event.data item.innerHTML = event.data
} }
});
pendingTitles = document.querySelectorAll(".yttitle")
pendingTitles.forEach(function(item) {
ytid = item.dataset.ytid
fetch("/title/" + ytid).then(function(response) {
if(response.ok) {
return response.text().then(function(text) {
item.innerText = text
})
}
console.log("Response not ok")
});
}) })
}); });
</script> </script>
@ -45,7 +58,13 @@
<tr> <tr>
<td>{{ .Date }}</td> <td>{{ .Date }}</td>
<td>{{ .URL }}</td> <td>{{ .URL }}</td>
<td>{{ .Title }}</td> <td>
{{ if eq .Title "Loading" }}
<span class="yttitle" data-ytid="{{.Id}}">Loading...</span>
{{ else }}
{{ .Title }}
{{ end }}
</td>
<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>