Skip to content

[QUESTION] How to get multiple/single progressbars for concurrent processes ? #196

Description

@AYehia0

I am trying to create one bar or multiple ones for the length of the download list, I am using goroutines to download the list really fast.
as follows :
Inside the main

if soundData.Kind == "playlist" {
	var wg sync.WaitGroup
	plDownloadTracks := getPlaylistDownloadTracks(soundData, clientId)

	for _, dlT := range plDownloadTracks {

		wg.Add(1)

		go func(dlT []soundcloud.DownloadTrack) {
			defer wg.Done()
			t := getTrack(dlT, true)
			fp := soundcloud.Download(t, downloadPath)

			// silent indication of already existing files
			if fp == "" {
				return
			}
			soundcloud.AddMetadata(t, fp)

		}(dlT)
	}
	wg.Wait()

	fmt.Printf("\n%s Playlist saved to : %s\n", theme.Green("[-]"), theme.Magenta(downloadPath))
	return
}

and here is my Download function :

func Download(track DownloadTrack, dlpath string) string {
	trackName := track.SoundData.Title + "[" + track.Quality + "]." + track.Ext
	path := validateDownload(dlpath, trackName)

	resp, err := http.Get(track.Url)

	if err != nil {
		return ""
	}
	defer resp.Body.Close()

	// check if the file exists
	f, _ := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
	defer f.Close()

        // here I am using the progress bar from "github.com/schollz/progressbar/v3" but it doesn't support pools
	bar := bar.DefaultBytes(
		resp.ContentLength,
		"Downloading",
	)

	io.Copy(io.MultiWriter(f, bar), resp.Body)

	return path
}

Sorry for such a question but I searched the docs and I found nothing, I would really appreciate your help 😃

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions