Skip to content

Commit

Permalink
Throwing some ugly ass go concurrent to speed up the playlist parsing.
Browse files Browse the repository at this point in the history
- Added Yes?No prompt for downloading only the playlist.
- Added speeeeeed Yeahh boi.
  • Loading branch information
AYehia0 committed Jan 18, 2023
1 parent c124adc commit 8e6d231
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 9 deletions.
42 changes: 33 additions & 9 deletions internal/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package internal

import (
"fmt"
"strings"
"sync"

"github.com/AYehia0/soundcloud-dl/pkg/soundcloud"
"github.com/AYehia0/soundcloud-dl/pkg/theme"
Expand Down Expand Up @@ -48,20 +50,42 @@ func Sc(args []string, downloadPath string, bestQuality bool, search bool) {
return
}

fmt.Printf("%s Track found. Title : %s - Duration : %s\n", theme.Green("[+]"), theme.Magenta(soundData.Title), theme.Magenta(theme.FormatTime(soundData.Duration)))
fmt.Printf("%s %s found. Title : %s - Duration : %s\n", theme.Green("[+]"), strings.Title(soundData.Kind), theme.Magenta(soundData.Title), theme.Magenta(theme.FormatTime(soundData.Duration)))
}

list := soundcloud.GetFormattedDL(soundData, clientId)
// check if the url is a playlist
if soundData.Kind == "playlist" {
var wg sync.WaitGroup
plDownloadTracks := getPlaylistDownloadTracks(soundData, clientId)

// show available download options
qualities := getQualities(list)
if !bestQuality {
defaultQuality = chooseQuality(qualities)
} else {
defaultQuality = getHighestQuality(qualities)
for _, dlT := range plDownloadTracks {

wg.Add(1)

go func(dlT []soundcloud.DownloadTrack) {
defer wg.Done()
// bestQuality is true to avoid prompting the user for quality choosing each time and speed up
// TODO: get a single progress bar, this will require the use of "https://github.com/cheggaaa/pb" since the current pb doesn't support download pool (I think)
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
}

track := chooseTrackDownload(list, defaultQuality)
downloadTracks := soundcloud.GetFormattedDL(soundData, clientId)

track := getTrack(downloadTracks, bestQuality)
filePath := soundcloud.Download(track, downloadPath)

// add tags
Expand Down
62 changes: 62 additions & 0 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"fmt"
"os"
"sync"

"github.com/AYehia0/soundcloud-dl/pkg/soundcloud"
"github.com/AYehia0/soundcloud-dl/pkg/theme"
Expand Down Expand Up @@ -38,6 +39,8 @@ func chooseTrackDownload(tracks []soundcloud.DownloadTrack, target string) sound
return tracks[0]
}

// get all the available qualities inside the track
// used to choose a track to download based on the target quality
func getQualities(tracks []soundcloud.DownloadTrack) []string {
qualities := make([]string, 0)
for _, track := range tracks {
Expand All @@ -50,6 +53,10 @@ func getQualities(tracks []soundcloud.DownloadTrack) []string {
// prompt select quality, only if --best not passed
func chooseQuality(qualities []string) string {
fmt.Printf("%s Available Qualities :\n", theme.Green("[+]"))
if len(qualities) < 1 {
fmt.Printf("%s No qualities available to download!:\n", theme.Red("[-]"))
os.Exit(0)
}
prompt := promptui.Select{
Label: "Choose a quality:",
Items: qualities,
Expand Down Expand Up @@ -117,3 +124,58 @@ func getUserSearch() string {
return result

}

func getPlaylistDownloadTracks(soundData *soundcloud.SoundData, clientId string) [][]soundcloud.DownloadTrack {

var wg sync.WaitGroup
listDownloadTracks := make([][]soundcloud.DownloadTrack, 0)

playlistTracks := soundcloud.GetPlaylistTracks(soundData, clientId)

fmt.Printf("%s Playlist contains : %s track\n", theme.Green("[+]"), theme.Magenta(len(playlistTracks)))
if !promptYesNo() {
fmt.Printf("%s Exiting...\n", theme.Red("[-]"))
os.Exit(0)
}
for i, t := range playlistTracks {
wg.Add(1)

go func(t soundcloud.SoundData) {
defer wg.Done()
dlTrack := soundcloud.GetFormattedDL(&t, clientId)
listDownloadTracks = append(listDownloadTracks, dlTrack)
}(t)
fmt.Printf("%s %v - %s \n", theme.Green("[+]"), theme.Red(i+1), theme.Magenta(t.Title))
}
wg.Wait()
return listDownloadTracks
}

// get a final track to be downloaded
// if bestQuality is false it will prompt the user to choose a quality
func getTrack(downloadTracks []soundcloud.DownloadTrack, bestQuality bool) soundcloud.DownloadTrack {

// show available download options
qualities := getQualities(downloadTracks)
if !bestQuality {
defaultQuality = chooseQuality(qualities)
} else {
defaultQuality = getHighestQuality(qualities)
}

return chooseTrackDownload(downloadTracks, defaultQuality)

}

func promptYesNo() bool {
prompt := promptui.Select{
Label: "Download [Yes/No]",
Items: []string{"Yes", "No"},
}
_, result, err := prompt.Run()
if err != nil {
fmt.Printf("%s Download aborted!\n", theme.Red("[-]"))
os.Exit(0)
}
return result == "Yes"
}

0 comments on commit 8e6d231

Please sign in to comment.