Skip to content

Commit

Permalink
Now successfully scrapes and returns songs + artist names
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-pierce committed Sep 7, 2021
1 parent 74ccd01 commit 5f0a0ed
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions scraper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@ import (
"fmt"
"log"
"net/http"
"regexp"

"github.com/PuerkitoBio/goquery"
)

type LinkTag struct {
linkRef string
linkText string
}

func GetSongList() []string {
htmlDoc, err := GetHTML()
fmt.Println(htmlDoc)

if err != nil {
log.Fatalln(err)
}

links, err := GetLinks(htmlDoc)
return links
songs, err := GetSongs(htmlDoc)
fmt.Println(songs)
return songs
}

func GetHTML() (*goquery.Document, error) {
Expand All @@ -40,16 +36,21 @@ func GetHTML() (*goquery.Document, error) {
return doc, nil
}

func GetLinks(doc *goquery.Document) ([]string, error) {
func GetSongs(doc *goquery.Document) ([]string, error) {
var songList []string

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()
fmt.Println(songArtist)
fmt.Println(songTitle)
//fmt.Println(s)
})

// Only search for first artists in list of artists
splitExp := regexp.MustCompile(`&|Featuring| X `)
firstArtist := splitExp.Split(songArtist, -1)[0]

songInfo := songTitle + " " + firstArtist
//fmt.Println(songInfo)

songList = append(songList, songInfo)
})
return songList, nil
}

0 comments on commit 5f0a0ed

Please sign in to comment.