2022-06-14 15:33:34 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
_ "embed"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"io/fs"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gitlab.com/balki/ytui/db"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed templates/index.html
|
|
|
|
var page string
|
|
|
|
|
|
|
|
const port = 8080
|
|
|
|
|
|
|
|
var (
|
|
|
|
ytdlCmd = []string{"youtube-dl"}
|
|
|
|
videosPath = "./vids"
|
2022-06-14 21:09:30 -04:00
|
|
|
videosUrl = "/vids"
|
2022-06-14 15:33:34 -04:00
|
|
|
cachePath = "./cache"
|
|
|
|
dbPath = "./db.json"
|
|
|
|
approval bool = false
|
|
|
|
)
|
|
|
|
|
|
|
|
var d *db.Db
|
|
|
|
|
|
|
|
func parse() {
|
|
|
|
ytcmd := ytdlCmd[0]
|
|
|
|
flag.StringVar(&ytcmd, "ytdlcmd", ytcmd, "youtube-dl command seperated by spaces")
|
|
|
|
flag.StringVar(&videosPath, "videospath", videosPath, "Path where videos are saved")
|
2022-06-14 21:09:30 -04:00
|
|
|
flag.StringVar(&videosUrl, "videosurl", videosUrl, "Prefix of the url, i.e. https://domain.com/<this var>/<video filename>")
|
2022-06-14 15:33:34 -04:00
|
|
|
flag.StringVar(&cachePath, "cachepath", cachePath, "Path where temporary download files are saved")
|
2022-06-14 21:09:30 -04:00
|
|
|
flag.StringVar(&dbPath, "dbpath", dbPath, "Path where downloaded info is saved")
|
2022-06-14 15:33:34 -04:00
|
|
|
flag.BoolVar(&approval, "approval", approval, "Is approval required before allowing to watch")
|
|
|
|
flag.Parse()
|
|
|
|
if ytcmd != ytdlCmd[0] {
|
|
|
|
ytdlCmd = strings.Fields(ytcmd)
|
|
|
|
}
|
2022-06-14 21:09:30 -04:00
|
|
|
os.MkdirAll(cachePath, 0755)
|
|
|
|
os.MkdirAll(videosPath, 0755)
|
|
|
|
os.MkdirAll(path.Dir(dbPath), 0755)
|
2022-06-14 15:33:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2022-06-24 23:43:38 -04:00
|
|
|
log.Print("Youtube UI")
|
|
|
|
log.SetFlags(log.Flags() | log.Lshortfile)
|
2022-06-14 15:33:34 -04:00
|
|
|
parse()
|
2022-06-14 21:09:30 -04:00
|
|
|
tmpl := template.New("page")
|
|
|
|
tmpl = tmpl.Funcs(map[string]any{
|
|
|
|
"vids_prefix": func() string { return videosUrl },
|
|
|
|
})
|
|
|
|
tmpl, err := tmpl.Parse(page)
|
2022-06-14 15:33:34 -04:00
|
|
|
if err != nil {
|
2022-06-24 23:43:38 -04:00
|
|
|
log.Panic(err)
|
2022-06-14 15:33:34 -04:00
|
|
|
}
|
|
|
|
d, err = db.Load(dbPath)
|
|
|
|
if err != nil {
|
2022-06-24 23:43:38 -04:00
|
|
|
log.Panic(err)
|
2022-06-14 15:33:34 -04:00
|
|
|
}
|
|
|
|
defer d.Save()
|
|
|
|
seen := map[string]struct{}{}
|
|
|
|
d.Run(func(d *db.Jdb) {
|
|
|
|
for _, i := range d.Items {
|
|
|
|
seen[i.URL] = struct{}{}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
d.Run(func(d *db.Jdb) {
|
|
|
|
if err := tmpl.Execute(w, d); err != nil {
|
2022-06-24 23:43:38 -04:00
|
|
|
log.Panic(err)
|
2022-06-14 15:33:34 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
yturl := r.PostFormValue("youtube_url")
|
|
|
|
if yturl == "" {
|
2022-06-24 23:43:38 -04:00
|
|
|
log.Printf("yturl empty, postform:%v\n", r.PostForm)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
2022-06-14 15:33:34 -04:00
|
|
|
}
|
|
|
|
if _, ok := seen[yturl]; !ok {
|
|
|
|
seen[yturl] = struct{}{}
|
|
|
|
id := d.Add(db.Item{
|
2022-06-14 21:09:30 -04:00
|
|
|
Date: time.Now().Format("2006-01-02 15:04"),
|
2022-06-14 15:33:34 -04:00
|
|
|
URL: yturl,
|
|
|
|
Title: "Loading",
|
|
|
|
Approved: false,
|
|
|
|
Status: db.NotStarted,
|
|
|
|
})
|
|
|
|
go getTitle(id, yturl)
|
|
|
|
go download(id, yturl)
|
|
|
|
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
})
|
2022-06-24 23:43:38 -04:00
|
|
|
log.Panic(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
|
2022-06-14 15:33:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func getTitle(id int, yturl string) {
|
|
|
|
args := append(ytdlCmd, "--get-title", yturl)
|
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
2022-06-24 23:43:38 -04:00
|
|
|
var title string
|
|
|
|
if op, err := cmd.Output(); err != nil {
|
|
|
|
log.Printf("command failed, cmd: %v, err: %v", cmd, err)
|
|
|
|
title = "ERROR"
|
|
|
|
} else {
|
|
|
|
title = string(op)
|
2022-06-14 15:33:34 -04:00
|
|
|
}
|
|
|
|
d.Update(id, true, func(i *db.Item) {
|
2022-06-24 23:43:38 -04:00
|
|
|
i.Title = title
|
2022-06-14 15:33:34 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func download(id int, yturl string) {
|
|
|
|
d.Update(id, true, func(i *db.Item) {
|
|
|
|
i.Status = db.InProgress
|
|
|
|
})
|
|
|
|
pathTmpl := fmt.Sprintf("%s/video_%d.%%(ext)s", cachePath, id)
|
|
|
|
args := append(ytdlCmd, "--newline", "--output", pathTmpl, yturl)
|
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
|
|
rc, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
br := bufio.NewReader(rc)
|
|
|
|
for {
|
|
|
|
line, _, err := br.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
d.Update(id, false, func(i *db.Item) {
|
|
|
|
i.Progress = string(line)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
rc.Close()
|
|
|
|
err = cmd.Wait()
|
|
|
|
var status db.DownloadStatus
|
|
|
|
var fname string
|
|
|
|
if err != nil {
|
|
|
|
status = db.Error
|
|
|
|
} else {
|
|
|
|
status = db.Done
|
|
|
|
matches, err := fs.Glob(os.DirFS(cachePath), fmt.Sprintf("video_%d.*", id))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if len(matches) != 1 {
|
|
|
|
panic(len(matches))
|
|
|
|
}
|
|
|
|
fname = matches[0]
|
|
|
|
err = os.Rename(path.Join(cachePath, fname), path.Join(videosPath, fname))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.Update(id, true, func(i *db.Item) {
|
|
|
|
i.Status = status
|
|
|
|
i.FileName = fname
|
|
|
|
})
|
|
|
|
}
|