Skip to content

Commit

Permalink
Now correctly scrapes all 100 songs off of Billboard Top 100
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-pierce committed Sep 8, 2021
1 parent 1df4e9f commit fca7803
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
4 changes: 3 additions & 1 deletion auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/zmb3/spotify/v2/auth"
"log"
"net/http"
// "os"

"github.com/joho/godotenv"
"github.com/zmb3/spotify/v2"
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
32 changes: 16 additions & 16 deletions playlist-builder/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,38 @@ 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 {
log.Fatal(err)
}

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)
}
}
2 changes: 1 addition & 1 deletion scraper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fca7803

Please sign in to comment.