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"`
FileName string `json:"file_name"`
Pt pubsub.ProgressTracker `json:"-"`
TitleChan <-chan struct{} `json:"-"`
}
type Jdb struct {

40
main.go
View File

@ -107,8 +107,8 @@ func main() {
}
fmt.Println("path is", p, "id is", id)
var sc <-chan string
d.Run(func(d *db.Jdb) {
pt := d.Items[id].Pt
d.Update(id, false, func(i *db.Item) {
pt := i.Pt
if pt != nil {
sc = pt.Subscribe()
}
@ -126,8 +126,7 @@ func main() {
}
var status db.DownloadStatus
var fname string
d.Run(func(d *db.Jdb) {
i := &d.Items[id]
d.Update(id, true, func(i *db.Item) {
status = i.Status
fname = i.FileName
})
@ -143,10 +142,37 @@ func main() {
}
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))
}
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)
cmd := exec.Command(args[0], args[1:]...)
var title string
@ -175,11 +201,7 @@ func download(id int, yturl string) {
//Log progress
go func() {
var sc <-chan string
d.Run(func(d *db.Jdb) {
pt := d.Items[id].Pt
sc = pt.Subscribe()
})
sc := pt.Subscribe()
log.Println("Watching download progress for id: ", id)
if sc != nil {
for update := range sc {

View File

@ -21,6 +21,19 @@
socket.onmessage = function(event) {
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>
@ -45,7 +58,13 @@
<tr>
<td>{{ .Date }}</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>
{{ if eq .Status "Done" }}
<a target="_blank" href="{{ vids_prefix }}/{{ .FileName }}">Watch</a>