cleanup and add title

This commit is contained in:
Balakrishnan Balasubramanian 2022-07-14 16:13:27 -04:00
parent 5a8af2699b
commit e2ac680121
3 changed files with 15 additions and 14 deletions

View File

@ -31,7 +31,6 @@ type Item struct {
} }
type Jdb struct { type Jdb struct {
Foo string `json:"-"`
Items []Item `json:"items"` Items []Item `json:"items"`
} }
@ -73,7 +72,7 @@ func (d *Db) Transact(id int, persist bool, f func(*Item)) error {
} }
func (d *Db) save() error { func (d *Db) save() error {
data, err := json.Marshal(Jdb{"", d.items}) data, err := json.Marshal(Jdb{d.items})
if err != nil { if err != nil {
return err return err
} }
@ -83,7 +82,7 @@ func (d *Db) save() error {
func (d *Db) Run(f func(*Jdb)) { func (d *Db) Run(f func(*Jdb)) {
d.mutex.Lock() d.mutex.Lock()
defer d.mutex.Unlock() defer d.mutex.Unlock()
f(&Jdb{"", d.items}) f(&Jdb{d.items})
} }
func (d *Db) Save() error { func (d *Db) Save() error {

16
main.go
View File

@ -3,7 +3,6 @@ package main
import ( import (
"bufio" "bufio"
"embed" "embed"
_ "embed"
"flag" "flag"
"fmt" "fmt"
"html/template" "html/template"
@ -37,6 +36,7 @@ var (
saveAssets = false saveAssets = false
dbPath = "./db.json" dbPath = "./db.json"
port = 8080 port = 8080
title = "Youtube Downloader"
) )
var d *db.Db var d *db.Db
@ -44,6 +44,7 @@ var d *db.Db
func parse() { func parse() {
ytcmd := ytdlCmd[0] ytcmd := ytdlCmd[0]
flag.StringVar(&ytcmd, "ytdlcmd", ytcmd, "youtube-dl command seperated by spaces") flag.StringVar(&ytcmd, "ytdlcmd", ytcmd, "youtube-dl command seperated by spaces")
flag.StringVar(&title, "title", title, "Title of the page")
flag.StringVar(&videosPath, "videospath", videosPath, "Path where videos are saved") flag.StringVar(&videosPath, "videospath", videosPath, "Path where videos are saved")
flag.StringVar(&videosUrl, "videosurl", videosUrl, "Prefix of the url, i.e. https://domain.com/<this var>/<video filename>") flag.StringVar(&videosUrl, "videosurl", videosUrl, "Prefix of the url, i.e. https://domain.com/<this var>/<video filename>")
flag.StringVar(&cachePath, "cachepath", cachePath, "Path where temporary download files are saved") flag.StringVar(&cachePath, "cachepath", cachePath, "Path where temporary download files are saved")
@ -69,9 +70,6 @@ func main() {
log.SetFlags(log.Flags() | log.Lshortfile) log.SetFlags(log.Flags() | log.Lshortfile)
parse() parse()
tmpl := template.New("page") tmpl := template.New("page")
tmpl = tmpl.Funcs(map[string]any{
"vids_prefix": func() string { return videosUrl },
})
tmpl, err := tmpl.Parse(page) tmpl, err := tmpl.Parse(page)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
@ -105,9 +103,13 @@ func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet { if r.Method == http.MethodGet {
d.Run(func(d *db.Jdb) { d.Run(func(jd *db.Jdb) {
d.Foo = "Hello" td := struct {
if err := tmpl.Execute(w, d); err != nil { Title string
VidoesUrl string
Items []db.Item
}{title, videosUrl, jd.Items}
if err := tmpl.Execute(w, td); err != nil {
log.Panic(err) log.Panic(err)
} }
}) })

View File

@ -3,7 +3,7 @@
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>Youtube Downloader UI</title> <title>{{ $.Title }}</title>
<!-- Diable favicon requests: https://stackoverflow.com/a/13416784 --> <!-- Diable favicon requests: https://stackoverflow.com/a/13416784 -->
<link rel="icon" href="data:;base64,iVBORw0KGgo="> <link rel="icon" href="data:;base64,iVBORw0KGgo=">
<link href="assets/bootstrap.css" rel="stylesheet" > <link href="assets/bootstrap.css" rel="stylesheet" >
@ -45,7 +45,7 @@
</script> </script>
</head> </head>
<body> <body>
<h1 class="text-center"> {{ .Foo }} </h1> <h1 class="text-center"> {{ $.Title }} </h1>
<div class="container"> <div class="container">
<div class="row my-4 text-center"> <div class="row my-4 text-center">
<form method="POST" action="/"> <form method="POST" action="/">
@ -68,7 +68,7 @@
<h6 class="card-subtitle mb-2 text-muted">On {{ .Date }}</h6> <h6 class="card-subtitle mb-2 text-muted">On {{ .Date }}</h6>
<a href="{{ .URL }}" class="card-link d-block mb-2">Youtube Link</a> <a href="{{ .URL }}" class="card-link d-block mb-2">Youtube Link</a>
{{ if eq .Status "Done" }} {{ if eq .Status "Done" }}
<a class="btn btn-primary w-100" target="_blank" href="{{ vids_prefix }}/{{ .FileName }}">Watch</a> <a class="btn btn-primary w-100" target="_blank" href="{{ $.VidoesUrl }}/{{ .FileName }}">Watch</a>
{{ else if eq .Status "InProgress" }} {{ else if eq .Status "InProgress" }}
<span class="border border-info w-100 text-center d-block" data-ytstatus="progress" data-ytid="{{.Id}}">In Progress</span> <span class="border border-info w-100 text-center d-block" data-ytstatus="progress" data-ytid="{{.Id}}">In Progress</span>
{{ else }} {{ else }}
@ -103,7 +103,7 @@
</td> </td>
<td> <td>
{{ if eq .Status "Done" }} {{ if eq .Status "Done" }}
<a target="_blank" href="{{ vids_prefix }}/{{ .FileName }}">Watch</a> <a target="_blank" href="{{ $.VidoesUrl }}/{{ .FileName }}">Watch</a>
{{ else if eq .Status "InProgress" }} {{ else if eq .Status "InProgress" }}
<span data-ytstatus="progress" data-ytid="{{.Id}}">In Progress</span> <span data-ytstatus="progress" data-ytid="{{.Id}}">In Progress</span>
{{ else }} {{ else }}