mirror of
https://github.com/Superredstone/spotiflac-cli.git
synced 2026-03-07 20:18:07 +01:00
79 lines
1.6 KiB
Go
79 lines
1.6 KiB
Go
package lib
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
type UrlType int
|
|
|
|
const (
|
|
UrlTypeTrack UrlType = iota
|
|
UrlTypePlaylist
|
|
)
|
|
|
|
func ParseUrlType(url string) (UrlType, error) {
|
|
if strings.Contains(url, "https://open.spotify.com/track") {
|
|
return UrlTypeTrack, nil
|
|
}
|
|
|
|
if strings.Contains(url, "https://open.spotify.com/playlist") {
|
|
return UrlTypePlaylist, nil
|
|
}
|
|
|
|
return UrlTypeTrack, errors.New("Invalid URL, not a playlist nor a track.")
|
|
}
|
|
|
|
func ParseTrackId(url string) (string, error) {
|
|
tmp := strings.Split(url, "/")
|
|
|
|
if len(tmp) == 0 {
|
|
return "", errors.New("Invalid URL.")
|
|
}
|
|
|
|
tmp2 := strings.Split(tmp[len(tmp)-1], "?")
|
|
if len(tmp2) == 0 {
|
|
return tmp[len(tmp)-1], nil
|
|
}
|
|
|
|
return tmp2[0], nil
|
|
}
|
|
|
|
func BuildFileName(metadata TrackMetadata) (string, error) {
|
|
var result string
|
|
var artists string
|
|
|
|
firstArtistLen := len(metadata.Data.TrackUnion.FirstArtist.Items)
|
|
if firstArtistLen == 0 {
|
|
return result, errors.New("What? This should never happen.")
|
|
}
|
|
artists = metadata.Data.TrackUnion.FirstArtist.Items[firstArtistLen-1].Profile.Name
|
|
|
|
for _, artist := range(metadata.Data.TrackUnion.OtherArtists.Items) {
|
|
artists += ", " + artist.Profile.Name
|
|
}
|
|
|
|
result = fmt.Sprintf("%s - %s", metadata.Data.TrackUnion.Name, artists)
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func BuildFileOutput(outputFile string, fileName string, metadata TrackMetadata) (string, error) {
|
|
var result string
|
|
|
|
fileName, err := BuildFileName(metadata)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
|
|
if outputFile == "" {
|
|
result = path.Join(DEFAULT_DOWNLOAD_OUTPUT_FOLDER, fileName)
|
|
} else {
|
|
result = outputFile
|
|
}
|
|
|
|
return result, nil
|
|
}
|