From 08e7ecb4467e71c5d0669a8d9b90e65c8381a319 Mon Sep 17 00:00:00 2001 From: balki <3070606-balki@users.noreply.gitlab.com> Date: Wed, 29 Jun 2022 17:50:31 -0400 Subject: [PATCH] title fetch --- db/db.go | 15 +++++++------ main.go | 40 ++++++++++++++++++++++++++-------- templates/index.html | 51 ++++++++++++++++++++++++++++++-------------- 3 files changed, 74 insertions(+), 32 deletions(-) diff --git a/db/db.go b/db/db.go index 4e78a43..08a96f6 100644 --- a/db/db.go +++ b/db/db.go @@ -20,13 +20,14 @@ var ( ) type Item struct { - Id int `json:"id"` - Date string `json:"date"` - URL string `json:"url"` - Title string `json:"title"` - Status DownloadStatus `json:"status"` - FileName string `json:"file_name"` - Pt pubsub.ProgressTracker `json:"-"` + Id int `json:"id"` + Date string `json:"date"` + URL string `json:"url"` + Title string `json:"title"` + Status DownloadStatus `json:"status"` + FileName string `json:"file_name"` + Pt pubsub.ProgressTracker `json:"-"` + TitleChan <-chan struct{} `json:"-"` } type Jdb struct { diff --git a/main.go b/main.go index ee8b82a..bc5557c 100644 --- a/main.go +++ b/main.go @@ -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 { diff --git a/templates/index.html b/templates/index.html index d31bc90..8ded8dd 100644 --- a/templates/index.html +++ b/templates/index.html @@ -6,23 +6,36 @@
@@ -45,7 +58,13 @@