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

@ -20,13 +20,14 @@ var (
) )
type Item struct { type Item struct {
Id int `json:"id"` Id int `json:"id"`
Date string `json:"date"` Date string `json:"date"`
URL string `json:"url"` URL string `json:"url"`
Title string `json:"title"` Title string `json:"title"`
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

@ -6,23 +6,36 @@
<link rel="icon" href="data:;base64,iVBORw0KGgo="> <link rel="icon" href="data:;base64,iVBORw0KGgo=">
<script> <script>
// https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event // https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event
window.addEventListener('DOMContentLoaded', () => { window.addEventListener('DOMContentLoaded', () => {
progressElems = document.querySelectorAll(".ytprogress") progressElems = document.querySelectorAll(".ytprogress")
progressElems.forEach(function(item) { progressElems.forEach(function(item) {
// https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes // https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes
item.innerText = item.dataset.ytid item.innerText = item.dataset.ytid
ytid = item.dataset.ytid ytid = item.dataset.ytid
// https://stackoverflow.com/a/47472874 // https://stackoverflow.com/a/47472874
var url = new URL("/ws/" + ytid, window.location.href); var url = new URL("/ws/" + ytid, window.location.href);
url.protocol = url.protocol.replace('http', 'ws'); url.protocol = url.protocol.replace('http', 'ws');
let socket = new WebSocket(url.href) let socket = new WebSocket(url.href)
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>
</head> </head>
<body> <body>
@ -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>