Skip to content

Commit

Permalink
feat: add album data scraper
Browse files Browse the repository at this point in the history
  • Loading branch information
ghoshRitesh12 committed Jun 5, 2024
1 parent 8f2af67 commit 0941b13
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions parsers/album.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package parsers

import (
"strings"
"sync"

"github.com/ghoshRitesh12/brooktube/helpers"
"github.com/ghoshRitesh12/brooktube/models/album"
"github.com/ghoshRitesh12/brooktube/requests"
"github.com/ghoshRitesh12/brooktube/utils"
)

const ALBUM_SCRAPE_OPERATIONS int = 2

func (p *YTMusicAPI) GetAlbum(albumId string) (*album.ScrapedData, error) {
wg := &sync.WaitGroup{}
result := &album.ScrapedData{}
albumBrowseId := ""

if strings.HasPrefix(albumId, "MPREb_") {
albumBrowseId = albumId
} else {
browseId, err := helpers.GetAlbumBrowseId(albumId)
if err != nil {
return nil, err
}
albumBrowseId = browseId
}

if albumBrowseId == "" {
return nil, utils.ErrInvalidAlbumBrowseId
}

data, err := requests.FetchAlbum(albumBrowseId)
if err != nil {
return nil, err
}

outerContents := data.Contents.
SingleColumnBrowseResultsRenderer.Tabs[0].
TabRenderer.Content.SectionListRenderer.Contents
if len(outerContents) < 1 {
return nil, utils.ErrAlbumContentsNotFound
}

sections := &(outerContents[0].MusicShelfRenderer.Contents)

if len(*sections) == 0 {
return result, nil
}

wg.Add(ALBUM_SCRAPE_OPERATIONS)

go result.ScrapeAndSetBasicInfo(wg, &data.Header)
go result.Songs.ScrapeAndSet(wg, sections)

wg.Wait()

return result, nil
}

0 comments on commit 0941b13

Please sign in to comment.