diff --git a/.gitignore b/.gitignore
index 329aa21294f047bbbe577a5f5fbeba71cbcf00db..579c7ac6e983bae789f2d307a00f5c19a8e476a5 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 0000000000000000000000000000000000000000..7f524b9e18950647ef523c4edca09cc03477d870
--- /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 e381bf81c9f1386fcbdb5ad9f45629b5bd1890d9..c7f41454059c3dfad7a8239ab948eb0779ba3f2e 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) 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 0a5ca98a33c029aa1291ab7bced124f13273c95b..0d5cb47996c2813d2c47e83a088c5fa778553d04 100644
--- a/db/model.go
+++ b/db/model.go
@@ -9,6 +9,7 @@ gorm.Model
Title string
Link string
Format string
+ DateAfter string
OutputFolder string
}
diff --git a/templates/_footer.tmpl b/templates/_footer.tmpl
index 97a5bf86321cb9dfb79e36f15c192f3bb67392ba..b72953bfe205e50078ddbd7e2cf27b23ab0596ab 100644
--- a/templates/_footer.tmpl
+++ b/templates/_footer.tmpl
@@ -1,6 +1,5 @@
{{ define "_footer" }}
</main>
- </section>
<script>
function deleteEntry(id) {
fetch("/entries/"+id, { method: 'DELETE' })
@@ -20,7 +19,7 @@ }
});
}
-setInterval(getStatus, 1000);
+setInterval(getStatus, 5000);
</script>
</body>
diff --git a/templates/_head.tmpl b/templates/_head.tmpl
index b99510f9b7ebbd4035848c4735481ebd55ee3a5c..11a4aa80c3bfb9eb7394dc8b50c920e476174831 100644
--- a/templates/_head.tmpl
+++ b/templates/_head.tmpl
@@ -3,12 +3,11 @@ <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="/assets/bulma.min.css">
+ <link rel="stylesheet" href="/assets/style.css">
</head>
<body>
- <section class="section">
- <div class="container">
- <h1><a href="/"><strong>Home</strong></a></h1>
- </div>
+ <header>
+ <h1><a href="/">midr</a></h1>
+ </header>
<main class="container">
{{ end }}
diff --git a/templates/entry.tmpl b/templates/entry.tmpl
index 9f1181fdb5ad5771b731713f7b50b35f3bff9fb7..9edd5cdd0d0ce85251cbec5fc3f64230de0df150 100644
--- a/templates/entry.tmpl
+++ b/templates/entry.tmpl
@@ -1,6 +1,5 @@
{{ define "entry" }}
{{ template "_head" }}
-<div class="container">
{{ if (eq .ID 0) }}
<form action="/entries" method="POST">
{{ else }}
@@ -16,11 +15,14 @@ <label for="link">Link</label>
<input class="input" type="text" id="Link" name="Link" value="{{ .Link }}" placeholder="Paste a valid youtube-dl link" required>
</div>
<div class="field">
+ <label for="DateAfter">Date after</label>
+ <input class="input" type="text" id="DateAfter" name="DateAfter" value="{{ .DateAfter }}" placeholder="Select the start date" required>
+ </div>
+ <div class="field">
<label for="output">Output folder</label>
<input class="input" type="text" id="OutputFolder" name="OutputFolder" value="{{ .OutputFolder }}" placeholder="Select a ralative folder" required>
</div>
<button type="submit">Submit</button>
</form>
- </div>
{{ template "_footer" }}
{{ end }}
diff --git a/templates/index.tmpl b/templates/index.tmpl
index ae7f6747e8f506135ea1732727f54b8ab1886e4c..14cb420741e73d970f6161b700904b603c339c53 100644
--- a/templates/index.tmpl
+++ b/templates/index.tmpl
@@ -1,14 +1,13 @@
{{ define "index" }}
{{ template "_head" }}
-<div class="container">
-<table class="table">
+<table>
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Title</th>
<th scope="col">Link</th>
<th scope="col">Output</th>
- <th scope="col">Status</th>
+ <th class="fixed" scope="col">Status</th>
<th scope="col"></th>
</tr>
</thead>
@@ -19,19 +18,18 @@ <td>{{ .ID }}</td>
<td>{{ .Title }}</td>
<td>{{ .Link }}</td>
<td>{{ .OutputFolder }}</td>
- <td>
+ <td class="fixed">
<span id="status_{{ .ID }}" class="tag is-primary is-light"></span>
</td>
<td>
<a href="entries/{{ .ID }}" >Edit</a>
</span>
- <a onclick="deleteEntry({{ .ID }})">Delete</a>
+ <a href="#delete/{{ .ID }}" onclick="deleteEntry({{ .ID }})">Delete</a>
</td>
</tr>
{{ end }}
</tbody>
</table>
-</div>
<a href="/entries/createEntry" class="button">Create</a>
{{ template "_footer" }}
{{ end }}
diff --git a/worker/worker.go b/worker/worker.go
index a8f15180e1093473163b8eb4ccf274786ea6d380..2444e892278f1547dd820ac6e664107b158acdb0 100644
--- a/worker/worker.go
+++ b/worker/worker.go
@@ -3,6 +3,7 @@
import (
"context"
+ "git.sr.ht/~gabrielgio/midr/db"
"git.sr.ht/~gabrielgio/midr/yt"
work "git.sr.ht/~sircmpwn/dowork"
)
@@ -34,7 +35,7 @@ }
func NewWorkder() Worker {
return Worker{
- c: make(chan command, 10),
+ c: make(chan command),
jobs: make(map[uint]string),
}
}
@@ -44,26 +45,31 @@ v, found := w.jobs[index]
return !found || v == statusNotQueued
}
-func (w *Worker) SpawnWorker(index uint, link string, output string) {
+func (w *Worker) RemoveJob(id uint) {
+ delete(w.jobs, id)
+}
- if !w.CanEnqueue(index) {
+func (w *Worker) SpawnWorker(entry *db.Entry) {
+
+ 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 c0cf6cb7eef0168a5f79c944625d9862aa76c7c9..b9dc3336f203538ef073730e34f044a3eccdf409 100644
--- a/yt/manager.go
+++ b/yt/manager.go
@@ -3,11 +3,23 @@
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()
}