Skip to content

Commit

Permalink
Added date to description
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-pierce committed Sep 13, 2021
1 parent 8b6ac93 commit fdbbedb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package main

import (
"billboard-scraper/auth"
authorizeUser "billboard-scraper/auth"
"billboard-scraper/playlist-builder"
"billboard-scraper/scraper"
)

func main() {
// Get list of songs with songName + FIRST artist name
songList := scraper.GetSongList()
songList, weekOf := scraper.GetSongList()

// Authorize user for Spotify
client, ctx := authorizeUser.AuthUser()

// Build and create playlist on user's account
playlist.BuildPlaylist(client, ctx, songList)
playlist.BuildPlaylist(client, ctx, songList, weekOf)
}
11 changes: 7 additions & 4 deletions playlist-builder/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package playlist
import (
"context"
"fmt"
"github.com/zmb3/spotify/v2"
"log"

"github.com/zmb3/spotify/v2"
)

func BuildPlaylist(client *spotify.Client, ctx context.Context, songList []string) {
func BuildPlaylist(client *spotify.Client, ctx context.Context, songList []string, weekOf string) {
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("Logged in!")

fmt.Println("Getting songs...")
for _, songName := range songList {
Expand All @@ -29,7 +30,9 @@ func BuildPlaylist(client *spotify.Client, ctx context.Context, songList []strin
}

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)
descStr := fmt.Sprintf("Billboard Top 100 songs, compiled by a Golang application for the week of %s", weekOf)

newPlaylist, err := client.CreatePlaylistForUser(ctx, user.ID, "Top 100 (according to Golang)", descStr, true, false)
if err != nil {
log.Fatal(err)
}
Expand Down
14 changes: 9 additions & 5 deletions scraper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import (
"log"
"net/http"
"regexp"
"strings"

"github.com/PuerkitoBio/goquery"
)

// Returns the main list of songs
func GetSongList() []string {
func GetSongList() ([]string, string) {
htmlDoc, err := GetHTML()

if err != nil {
log.Fatalln(err)
}
songs := GetSongs(htmlDoc)
return songs
songs, weekOf := GetSongs(htmlDoc)
return songs, weekOf
}

// Makes request to Billboard website, returns html document that can be parsed
Expand All @@ -36,9 +37,12 @@ func GetHTML() (*goquery.Document, error) {
}

// Parses document for songs, returns slice of song title + first artist name
func GetSongs(doc *goquery.Document) []string {
func GetSongs(doc *goquery.Document) ([]string, string) {
var songList []string

weekOf := doc.Find(".date-selector__wrapper").Find("div").Find("button").Text()
weekOf = strings.TrimSpace(weekOf)

doc.Find(".chart-list__element .display--flex").Each(func(i int, s *goquery.Selection) {
songTitle := s.Find(".chart-element__information__song").Text()
songArtist := s.Find(".chart-element__information__artist").Text()
Expand All @@ -50,5 +54,5 @@ func GetSongs(doc *goquery.Document) []string {
songInfo := songTitle + " " + firstArtist
songList = append(songList, songInfo)
})
return songList
return songList, weekOf
}

0 comments on commit fdbbedb

Please sign in to comment.