diff --git a/README.md b/README.md
index e19fa4664874be84e39fe1be7740549b2ffa8a9c..7c8977099564782646fec54a964788e201819dd6 100644
--- a/README.md
+++ b/README.md
@@ -2,4 +2,3 @@ # yt downloader simple frontend
This projects aim to automat a bit youtube-dl/yt-dlp downloads and learn some
go in the process.
-
diff --git a/controller/controller.go b/controller/controller.go
index c7f41454059c3dfad7a8239ab948eb0779ba3f2e..701d34c80d127577f8ee7142e43582e3d4674688 100644
--- a/controller/controller.go
+++ b/controller/controller.go
@@ -1,8 +1,10 @@
package controller
import (
+ "log"
"net/http"
"strconv"
+ "strings"
"time"
"git.sr.ht/~gabrielgio/midr/db"
@@ -13,6 +15,13 @@
type Env struct {
Entries db.EntryModel
Worker worker.Worker
+}
+
+func logBytes(logc <-chan []byte) {
+ for l := range logc {
+ logs := strings.TrimRight(string(l), "\t \n")
+ log.Println(logs)
+ }
}
func (e *Env) GetEntries(c *gin.Context) {
@@ -41,7 +50,9 @@ func (e *Env) CreateEntry(c *gin.Context) {
var entry db.Entry
c.ShouldBind(&entry)
e.Entries.Create(&entry)
- e.Worker.SpawnWorker(&entry)
+ log := e.Worker.RunYtDlpWorker(&entry)
+ go logBytes(log)
+
c.Redirect(http.StatusFound, "/")
}
@@ -66,7 +77,10 @@ for {
entries := e.Entries.All()
for _, entry := range entries {
- e.Worker.SpawnWorker(&entry)
+ log := e.Worker.RunYtDlpWorker(&entry)
+ if log != nil {
+ go logBytes(log)
+ }
}
time.Sleep(30 * time.Second)
}
diff --git a/worker/worker.go b/worker/worker.go
index 2444e892278f1547dd820ac6e664107b158acdb0..525a736f254e2fb9a5d2fa4c30d7f2f6de06f923 100644
--- a/worker/worker.go
+++ b/worker/worker.go
@@ -1,10 +1,13 @@
package worker
import (
+ "bufio"
+ "bytes"
"context"
+ "fmt"
+ "os/exec"
"git.sr.ht/~gabrielgio/midr/db"
- "git.sr.ht/~gabrielgio/midr/yt"
work "git.sr.ht/~sircmpwn/dowork"
)
@@ -40,6 +43,30 @@ jobs: make(map[uint]string),
}
}
+func RunYtDlpProcess(entry *db.Entry) (error, []byte, []byte) {
+ args := []string{entry.Link}
+ var stdout bytes.Buffer
+ var stderr bytes.Buffer
+
+ output_template := fmt.Sprintf("%s/%%(title)s.%%(ext)s", entry.OutputFolder)
+ args = append(args, "-o", output_template)
+
+ downloaded_txt := fmt.Sprintf("%s/downloaded.txt", entry.OutputFolder)
+ args = append(args, "--download-archive", downloaded_txt)
+
+ if len(entry.DateAfter) > 0 {
+ args = append(args, "--dateafter", entry.DateAfter)
+ }
+
+ cmd := exec.Command("yt-dlp", args...)
+ cmd.Stdout = bufio.NewWriter(&stdout)
+ cmd.Stderr = bufio.NewWriter(&stderr)
+
+ err := cmd.Run()
+
+ return err, stdout.Bytes(), stderr.Bytes()
+}
+
func (w *Worker) CanEnqueue(index uint) bool {
v, found := w.jobs[index]
return !found || v == statusNotQueued
@@ -49,23 +76,31 @@ func (w *Worker) RemoveJob(id uint) {
delete(w.jobs, id)
}
-func (w *Worker) SpawnWorker(entry *db.Entry) {
-
+func (w *Worker) RunYtDlpWorker(entry *db.Entry) <-chan []byte {
if !w.CanEnqueue(entry.ID) {
- return
+ return nil
}
+
+ log := make(chan []byte)
w.c <- command{action: commandEnqueue, index: entry.ID}
task := work.NewTask(func(ctx context.Context) error {
w.c <- command{action: commandStart, index: entry.ID}
- yt.RunYtDlpProcess(entry)
- return nil
+ err, stdout, stderr := RunYtDlpProcess(entry)
+
+ log <- stdout
+ log <- stderr
+
+ return err
}).After(func(ctx context.Context, task *work.Task) {
w.c <- command{action: commandDequeue, index: entry.ID}
+ close(log)
})
work.Enqueue(task)
+
+ return log
}
func (w *Worker) startReader() {
diff --git a/yt/manager.go b/yt/manager.go
deleted file mode 100644
index b9dc3336f203538ef073730e34f044a3eccdf409..0000000000000000000000000000000000000000
--- a/yt/manager.go
+++ /dev/null
@@ -1,25 +0,0 @@
-package yt
-
-import (
- "fmt"
- "os/exec"
-
- "git.sr.ht/~gabrielgio/midr/db"
-)
-
-func RunYtDlpProcess(entry *db.Entry) error {
- args := []string{entry.Link}
-
- output_template := fmt.Sprintf("%s/%%(title)s.%%(ext)s", entry.OutputFolder)
- args = append(args, "-o", output_template)
-
- downloaded_txt := fmt.Sprintf("%s/downloaded.txt", entry.OutputFolder)
- args = append(args, "--download-archive", downloaded_txt)
-
- if len(entry.DateAfter) > 0 {
- args = append(args, "--dateafter", entry.DateAfter)
- }
-
- cmd := exec.Command("yt-dlp", args...)
- return cmd.Run()
-}