Channel based implementation for spt/get.go wrappers

This commit is contained in:
aditya-K2
2023-04-26 03:08:39 +05:30
parent 60cfcde37a
commit b0a7c9cdb0

View File

@@ -46,16 +46,11 @@ var (
PageContinue = errors.New("CONTINUE")
)
// GetPlaylist retrieves a Spotify playlist by its ID and returns a
// Playlist object. If the playlist is already cached, it returns cached object.
// If not cached or if the snapshotID for the playlist changes, it retrieves
// the playlist from Spotify API and caches it. It uses a background go routine
// to fetch all pages of tracks for the playlist and appends them to the tracks
// list in the Playlist object. When done, it passes the error value to the
// error handler.
func GetPlaylist(playlistId spotify.ID, errHandler func(error)) (*Playlist, error) {
func GetPlaylist(playlistId spotify.ID) (*Playlist, chan error) {
c := make(chan error)
if fp, err := Client.GetPlaylist(ctx(), playlistId); err != nil {
return nil, err
go func() { c <- err }()
return nil, c
} else {
if _, ok := playlistCache[fp.ID]; !ok || playlistCache[fp.ID].SnapshotID != fp.SnapshotID {
tracks := &[]spotify.PlaylistTrack{}
@@ -66,10 +61,10 @@ func GetPlaylist(playlistId spotify.ID, errHandler func(error)) (*Playlist, erro
go func() {
for page := 1; ; page++ {
if perr := Client.NextPage(ctx(), &fp.Tracks); perr == spotify.ErrNoMorePages {
errHandler(nil)
c <- nil
break
} else if perr != nil {
errHandler(perr)
c <- perr
return
}
addTracks()
@@ -81,25 +76,20 @@ func GetPlaylist(playlistId spotify.ID, errHandler func(error)) (*Playlist, erro
tracks,
}
playlistCache[fp.ID] = p
return p, nil
} else {
errHandler(nil)
return playlistCache[fp.ID], nil
return p, c
}
go func() { c <- nil }()
return playlistCache[fp.ID], c
}
}
// GetAlbum retrieves a Spotify album by its ID and returns an Album object.
// If the album is already cached, it returns the cached object.
// If not, it retrieves the album from the Spotify API and caches it.
// It uses a background go routine to fetch all pages of tracks for the album
// and appends them to the tracks list in the Album object.
// When done, it passes the error value to the error handler.
func GetAlbum(albumID spotify.ID, errHandler func(error)) (*Album, error) {
func GetAlbum(albumID spotify.ID) (*Album, chan error) {
c := make(chan error)
if _, ok := albumCache[albumID]; !ok {
fa, err := Client.GetAlbum(ctx(), albumID)
if err != nil {
return nil, err
go func() { c <- err }()
return nil, c
}
tracks := &[]spotify.SimpleTrack{}
addTracks := func() {
@@ -109,10 +99,10 @@ func GetAlbum(albumID spotify.ID, errHandler func(error)) (*Album, error) {
go func() {
for page := 1; ; page++ {
if perr := Client.NextPage(ctx(), &fa.Tracks); perr == spotify.ErrNoMorePages {
errHandler(nil)
c <- nil
break
} else if perr != nil {
errHandler(perr)
c <- perr
return
}
addTracks()
@@ -123,23 +113,21 @@ func GetAlbum(albumID spotify.ID, errHandler func(error)) (*Album, error) {
tracks,
}
albumCache[fa.ID] = p
return p, nil
return p, c
} else {
errHandler(nil)
return albumCache[albumID], nil
go func() { c <- nil }()
return albumCache[albumID], c
}
}
// CurrentUserSavedAlbums returns the SavedAlbums of the current user in a
// specific manner. It returns the first page and then starts a go routine
// in the background and keeps updating the SavedAlbums
// When done, it passes the error value to the error handler.
func CurrentUserSavedAlbums(errHandler func(err error)) (*SavedAlbums, error) {
func CurrentUserSavedAlbums() (*SavedAlbums, chan error) {
c := make(chan error)
_a := make(SavedAlbums, 0)
albums := &_a
if sp, err := Client.CurrentUsersAlbums(ctx()); err != nil {
return nil, err
go func() { c <- err }()
return nil, c
} else {
addAlbums := func() {
_a = append(_a, sp.Albums...)
@@ -148,28 +136,26 @@ func CurrentUserSavedAlbums(errHandler func(err error)) (*SavedAlbums, error) {
go func() {
for page := 1; ; page++ {
if perr := Client.NextPage(ctx(), sp); perr == spotify.ErrNoMorePages {
errHandler(nil)
c <- nil
break
} else if perr != nil {
errHandler(perr)
c <- perr
return
}
addAlbums()
}
}()
return albums, nil
return albums, c
}
}
// CurrentUserPlaylists returns the UserPlaylists of the current user in a
// specific manner. It returns the first page and then starts a go routine in
// the background and keeps updating the UserPlaylists
// When done, it passes the error value to the error handler.
func CurrentUserPlaylists(errHandler func(err error)) (*UserPlaylists, error) {
func CurrentUserPlaylists() (*UserPlaylists, chan error) {
c := make(chan error)
_p := make(UserPlaylists, 0)
playlists := &_p
if spp, err := Client.CurrentUsersPlaylists(ctx()); err != nil {
return nil, err
go func() { c <- err }()
return nil, c
} else {
addPlaylists := func() {
_p = append(_p, spp.Playlists...)
@@ -178,28 +164,26 @@ func CurrentUserPlaylists(errHandler func(err error)) (*UserPlaylists, error) {
go func() {
for page := 1; ; page++ {
if perr := Client.NextPage(ctx(), spp); perr == spotify.ErrNoMorePages {
errHandler(nil)
c <- nil
break
} else if perr != nil {
errHandler(perr)
c <- perr
return
}
addPlaylists()
}
}()
return playlists, nil
return playlists, c
}
}
// CurrentUserSavedTracks returns the LikedSongs of the current user in a
// specific manner. It returns the first page and then starts a go routine in
// the background and keeps updating the LikedSongs
// When done, it passes the error value to the error handler.
func CurrentUserSavedTracks(errHandler func(err error)) (*LikedSongs, error) {
func CurrentUserSavedTracks() (*LikedSongs, chan error) {
c := make(chan error)
_p := make(LikedSongs, 0)
playlists := &_p
if ls, err := Client.CurrentUsersTracks(ctx()); err != nil {
return nil, err
go func() { c <- err }()
return nil, c
} else {
addTracks := func() {
_p = append(_p, ls.Tracks...)
@@ -208,25 +192,27 @@ func CurrentUserSavedTracks(errHandler func(err error)) (*LikedSongs, error) {
go func() {
for page := 1; ; page++ {
if perr := Client.NextPage(ctx(), ls); perr == spotify.ErrNoMorePages {
errHandler(nil)
c <- nil
break
} else if perr != nil {
errHandler(perr)
c <- perr
return
}
addTracks()
}
}()
return playlists, nil
return playlists, c
}
}
func CurrentUserFollowedArtists(errHandler func(error)) (*FollowedArtists, error) {
func CurrentUserFollowedArtists() (*FollowedArtists, chan error) {
c := make(chan error)
// TODO: Check if this is the proper implementation
_a := make(FollowedArtists, 0)
artists := &_a
if ar, err := Client.CurrentUsersFollowedArtists(ctx()); err != nil {
return artists, err
go func() { c <- err }()
return nil, c
} else {
ap := spotify.ID("")
addArtists := func() {
@@ -237,14 +223,14 @@ func CurrentUserFollowedArtists(errHandler func(error)) (*FollowedArtists, error
go func() {
for {
if len(ar.Artists) == 0 {
errHandler(nil)
c <- nil
}
if ar, err = Client.CurrentUsersFollowedArtists(ctx(), spotify.After(string(ap))); err != nil {
if err == spotify.ErrNoMorePages {
errHandler(nil)
c <- nil
break
} else {
errHandler(err)
c <- err
break
}
} else {
@@ -252,7 +238,7 @@ func CurrentUserFollowedArtists(errHandler func(error)) (*FollowedArtists, error
}
}
}()
return artists, nil
return artists, c
}
}