diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | assets/style.css | 100 | ||||
-rw-r--r-- | controller/controller.go | 11 | ||||
-rw-r--r-- | db/model.go | 1 | ||||
-rw-r--r-- | templates/_footer.tmpl | 3 | ||||
-rw-r--r-- | templates/_head.tmpl | 9 | ||||
-rw-r--r-- | templates/entry.tmpl | 6 | ||||
-rw-r--r-- | templates/index.tmpl | 10 | ||||
-rw-r--r-- | worker/worker.go | 22 | ||||
-rw-r--r-- | yt/manager.go | 22 |
10 files changed, 153 insertions, 32 deletions
@@ -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" }} </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 b99510f..11a4aa8 100644 --- a/templates/_head.tmpl +++ b/templates/_head.tmpl @@ -3,12 +3,11 @@ <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 9f1181f..9edd5cd 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 @@ <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 ae7f674..14cb420 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>{{ .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 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() } |