yt-mdl/downloader.go

134 lines
2.9 KiB
Go
Raw Normal View History

2023-08-20 11:22:32 +02:00
package main
import (
"os/exec"
"strings"
"time"
"github.com/jedib0t/go-pretty/v6/progress"
)
const YT_DLP string = "yt-dlp"
const YT_DLP_FLAG_GET_TITLE string = "--get-title"
const JOB_STATUS_NEW = 1
const JOB_STATUS_COMPLETED = 2
const JOB_STATUS_ERR = 3
type DownloadCtx struct {
link string
title string
progress uint8
status uint8
tracker *progress.Tracker
err *error
}
func Download(links *[]string) {
// Start and manage goroutines here
downloadCtxsChan := make(chan *DownloadCtx)
var jobs []DownloadCtx
2023-08-20 11:22:32 +02:00
startedGoRoutines := 0
pw := progress.NewWriter()
pw.SetStyle(progress.StyleDefault)
2023-08-20 11:22:32 +02:00
pw.SetTrackerLength(10)
pw.SetNumTrackersExpected(len(*links))
pw.SetUpdateFrequency(time.Millisecond * 100)
pw.Style().Colors = progress.StyleColorsExample
pw.SetTrackerPosition(progress.PositionLeft)
2023-08-20 11:22:32 +02:00
pw.Style().Visibility.ETA = false
pw.Style().Visibility.ETAOverall = false
2023-08-20 11:22:32 +02:00
pw.Style().Visibility.Speed = false
pw.Style().Visibility.Percentage = false
pw.Style().Visibility.Value = false
pw.Style().Visibility.TrackerOverall = true
pw.Style().Options.TimeInProgressPrecision = time.Second
pw.Style().Options.TimeDonePrecision = time.Second
2023-08-20 11:22:32 +02:00
go pw.Render()
for _, val := range *links {
ctx := createDownloadCtx(val)
jobs = append(jobs, *ctx)
2023-08-20 11:22:32 +02:00
pw.AppendTracker(ctx.tracker)
go startJob(ctx, downloadCtxsChan)
2023-08-20 11:22:32 +02:00
startedGoRoutines++
}
for i := 0; i < startedGoRoutines; i++ {
ctx := <- downloadCtxsChan
if ctx.err != nil {
2023-08-20 11:22:32 +02:00
ctx.tracker.MarkAsErrored()
} else {
ctx.tracker.MarkAsDone()
}
2023-08-20 11:22:32 +02:00
}
time.Sleep(time.Millisecond * 100)
2023-08-20 11:22:32 +02:00
pw.Stop()
for pw.IsRenderInProgress() {}
2023-08-20 11:22:32 +02:00
}
func createDownloadCtx(link string) *DownloadCtx {
2023-08-20 11:22:32 +02:00
var ctx DownloadCtx
ctx.tracker = &progress.Tracker{}
ctx.tracker.SetValue(0)
ctx.tracker.Message = link
ctx.tracker.Units = progress.UnitsDefault
ctx.link = link
ctx.status = JOB_STATUS_NEW
return &ctx
2023-08-20 11:22:32 +02:00
}
func startJob(ctx *DownloadCtx, downloadCtxs chan *DownloadCtx) {
2023-08-20 11:22:32 +02:00
getTitle(ctx)
if ctx.err != nil {
downloadCtxs <- ctx
2023-08-20 11:22:32 +02:00
return
}
ctx.tracker.UpdateMessage(ctx.title)
ytdlpDownload(ctx, downloadCtxs)
2023-08-20 11:22:32 +02:00
downloadCtxs <- ctx
}
func getTitle(ctx *DownloadCtx) {
2023-08-20 11:22:32 +02:00
cmd := exec.Command(YT_DLP, YT_DLP_FLAG_GET_TITLE, ctx.link)
stdout, err := cmd.Output()
if err != nil {
ctx.err = &err
ctx.status = JOB_STATUS_ERR
return
}
title := string(stdout)
title = strings.TrimSpace(title)
ctx.title = title
}
func ytdlpDownload(
ctx *DownloadCtx, downloadCtxs chan *DownloadCtx) {
2023-08-20 11:22:32 +02:00
cmd := exec.Command(YT_DLP, ctx.link)
_, err := cmd.Output()
2023-08-20 11:22:32 +02:00
if err != nil {
ctx.err = &err
ctx.status = JOB_STATUS_ERR
downloadCtxs <- ctx
2023-08-20 11:22:32 +02:00
return
}
ctx.status = JOB_STATUS_COMPLETED
2023-08-20 11:22:32 +02:00
}