From fca7803f4f6d0c1040968950cc66a5833b1db217 Mon Sep 17 00:00:00 2001 From: Kevin Pierce Date: Tue, 7 Sep 2021 22:38:36 -0400 Subject: [PATCH] Now correctly scrapes all 100 songs off of Billboard Top 100 --- auth/auth.go | 4 +++- main.go | 4 ++++ playlist-builder/playlist.go | 32 ++++++++++++++++---------------- scraper/scraper.go | 2 +- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index 79f41a2..d3d68ab 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -6,7 +6,6 @@ import ( "github.com/zmb3/spotify/v2/auth" "log" "net/http" - // "os" "github.com/joho/godotenv" "github.com/zmb3/spotify/v2" @@ -20,6 +19,8 @@ var ( state = "abc123" ) +// Authentication for Spotify +// Adapted from https://github.com/zmb3/spotify/blob/master/examples/authenticate/authcode/authenticate.go func AuthUser() (*spotify.Client, context.Context) { err := godotenv.Load() if err != nil { @@ -50,6 +51,7 @@ func AuthUser() (*spotify.Client, context.Context) { return client, context.Background() } +// Auth helper func completeAuth(w http.ResponseWriter, r *http.Request) { tok, err := auth.Token(r.Context(), state, r) if err != nil { diff --git a/main.go b/main.go index 9d86659..b1aabbd 100644 --- a/main.go +++ b/main.go @@ -7,8 +7,12 @@ import ( ) func main() { + // Get list of songs with songName + FIRST artist name songList := scraper.GetSongList() + // Authorize user for Spotify client, ctx := authorizeUser.AuthUser() + + // Build and create playlist on user's account playlist.BuildPlaylist(client, ctx, songList) } diff --git a/playlist-builder/playlist.go b/playlist-builder/playlist.go index 59735ce..4309d75 100644 --- a/playlist-builder/playlist.go +++ b/playlist-builder/playlist.go @@ -8,14 +8,14 @@ import ( ) func BuildPlaylist(client *spotify.Client, ctx context.Context, songList []string) { - fmt.Println(songList) - var uriList []spotify.URI + var uriList []spotify.ID user, err := client.CurrentUser(ctx) if err != nil { log.Fatal(err) } fmt.Println("You are logged in as:", user.ID) + fmt.Println("Getting songs...") for _, songName := range songList { result, err := client.Search(ctx, songName, spotify.SearchTypeTrack) if err != nil { @@ -23,23 +23,23 @@ func BuildPlaylist(client *spotify.Client, ctx context.Context, songList []strin } if len(result.Tracks.Tracks) > 0 { - songURI := result.Tracks.Tracks[0].SimpleTrack.URI + songURI := result.Tracks.Tracks[0].SimpleTrack.ID uriList = append(uriList, songURI) } } - fmt.Println(uriList) - - // ///result, err := client.Search(ctx, songList[0], spotify.SearchTypeTrack) - // if err != nil { - // log.Fatal(err) - // } - - // fmt.Println(result.Tracks.Tracks[0].SimpleTrack.URI) + fmt.Println("Creating playlist...") + newPlaylist, err := client.CreatePlaylistForUser(ctx, user.ID, "Top 100 (according to Golang)", "Billboard Top 100 songs, compiled by a Golang application", true, false) + if err != nil { + log.Fatal(err) + } + newPlaylistID := newPlaylist.SimplePlaylist.ID - // newPlaylist, err := client.CreatePlaylistForUser(context.Background(), user.ID, "TEST GOLANG", "Test for my golang application", true, false) - // if err != nil { - // log.Fatal(err) - // } - // fmt.Println(newPlaylist) + version, err := client.AddTracksToPlaylist(ctx, newPlaylistID, uriList...) + if err != nil { + log.Fatal(err) + } else { + fmt.Println("Succesfully created playlist!") + fmt.Println(version) + } } diff --git a/scraper/scraper.go b/scraper/scraper.go index 5e1bd26..6edb05e 100644 --- a/scraper/scraper.go +++ b/scraper/scraper.go @@ -44,7 +44,7 @@ func GetSongs(doc *goquery.Document) []string { songArtist := s.Find(".chart-element__information__artist").Text() // Only search for first artists in list of artists - splitExp := regexp.MustCompile(`&|Featuring| X `) + splitExp := regexp.MustCompile(`&|Featuring| X | x`) firstArtist := splitExp.Split(songArtist, -1)[0] songInfo := songTitle + " " + firstArtist