From 90d9d819b70f68e10482954cfc461737c0165f8a Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Wed, 13 Jul 2022 12:22:15 +0200 Subject: feat: Add custom css --- .gitignore | 1 + assets/style.css | 100 +++++++++++++++++++++++++++++++++++++++++++++++ controller/controller.go | 11 ++++-- db/model.go | 1 + templates/_footer.tmpl | 3 +- templates/_head.tmpl | 9 ++--- templates/entry.tmpl | 6 ++- templates/index.tmpl | 10 ++--- worker/worker.go | 22 +++++++---- yt/manager.go | 22 ++++++++--- 10 files changed, 153 insertions(+), 32 deletions(-) create mode 100644 assets/style.css diff --git a/.gitignore b/.gitignore index 329aa21..579c7ac 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ midr __debug_bin .cache/ +.nenv/ diff --git a/assets/style.css b/assets/style.css new file mode 100644 index 0000000..7f524b9 --- /dev/null +++ b/assets/style.css @@ -0,0 +1,100 @@ +html, body, div, h1, header,section, table, th, td, tr{ + margin: 0; + padding: 0; + border: 0; +} + +body { + font-family: sans-serif; + margin: 0 auto; + background-color: #f4f4f4; +} + +header { + background-color: #0062cc; + padding: .5em; + margin: 0 auto; +} + +header h1 { + font-size: large; + text-transform: uppercase; +} + +header > h1 > a { + color: white; + text-decoration: none; +} + +main { + margin: 1em; +} + +table { + display: block; + border-spacing: 0; + width: 100%; + overflow:auto; + margin-top: 1em; +} + +th.fixed, td.fixed { + width: 100px; + overflow: hidden; +} + +th { + padding: 1em; + text-align: left; +} + +td { + padding: 1em; + overflow: hidden; + white-space: nowrap; + border-top: 1px solid #ccc; +} + +form { + width: 70%; + max-width: 500px; +} + +form input { + width: 100% +} + +form label { + display: block; +} + +.container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; +} + +.field { + margin-bottom: 1em; +} + +a.button, button { + display: inline-block; + padding: .1rem .75rem; + background: #e9ecef; + border: #343a40 1px solid; + font-size: .9rem; + font-weight: 400; + line-height: 1.5; + cursor: pointer; + color: #000; + border-radius: 0; + text-decoration: none; + transition: 0.5s all; + align-self: flex-start; +} + +a.button:hover { + background-color: #fff; +} diff --git a/controller/controller.go b/controller/controller.go index e381bf8..c7f4145 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -2,6 +2,7 @@ package controller import ( "net/http" + "strconv" "time" "git.sr.ht/~gabrielgio/midr/db" @@ -40,7 +41,7 @@ func (e *Env) CreateEntry(c *gin.Context) { var entry db.Entry c.ShouldBind(&entry) e.Entries.Create(&entry) - e.Worker.SpawnWorker(entry.ID, entry.Link, entry.OutputFolder) + e.Worker.SpawnWorker(&entry) c.Redirect(http.StatusFound, "/") } @@ -48,6 +49,8 @@ func (e *Env) DeleteEntry(c *gin.Context) { var entry db.Entry id := c.Param("id") e.Entries.Delete(id) + u64, _ := strconv.ParseUint(id, 10, 32) + e.Worker.RemoveJob(uint(u64)) c.HTML(http.StatusOK, "entry", entry) } @@ -59,13 +62,13 @@ func (e *Env) GetJobs(c *gin.Context) { func (e *Env) StartScheduler() { e.Worker.StartReader() go func() { - for true { + for { entries := e.Entries.All() for _, entry := range entries { - e.Worker.SpawnWorker(entry.ID, entry.Link, entry.OutputFolder) + e.Worker.SpawnWorker(&entry) } - time.Sleep(30 * time.Minute) + time.Sleep(30 * time.Second) } }() } diff --git a/db/model.go b/db/model.go index 0a5ca98..0d5cb47 100644 --- a/db/model.go +++ b/db/model.go @@ -9,6 +9,7 @@ type Entry struct { Title string Link string Format string + DateAfter string OutputFolder string } diff --git a/templates/_footer.tmpl b/templates/_footer.tmpl index 97a5bf8..b72953b 100644 --- a/templates/_footer.tmpl +++ b/templates/_footer.tmpl @@ -1,6 +1,5 @@ {{ define "_footer" }} - diff --git a/templates/_head.tmpl b/templates/_head.tmpl index b99510f..11a4aa8 100644 --- a/templates/_head.tmpl +++ b/templates/_head.tmpl @@ -3,12 +3,11 @@ - + -
-
-

Home

-
+
+

midr

+
{{ end }} diff --git a/templates/entry.tmpl b/templates/entry.tmpl index 9f1181f..9edd5cd 100644 --- a/templates/entry.tmpl +++ b/templates/entry.tmpl @@ -1,6 +1,5 @@ {{ define "entry" }} {{ template "_head" }} -
{{ if (eq .ID 0) }}
{{ else }} @@ -15,12 +14,15 @@
+
+ + +
- {{ template "_footer" }} {{ end }} diff --git a/templates/index.tmpl b/templates/index.tmpl index ae7f674..14cb420 100644 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -1,14 +1,13 @@ {{ define "index" }} {{ template "_head" }} -
- +
- + @@ -19,19 +18,18 @@ - {{ end }}
ID Title Link OutputStatusStatus
{{ .Title }} {{ .Link }} {{ .OutputFolder }} + Edit - Delete + Delete
-
Create {{ template "_footer" }} {{ end }} diff --git a/worker/worker.go b/worker/worker.go index a8f1518..2444e89 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -3,6 +3,7 @@ package worker import ( "context" + "git.sr.ht/~gabrielgio/midr/db" "git.sr.ht/~gabrielgio/midr/yt" work "git.sr.ht/~sircmpwn/dowork" ) @@ -34,7 +35,7 @@ type Job struct { func NewWorkder() Worker { return Worker{ - c: make(chan command, 10), + c: make(chan command), jobs: make(map[uint]string), } } @@ -44,26 +45,31 @@ func (w *Worker) CanEnqueue(index uint) bool { return !found || v == statusNotQueued } -func (w *Worker) SpawnWorker(index uint, link string, output string) { +func (w *Worker) RemoveJob(id uint) { + delete(w.jobs, id) +} + +func (w *Worker) SpawnWorker(entry *db.Entry) { - if !w.CanEnqueue(index) { + if !w.CanEnqueue(entry.ID) { return } - w.c <- command{action: commandEnqueue, index: index} + w.c <- command{action: commandEnqueue, index: entry.ID} task := work.NewTask(func(ctx context.Context) error { - w.c <- command{action: commandStart, index: index} - yt.RunYtDlpProcess(link, output) + + w.c <- command{action: commandStart, index: entry.ID} + yt.RunYtDlpProcess(entry) return nil }).After(func(ctx context.Context, task *work.Task) { - w.c <- command{action: commandDequeue, index: index} + w.c <- command{action: commandDequeue, index: entry.ID} }) work.Enqueue(task) } func (w *Worker) startReader() { - for true { + for { command := <-w.c if command.action == commandEnqueue { diff --git a/yt/manager.go b/yt/manager.go index c0cf6cb..b9dc333 100644 --- a/yt/manager.go +++ b/yt/manager.go @@ -3,11 +3,23 @@ package yt import ( "fmt" "os/exec" + + "git.sr.ht/~gabrielgio/midr/db" ) -func RunYtDlpProcess(link string, output string) { - output_template := fmt.Sprintf("%s/%%(title)s.%%(ext)s", output) - downloaded_txt := fmt.Sprintf("%s/downloaded.txt", output) - cmd := exec.Command("yt-dlp", link, "-o", output_template, "--download-archive", downloaded_txt) - cmd.Run() +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() } -- cgit v1.2.3