From 9727d52a8bc5e12ab69cab84704d71b399ac346b Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Sat, 8 Apr 2023 22:10:05 +0530 Subject: [PATCH] add more wrapper functions --- gspotify/get.go | 131 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 110 insertions(+), 21 deletions(-) diff --git a/gspotify/get.go b/gspotify/get.go index f1c80dd..325b4c4 100644 --- a/gspotify/get.go +++ b/gspotify/get.go @@ -2,25 +2,36 @@ package gspotify import ( "context" + "errors" "github.com/zmb3/spotify/v2" ) type Playlist struct { SnapshotID string + ID spotify.ID Tracks []spotify.PlaylistTrack } -type Playable struct { - ID spotify.ID - Name string - Type string +type Album struct { + spotify.FullAlbum + Tracks []spotify.SimpleTrack +} + +type SavedAlbums []spotify.SavedAlbum +type UserPlaylists []spotify.SimplePlaylist + +type Playable interface { + Type() string + Uri() } var ( - playlistCache map[spotify.ID]*Playlist = make(map[spotify.ID]*Playlist) + ctx = func() context.Context { return context.Background() } Client *spotify.Client - ctx = context.Background() + playlistCache map[spotify.ID]*Playlist = make(map[spotify.ID]*Playlist) + albumCache map[spotify.ID]*Album = make(map[spotify.ID]*Album) + PageContinue = errors.New("CONTINUE") ) func getPlaylistTracks(trackPage *spotify.PlaylistTrackPage) ([]spotify.PlaylistTrack, error) { @@ -30,7 +41,24 @@ func getPlaylistTracks(trackPage *spotify.PlaylistTrackPage) ([]spotify.Playlist } addTracks() for page := 1; ; page++ { - if perr := Client.NextPage(ctx, trackPage); perr == spotify.ErrNoMorePages { + if perr := Client.NextPage(ctx(), trackPage); perr == spotify.ErrNoMorePages { + break + } else if perr != nil { + return nil, perr + } + addTracks() + } + return tracks, nil +} + +func getAlbumTracks(trackPage *spotify.SimpleTrackPage) ([]spotify.SimpleTrack, error) { + tracks := make([]spotify.SimpleTrack, 0) + addTracks := func() { + tracks = append(tracks, trackPage.Tracks...) + } + addTracks() + for page := 1; ; page++ { + if perr := Client.NextPage(ctx(), trackPage); perr == spotify.ErrNoMorePages { break } else if perr != nil { return nil, perr @@ -41,7 +69,7 @@ func getPlaylistTracks(trackPage *spotify.PlaylistTrackPage) ([]spotify.Playlist } func GetPlaylist(playlistId spotify.ID) (*Playlist, error) { - if fp, err := Client.GetPlaylist(ctx, playlistId); err != nil { + if fp, err := Client.GetPlaylist(ctx(), playlistId); err != nil { return nil, err } else { if _, ok := playlistCache[fp.ID]; !ok || playlistCache[fp.ID].SnapshotID != fp.SnapshotID { @@ -49,8 +77,9 @@ func GetPlaylist(playlistId spotify.ID) (*Playlist, error) { return nil, err } else { p := &Playlist{ - SnapshotID: fp.SnapshotID, - Tracks: tracks, + fp.SnapshotID, + fp.ID, + tracks, } playlistCache[fp.ID] = p return p, nil @@ -61,23 +90,83 @@ func GetPlaylist(playlistId spotify.ID) (*Playlist, error) { } } -func CurrentUserAllPlaylists() ([]spotify.SimplePlaylist, error) { - playlists := make([]spotify.SimplePlaylist, 0) - if spage, err := Client.CurrentUsersPlaylists(ctx); err != nil { +func GetAlbum(albumID spotify.ID) (*Album, error) { + if fa, err := Client.GetAlbum(ctx(), albumID); err != nil { + return nil, err + } else { + if _, ok := albumCache[fa.ID]; !ok { + if tracks, err := getAlbumTracks(&fa.Tracks); err != nil { + return nil, err + } else { + p := &Album{ + *fa, + tracks, + } + albumCache[fa.ID] = p + return p, nil + } + } else { + return albumCache[fa.ID], nil + } + } +} + +// CurrentUserSavedAlbums Returns the SavedAlbums in a very specific manner. +// It returns the first page and then starts a go routine in the background +// and keeps updating the SavedAlbums and sends nil to the provided channel +// if successful else sends the corresponding error. +func CurrentUserSavedAlbums(done func(status bool, err error)) (*SavedAlbums, error) { + _a := make(SavedAlbums, 0) + albums := &_a + if sp, err := Client.CurrentUsersAlbums(ctx()); err != nil { + return nil, err + } else { + addAlbums := func() { + _a = append(_a, sp.Albums...) + } + addAlbums() + go func() { + for page := 1; ; page++ { + if perr := Client.NextPage(ctx(), sp); perr == spotify.ErrNoMorePages { + done(true, nil) + break + } else if perr != nil { + done(false, perr) + break + } + addAlbums() + } + }() + return albums, nil + } +} + +// CurrentUserPlaylists Returns the UserPlaylists in a very specific manner. +// It returns the first page and then starts a go routine in the background +// and keeps updating the UserPlaylists and sends nil to the provided channel +// if successful else sends the corresponding error. +func CurrentUserPlaylists(done func(status bool, err error)) (*UserPlaylists, error) { + _p := make(UserPlaylists, 0) + playlists := &_p + if spp, err := Client.CurrentUsersPlaylists(ctx()); err != nil { return nil, err } else { addPlaylists := func() { - playlists = append(playlists, spage.Playlists...) + _p = append(_p, spp.Playlists...) } addPlaylists() - for page := 1; ; page++ { - if perr := Client.NextPage(ctx, spage); perr == spotify.ErrNoMorePages { - break - } else if perr != nil { - return nil, perr + go func() { + for page := 1; ; page++ { + if perr := Client.NextPage(ctx(), spp); perr == spotify.ErrNoMorePages { + done(true, nil) + break + } else if perr != nil { + done(false, perr) + break + } + addPlaylists() } - addPlaylists() - } + }() return playlists, nil } }