hopefully channel working

This commit is contained in:
Balakrishnan Balasubramanian 2022-06-28 15:26:16 -04:00
parent 882f8aea2f
commit e2dc04c233
3 changed files with 81 additions and 61 deletions

View File

@ -6,6 +6,8 @@ import (
"fmt"
"os"
"sync"
"gitlab.com/balki/ytui/pubsub"
)
type DownloadStatus string
@ -18,14 +20,13 @@ var (
)
type Item struct {
Id int `json:"id"`
Date string `json:"date"`
URL string `json:"url"`
Title string `json:"title"`
Approved bool `json:"approved"`
Status DownloadStatus `json:"status"`
FileName string `json:"file_name"`
Progress string `json:"-"`
Id int `json:"id"`
Date string `json:"date"`
URL string `json:"url"`
Title string `json:"title"`
Status DownloadStatus `json:"status"`
FileName string `json:"file_name"`
Pt pubsub.ProgressTracker `json:"-"`
}
type Jdb struct {
@ -37,16 +38,21 @@ type Db struct {
mutex sync.Mutex
lastId int
path string
index map[string]int
}
func (d *Db) Add(i Item) int {
func (d *Db) Add(i Item) (int, bool) {
d.mutex.Lock()
defer d.mutex.Unlock()
if id, ok := d.index[i.URL]; ok {
return id, false
}
i.Id = d.lastId
d.lastId++
d.items = append(d.items, i)
d.save()
return i.Id
d.index[i.URL] = i.Id
return i.Id, true
}
func (d *Db) Update(id int, persist bool, f func(*Item)) error {
@ -101,14 +107,17 @@ func Load(path string) (*Db, error) {
return nil, err
}
m := 0
indexMap := map[string]int{}
for _, item := range jd.Items {
if item.Id > m {
m = item.Id
}
indexMap[item.URL] = item.Id
}
return &Db{
items: jd.Items,
path: path,
lastId: m + 1,
index: indexMap,
}, nil
}

111
main.go
View File

@ -16,6 +16,7 @@ import (
"time"
"gitlab.com/balki/ytui/db"
"gitlab.com/balki/ytui/pubsub"
)
//go:embed templates/index.html
@ -24,12 +25,11 @@ var page string
const port = 8080
var (
ytdlCmd = []string{"youtube-dl"}
videosPath = "./vids"
videosUrl = "/vids"
cachePath = "./cache"
dbPath = "./db.json"
approval bool = false
ytdlCmd = []string{"youtube-dl"}
videosPath = "./vids"
videosUrl = "/vids"
cachePath = "./cache"
dbPath = "./db.json"
)
var d *db.Db
@ -41,7 +41,6 @@ func parse() {
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(&dbPath, "dbpath", dbPath, "Path where downloaded info is saved")
flag.BoolVar(&approval, "approval", approval, "Is approval required before allowing to watch")
flag.Parse()
if ytcmd != ytdlCmd[0] {
ytdlCmd = strings.Fields(ytcmd)
@ -68,12 +67,6 @@ func main() {
log.Panic(err)
}
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) {
@ -89,18 +82,15 @@ func main() {
w.WriteHeader(http.StatusInternalServerError)
return
}
if _, ok := seen[yturl]; !ok {
seen[yturl] = struct{}{}
id := d.Add(db.Item{
Date: time.Now().Format("2006-01-02 15:04"),
URL: yturl,
Title: "Loading",
Approved: false,
Status: db.NotStarted,
})
id, isNew := d.Add(db.Item{
Date: time.Now().Format("2006-01-02 15:04"),
URL: yturl,
Title: "Loading",
Status: db.NotStarted,
})
if isNew {
go getTitle(id, yturl)
go download(id, yturl)
}
http.Redirect(w, r, "/", http.StatusSeeOther)
})
@ -123,18 +113,43 @@ func getTitle(id int, yturl string) {
}
func download(id int, yturl string) {
pt := pubsub.NewProgressTracker()
d.Update(id, true, func(i *db.Item) {
i.Status = db.InProgress
i.Pt = pt
})
pc, err := pt.Publish()
defer close(pc)
if err != nil {
log.Panic(err)
}
var status db.DownloadStatus
var fname string
if fname, err = downloadYt(id, yturl, pc); err != nil {
status = db.Error
} else {
status = db.Done
}
d.Update(id, true, func(i *db.Item) {
i.Status = status
i.FileName = fname
})
}
func downloadYt(id int, yturl string, pc chan<- string) (string, error) {
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)
pc <- "Pre starting error"
return "", err
}
defer rc.Close()
pc <- "Starting download"
if err := cmd.Start(); err != nil {
log.Panic(err)
pc <- "Start error"
return "", err
}
br := bufio.NewReader(rc)
for {
@ -142,33 +157,31 @@ func download(id int, yturl string) {
if err != nil {
break
}
d.Update(id, false, func(i *db.Item) {
i.Progress = string(line)
})
pc <- string(line)
}
rc.Close()
pc <- "Waiting to complete..."
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)
}
pc <- "Download Error"
return "", fmt.Errorf("Download Error, err: %w", err)
}
d.Update(id, true, func(i *db.Item) {
i.Status = status
i.FileName = fname
})
pc <- "Download Done, renaming"
matches, err := fs.Glob(os.DirFS(cachePath), fmt.Sprintf("video_%d.*", id))
if err != nil {
pc <- "Match Error"
return "", fmt.Errorf("Glob match error, err: %w\n", err)
}
if len(matches) != 1 {
pc <- "Multiple Match Error"
return "", fmt.Errorf("Got multiple matches, count: %v", len(matches))
}
fname = matches[0]
source := path.Join(cachePath, fname)
destination := path.Join(videosPath, fname)
if err := os.Rename(source, destination); err != nil {
pc <- "Rename error"
return "", fmt.Errorf("Rename error, fname: %q, source: %q, destination: %q, err: %w\n", fname, source, destination, err)
}
return fname, nil
}

View File

@ -29,8 +29,6 @@
<td>
{{ if eq .Status "Done" }}
<a target="_blank" href="{{ vids_prefix }}/{{ .FileName }}">Watch</a>
{{ else if eq .Status "InProgress" }}
{{ .Progress }}
{{ else }}
{{ .Status }}
{{ end }}