From 1dd7cd37113ee7bbd1ff401e076dc13259b85da9 Mon Sep 17 00:00:00 2001 From: Lawrence Craft Date: Fri, 4 Mar 2016 00:56:04 +0000 Subject: [PATCH] Custom playlist name via command line Fulfills partial requirements for #2 --- main.go | 6 +++++- spotify.go | 10 ++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 745cf7d..73eac04 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "flag" "fmt" log "github.com/Sirupsen/logrus" "github.com/skratchdot/open-golang/open" @@ -124,6 +125,9 @@ func main() { os.Exit(1) } + str := flag.String("p", "", "Default playlist name") + flag.Parse() + url := os.Args[len(os.Args)-1] songs, err := GetShows(url) @@ -150,7 +154,7 @@ func main() { os.Exit(1) } - playlist, err := BuildPlaylist(client, songs, "GB") + playlist, err := BuildPlaylist(client, *str, songs, "GB") if err != nil { fmt.Println("Error building playlist:", err) diff --git a/spotify.go b/spotify.go index d5055ef..0e53104 100644 --- a/spotify.go +++ b/spotify.go @@ -93,7 +93,7 @@ func generateState() string { // BuildPlaylist creates a Spotify playlist and returns a playlist identifier. authedClient is // an authenticated Spotify client, and songs is a list of songs scraped from the Echoes website -func BuildPlaylist(authedClient *spotify.Client, songs []EchoesSong, market string) (string, error) { +func BuildPlaylist(authedClient *spotify.Client, playlistName string, songs []EchoesSong, market string) (string, error) { terms := echoesSongsToSearchStrings(songs) fmt.Println("Buildling playlist of", len(terms), "terms") results := make([]chan *spotify.SearchResult, 0, len(songs)) @@ -151,14 +151,16 @@ func BuildPlaylist(authedClient *spotify.Client, songs []EchoesSong, market stri } if len(tracks) > 0 { - return CreatePlaylist(authedClient, tracks) + return CreatePlaylist(authedClient, playlistName, tracks) } return "", errors.New("No tracks found") } // CreatePlaylist creates a new playlist given a set of tracks with a default name -func CreatePlaylist(authedClient *spotify.Client, tracks []spotify.FullTrack) (string, error) { - playlistName := generatePlaylistName(time.Now()) +func CreatePlaylist(authedClient *spotify.Client, playlistName string, tracks []spotify.FullTrack) (string, error) { + if playlistName == "" { + playlistName = generatePlaylistName(time.Now()) + } user, err := authedClient.CurrentUser() if err != nil {