downlaoder: remove err chan, stick to one stream. fixed logical errors

This commit is contained in:
HeshamTB 2023-08-20 17:25:29 +03:00
parent 766b4075dd
commit fe62c19dd9

View File

@ -28,52 +28,55 @@ type DownloadCtx struct {
func Download(links *[]string) { func Download(links *[]string) {
// Start and manage goroutines here // Start and manage goroutines here
// Now get titles only
downloadCtxsChan := make(chan *DownloadCtx) downloadCtxsChan := make(chan *DownloadCtx)
getTitleErrChan := make(chan *DownloadCtx) var jobs []DownloadCtx
startedGoRoutines := 0 startedGoRoutines := 0
pw := progress.NewWriter() pw := progress.NewWriter()
pw.SetStyle(progress.StyleDefault)
pw.SetTrackerLength(10) pw.SetTrackerLength(10)
pw.SetNumTrackersExpected(len(*links)) pw.SetNumTrackersExpected(len(*links))
pw.SetStyle(progress.StyleDefault)
pw.SetUpdateFrequency(time.Millisecond * 100) pw.SetUpdateFrequency(time.Millisecond * 100)
pw.Style().Colors = progress.StyleColorsExample
pw.SetTrackerPosition(progress.PositionLeft)
pw.Style().Visibility.ETA = false pw.Style().Visibility.ETA = false
pw.Style().Colors = progress.StyleColorsExample pw.Style().Visibility.ETAOverall = false
pw.Style().Visibility.Speed = false pw.Style().Visibility.Speed = false
pw.Style().Visibility.Percentage = false pw.Style().Visibility.Percentage = false
pw.Style().Visibility.Value = false pw.Style().Visibility.Value = false
pw.Style().Visibility.TrackerOverall = true pw.Style().Visibility.TrackerOverall = true
pw.Style().Options.TimeInProgressPrecision = time.Second
pw.Style().Options.TimeDonePrecision = time.Second
go pw.Render() go pw.Render()
for _, val := range *links { for _, val := range *links {
ctx := createDownloadCtx(val) ctx := createDownloadCtx(val)
jobs = append(jobs, *ctx)
pw.AppendTracker(ctx.tracker) pw.AppendTracker(ctx.tracker)
go startJob(&ctx, downloadCtxsChan, getTitleErrChan) go startJob(ctx, downloadCtxsChan)
startedGoRoutines++ startedGoRoutines++
} }
for startedGoRoutines > 0 { for i := 0; i < startedGoRoutines; i++ {
select { ctx := <- downloadCtxsChan
case ctx := <- downloadCtxsChan: if ctx.err != nil {
ctx.tracker.UpdateMessage(ctx.title)
startedGoRoutines--
case ctx := <- getTitleErrChan:
ctx.tracker.MarkAsErrored() ctx.tracker.MarkAsErrored()
startedGoRoutines-- } else {
} ctx.tracker.MarkAsDone()
}
} }
time.Sleep(time.Millisecond * 100)
pw.Stop() pw.Stop()
for pw.IsRenderInProgress() {} for pw.IsRenderInProgress() {}
} }
func createDownloadCtx(link string) DownloadCtx { func createDownloadCtx(link string) *DownloadCtx {
var ctx DownloadCtx var ctx DownloadCtx
ctx.tracker = &progress.Tracker{} ctx.tracker = &progress.Tracker{}
ctx.tracker.SetValue(0) ctx.tracker.SetValue(0)
@ -81,31 +84,25 @@ func createDownloadCtx(link string) DownloadCtx {
ctx.tracker.Units = progress.UnitsDefault ctx.tracker.Units = progress.UnitsDefault
ctx.link = link ctx.link = link
ctx.status = JOB_STATUS_NEW ctx.status = JOB_STATUS_NEW
return ctx return &ctx
} }
func startJob( func startJob(ctx *DownloadCtx, downloadCtxs chan *DownloadCtx) {
ctx *DownloadCtx, downloadCtxs chan *DownloadCtx, errs chan *DownloadCtx) {
getTitle(ctx) getTitle(ctx)
if ctx.err != nil { if ctx.err != nil {
errs <- ctx downloadCtxs <- ctx
return return
} }
ctx.tracker.UpdateMessage(ctx.title) ctx.tracker.UpdateMessage(ctx.title)
ytdlpDownload(ctx, downloadCtxs, errs) ytdlpDownload(ctx, downloadCtxs)
if ctx.err != nil {
errs <- ctx
return
}
downloadCtxs <- ctx downloadCtxs <- ctx
} }
func getTitle(
ctx *DownloadCtx) { func getTitle(ctx *DownloadCtx) {
cmd := exec.Command(YT_DLP, YT_DLP_FLAG_GET_TITLE, ctx.link) cmd := exec.Command(YT_DLP, YT_DLP_FLAG_GET_TITLE, ctx.link)
stdout, err := cmd.Output() stdout, err := cmd.Output()
@ -121,16 +118,16 @@ func getTitle(
} }
func ytdlpDownload( func ytdlpDownload(
ctx *DownloadCtx, downloadCtxs chan *DownloadCtx, errs chan *DownloadCtx) { ctx *DownloadCtx, downloadCtxs chan *DownloadCtx) {
cmd := exec.Command(YT_DLP, ctx.link) cmd := exec.Command(YT_DLP, ctx.link)
_, err := cmd.Output() _, err := cmd.Output()
if err != nil { if err != nil {
ctx.err = &err ctx.err = &err
ctx.status = JOB_STATUS_ERR ctx.status = JOB_STATUS_ERR
errs <- ctx downloadCtxs <- ctx
return return
} }
downloadCtxs <- ctx ctx.status = JOB_STATUS_COMPLETED
} }