cleanup and add title
This commit is contained in:
		
							
								
								
									
										5
									
								
								db/db.go
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								db/db.go
									
									
									
									
									
								
							@@ -31,7 +31,6 @@ type Item struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Jdb struct {
 | 
			
		||||
	Foo   string `json:"-"`
 | 
			
		||||
	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 {
 | 
			
		||||
	data, err := json.Marshal(Jdb{"", d.items})
 | 
			
		||||
	data, err := json.Marshal(Jdb{d.items})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
@@ -83,7 +82,7 @@ func (d *Db) save() error {
 | 
			
		||||
func (d *Db) Run(f func(*Jdb)) {
 | 
			
		||||
	d.mutex.Lock()
 | 
			
		||||
	defer d.mutex.Unlock()
 | 
			
		||||
	f(&Jdb{"", d.items})
 | 
			
		||||
	f(&Jdb{d.items})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (d *Db) Save() error {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										16
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								main.go
									
									
									
									
									
								
							@@ -3,7 +3,6 @@ package main
 | 
			
		||||
import (
 | 
			
		||||
	"bufio"
 | 
			
		||||
	"embed"
 | 
			
		||||
	_ "embed"
 | 
			
		||||
	"flag"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"html/template"
 | 
			
		||||
@@ -37,6 +36,7 @@ var (
 | 
			
		||||
	saveAssets = false
 | 
			
		||||
	dbPath     = "./db.json"
 | 
			
		||||
	port       = 8080
 | 
			
		||||
	title      = "Youtube Downloader"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var d *db.Db
 | 
			
		||||
@@ -44,6 +44,7 @@ var d *db.Db
 | 
			
		||||
func parse() {
 | 
			
		||||
	ytcmd := ytdlCmd[0]
 | 
			
		||||
	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(&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")
 | 
			
		||||
@@ -69,9 +70,6 @@ func main() {
 | 
			
		||||
	log.SetFlags(log.Flags() | log.Lshortfile)
 | 
			
		||||
	parse()
 | 
			
		||||
	tmpl := template.New("page")
 | 
			
		||||
	tmpl = tmpl.Funcs(map[string]any{
 | 
			
		||||
		"vids_prefix": func() string { return videosUrl },
 | 
			
		||||
	})
 | 
			
		||||
	tmpl, err := tmpl.Parse(page)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Panic(err)
 | 
			
		||||
@@ -105,9 +103,13 @@ func main() {
 | 
			
		||||
 | 
			
		||||
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		if r.Method == http.MethodGet {
 | 
			
		||||
			d.Run(func(d *db.Jdb) {
 | 
			
		||||
				d.Foo = "Hello"
 | 
			
		||||
				if err := tmpl.Execute(w, d); err != nil {
 | 
			
		||||
			d.Run(func(jd *db.Jdb) {
 | 
			
		||||
				td := struct {
 | 
			
		||||
					Title     string
 | 
			
		||||
					VidoesUrl string
 | 
			
		||||
					Items     []db.Item
 | 
			
		||||
				}{title, videosUrl, jd.Items}
 | 
			
		||||
				if err := tmpl.Execute(w, td); err != nil {
 | 
			
		||||
					log.Panic(err)
 | 
			
		||||
				}
 | 
			
		||||
			})
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,7 @@
 | 
			
		||||
    <head>
 | 
			
		||||
        <meta charset="utf-8">
 | 
			
		||||
        <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 -->
 | 
			
		||||
        <link rel="icon" href="data:;base64,iVBORw0KGgo=">
 | 
			
		||||
        <link href="assets/bootstrap.css" rel="stylesheet" >
 | 
			
		||||
@@ -45,7 +45,7 @@
 | 
			
		||||
        </script>
 | 
			
		||||
    </head>
 | 
			
		||||
    <body>
 | 
			
		||||
        <h1 class="text-center"> {{ .Foo }} </h1>
 | 
			
		||||
        <h1 class="text-center"> {{ $.Title }} </h1>
 | 
			
		||||
        <div class="container">
 | 
			
		||||
            <div class="row my-4 text-center">
 | 
			
		||||
                <form method="POST" action="/">
 | 
			
		||||
@@ -68,7 +68,7 @@
 | 
			
		||||
                                <h6 class="card-subtitle mb-2 text-muted">On {{ .Date }}</h6>
 | 
			
		||||
                                <a href="{{ .URL }}" class="card-link d-block mb-2">Youtube Link</a>
 | 
			
		||||
                                {{ 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" }}
 | 
			
		||||
                                <span class="border border-info w-100 text-center d-block" data-ytstatus="progress" data-ytid="{{.Id}}">In Progress</span>
 | 
			
		||||
                                {{ else }}
 | 
			
		||||
@@ -103,7 +103,7 @@
 | 
			
		||||
                        </td>
 | 
			
		||||
                        <td>
 | 
			
		||||
                            {{ 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" }}
 | 
			
		||||
                            <span data-ytstatus="progress" data-ytid="{{.Id}}">In Progress</span>
 | 
			
		||||
                            {{ else }}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user