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 {
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 {

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

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