From 7ea1ba4b5cc85e0be3d9a1adaf86401cabdc0576 Mon Sep 17 00:00:00 2001 From: Superredstone Date: Fri, 13 Feb 2026 15:57:56 +0100 Subject: [PATCH] style: clean url recognition --- pkg/download.go | 11 ++++++----- pkg/utils.go | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 pkg/utils.go diff --git a/pkg/download.go b/pkg/download.go index c7698ac..b8a9c26 100644 --- a/pkg/download.go +++ b/pkg/download.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "strconv" - "strings" "github.com/Superredstone/spotiflac-cli/app" ) @@ -19,7 +18,10 @@ func Download(application *app.App, url string, output_folder string) error { output_folder = DEFAULT_DOWNLOAD_OUTPUT_FOLDER } - if strings.Contains(url, "https://open.spotify.com/track") { + url_type := GetUrlType(url) + + switch url_type { + case UrlTypeTrack: metadata, err := GetMetadata[MetadataSong](application, url) if err != nil { return err @@ -40,10 +42,9 @@ func Download(application *app.App, url string, output_folder string) error { _, err = application.DownloadTrack(downloadRequest) return err - } else if strings.Contains(url, "https://open.spotify.com/playlist") { + case UrlTypePlaylist: metadata, err := GetMetadata[MetadataPlaylist](application, url) if err != nil { - fmt.Println("Unable to fetch metadata for song " + url) return err } @@ -70,5 +71,5 @@ func Download(application *app.App, url string, output_folder string) error { return nil } - return errors.New("Invalid Spotify URL.") + return errors.New("Invalid URL.") } diff --git a/pkg/utils.go b/pkg/utils.go new file mode 100644 index 0000000..9cce6f2 --- /dev/null +++ b/pkg/utils.go @@ -0,0 +1,25 @@ +package pkg + +import ( + "strings" +) + +type UrlType int + +const ( + UrlTypeTrack UrlType = iota + UrlTypePlaylist + UrlTypeInvalid +) + +func GetUrlType(url string) UrlType { + if strings.Contains(url, "https://open.spotify.com/track") { + return UrlTypeTrack + } + + if strings.Contains(url, "https://open.spotify.com/playlist") { + return UrlTypePlaylist + } + + return UrlTypeInvalid +}