Make ctx a variable

This commit is contained in:
aditya-K2
2023-05-24 23:26:23 +05:30
parent a7af989d93
commit f0046c374f
3 changed files with 35 additions and 35 deletions

View File

@@ -8,13 +8,13 @@ import (
) )
func AddTracksToPlaylist(playlistId spotify.ID, t ...spotify.ID) error { func AddTracksToPlaylist(playlistId spotify.ID, t ...spotify.ID) error {
_, err := Client.AddTracksToPlaylist(ctx(), playlistId, t...) _, err := Client.AddTracksToPlaylist(ctx, playlistId, t...)
return err return err
} }
func QueueTracks(ids ...spotify.ID) error { func QueueTracks(ids ...spotify.ID) error {
count := 0 count := 0
_ctx := ctx() _ctx := ctx
for _, id := range ids { for _, id := range ids {
if err := Client.QueueSong(_ctx, id); err != nil { if err := Client.QueueSong(_ctx, id); err != nil {
return errors.New(fmt.Sprintf("%s | Tracks Queued: %d", err.Error(), count)) return errors.New(fmt.Sprintf("%s | Tracks Queued: %d", err.Error(), count))

View File

@@ -39,7 +39,7 @@ type Playable interface {
} }
var ( var (
ctx = func() context.Context { return context.Background() } ctx = context.Background()
Client *spotify.Client Client *spotify.Client
playlistCache map[spotify.ID]*Playlist = make(map[spotify.ID]*Playlist) playlistCache map[spotify.ID]*Playlist = make(map[spotify.ID]*Playlist)
albumCache map[spotify.ID]*Album = make(map[spotify.ID]*Album) albumCache map[spotify.ID]*Album = make(map[spotify.ID]*Album)
@@ -48,7 +48,7 @@ var (
func GetPlaylist(playlistId spotify.ID) (*Playlist, chan error) { func GetPlaylist(playlistId spotify.ID) (*Playlist, chan error) {
c := make(chan error) c := make(chan error)
if fp, err := Client.GetPlaylist(ctx(), playlistId); err != nil { if fp, err := Client.GetPlaylist(ctx, playlistId); err != nil {
go func() { c <- err }() go func() { c <- err }()
return nil, c return nil, c
} else { } else {
@@ -60,7 +60,7 @@ func GetPlaylist(playlistId spotify.ID) (*Playlist, chan error) {
addTracks() addTracks()
go func() { go func() {
for page := 1; ; page++ { for page := 1; ; page++ {
if perr := Client.NextPage(ctx(), &fp.Tracks); perr == spotify.ErrNoMorePages { if perr := Client.NextPage(ctx, &fp.Tracks); perr == spotify.ErrNoMorePages {
c <- nil c <- nil
break break
} else if perr != nil { } else if perr != nil {
@@ -86,7 +86,7 @@ func GetPlaylist(playlistId spotify.ID) (*Playlist, chan error) {
func GetAlbum(albumID spotify.ID) (*Album, chan error) { func GetAlbum(albumID spotify.ID) (*Album, chan error) {
c := make(chan error) c := make(chan error)
if _, ok := albumCache[albumID]; !ok { if _, ok := albumCache[albumID]; !ok {
fa, err := Client.GetAlbum(ctx(), albumID) fa, err := Client.GetAlbum(ctx, albumID)
if err != nil { if err != nil {
go func() { c <- err }() go func() { c <- err }()
return nil, c return nil, c
@@ -98,7 +98,7 @@ func GetAlbum(albumID spotify.ID) (*Album, chan error) {
addTracks() addTracks()
go func() { go func() {
for page := 1; ; page++ { for page := 1; ; page++ {
if perr := Client.NextPage(ctx(), &fa.Tracks); perr == spotify.ErrNoMorePages { if perr := Client.NextPage(ctx, &fa.Tracks); perr == spotify.ErrNoMorePages {
c <- nil c <- nil
break break
} else if perr != nil { } else if perr != nil {
@@ -125,7 +125,7 @@ func CurrentUserSavedAlbums() (*SavedAlbums, chan error) {
c := make(chan error) c := make(chan error)
_a := make(SavedAlbums, 0) _a := make(SavedAlbums, 0)
albums := &_a albums := &_a
if sp, err := Client.CurrentUsersAlbums(ctx()); err != nil { if sp, err := Client.CurrentUsersAlbums(ctx); err != nil {
go func() { c <- err }() go func() { c <- err }()
return nil, c return nil, c
} else { } else {
@@ -135,7 +135,7 @@ func CurrentUserSavedAlbums() (*SavedAlbums, chan error) {
addAlbums() addAlbums()
go func() { go func() {
for page := 1; ; page++ { for page := 1; ; page++ {
if perr := Client.NextPage(ctx(), sp); perr == spotify.ErrNoMorePages { if perr := Client.NextPage(ctx, sp); perr == spotify.ErrNoMorePages {
c <- nil c <- nil
break break
} else if perr != nil { } else if perr != nil {
@@ -153,7 +153,7 @@ func CurrentUserPlaylists() (*UserPlaylists, chan error) {
c := make(chan error) c := make(chan error)
_p := make(UserPlaylists, 0) _p := make(UserPlaylists, 0)
playlists := &_p playlists := &_p
if spp, err := Client.CurrentUsersPlaylists(ctx()); err != nil { if spp, err := Client.CurrentUsersPlaylists(ctx); err != nil {
go func() { c <- err }() go func() { c <- err }()
return nil, c return nil, c
} else { } else {
@@ -163,7 +163,7 @@ func CurrentUserPlaylists() (*UserPlaylists, chan error) {
addPlaylists() addPlaylists()
go func() { go func() {
for page := 1; ; page++ { for page := 1; ; page++ {
if perr := Client.NextPage(ctx(), spp); perr == spotify.ErrNoMorePages { if perr := Client.NextPage(ctx, spp); perr == spotify.ErrNoMorePages {
c <- nil c <- nil
break break
} else if perr != nil { } else if perr != nil {
@@ -181,7 +181,7 @@ func CurrentUserSavedTracks() (*LikedSongs, chan error) {
c := make(chan error) c := make(chan error)
_p := make(LikedSongs, 0) _p := make(LikedSongs, 0)
playlists := &_p playlists := &_p
if ls, err := Client.CurrentUsersTracks(ctx()); err != nil { if ls, err := Client.CurrentUsersTracks(ctx); err != nil {
go func() { c <- err }() go func() { c <- err }()
return nil, c return nil, c
} else { } else {
@@ -191,7 +191,7 @@ func CurrentUserSavedTracks() (*LikedSongs, chan error) {
addTracks() addTracks()
go func() { go func() {
for page := 1; ; page++ { for page := 1; ; page++ {
if perr := Client.NextPage(ctx(), ls); perr == spotify.ErrNoMorePages { if perr := Client.NextPage(ctx, ls); perr == spotify.ErrNoMorePages {
c <- nil c <- nil
break break
} else if perr != nil { } else if perr != nil {
@@ -210,7 +210,7 @@ func CurrentUserFollowedArtists() (*FollowedArtists, chan error) {
// TODO: Check if this is the proper implementation // TODO: Check if this is the proper implementation
_a := make(FollowedArtists, 0) _a := make(FollowedArtists, 0)
artists := &_a artists := &_a
if ar, err := Client.CurrentUsersFollowedArtists(ctx()); err != nil { if ar, err := Client.CurrentUsersFollowedArtists(ctx); err != nil {
go func() { c <- err }() go func() { c <- err }()
return nil, c return nil, c
} else { } else {
@@ -225,7 +225,7 @@ func CurrentUserFollowedArtists() (*FollowedArtists, chan error) {
if len(ar.Artists) == 0 { if len(ar.Artists) == 0 {
c <- nil c <- nil
} }
if ar, err = Client.CurrentUsersFollowedArtists(ctx(), spotify.After(string(ap))); err != nil { if ar, err = Client.CurrentUsersFollowedArtists(ctx, spotify.After(string(ap))); err != nil {
if err == spotify.ErrNoMorePages { if err == spotify.ErrNoMorePages {
c <- nil c <- nil
break break
@@ -243,15 +243,15 @@ func CurrentUserFollowedArtists() (*FollowedArtists, chan error) {
} }
func RecentlyPlayed() ([]spotify.RecentlyPlayedItem, error) { func RecentlyPlayed() ([]spotify.RecentlyPlayedItem, error) {
return Client.PlayerRecentlyPlayedOpt(ctx(), &spotify.RecentlyPlayedOptions{Limit: 50}) return Client.PlayerRecentlyPlayedOpt(ctx, &spotify.RecentlyPlayedOptions{Limit: 50})
} }
func GetPlayerState() (*spotify.PlayerState, error) { func GetPlayerState() (*spotify.PlayerState, error) {
return Client.PlayerState(ctx()) return Client.PlayerState(ctx)
} }
func GetTopTracks() ([]spotify.FullTrack, error) { func GetTopTracks() ([]spotify.FullTrack, error) {
c, err := Client.CurrentUsersTopTracks(ctx(), spotify.Limit(topTracksLimit)) c, err := Client.CurrentUsersTopTracks(ctx, spotify.Limit(topTracksLimit))
if c != nil { if c != nil {
return c.Tracks, err return c.Tracks, err
} else { } else {
@@ -260,7 +260,7 @@ func GetTopTracks() ([]spotify.FullTrack, error) {
} }
func GetTopArtists() ([]spotify.FullArtist, error) { func GetTopArtists() ([]spotify.FullArtist, error) {
c, err := Client.CurrentUsersTopArtists(ctx(), spotify.Limit(topTracksLimit)) c, err := Client.CurrentUsersTopArtists(ctx, spotify.Limit(topTracksLimit))
if c != nil { if c != nil {
return c.Artists, err return c.Artists, err
} else { } else {
@@ -269,15 +269,15 @@ func GetTopArtists() ([]spotify.FullArtist, error) {
} }
func GetArtistTopTracks(artistID spotify.ID) ([]spotify.FullTrack, error) { func GetArtistTopTracks(artistID spotify.ID) ([]spotify.FullTrack, error) {
c, err := Client.CurrentUser(ctx()) c, err := Client.CurrentUser(ctx)
if err != nil { if err != nil {
return []spotify.FullTrack{}, err return []spotify.FullTrack{}, err
} }
return Client.GetArtistsTopTracks(ctx(), artistID, c.Country) return Client.GetArtistsTopTracks(ctx, artistID, c.Country)
} }
func GetArtistAlbums(artistID spotify.ID) ([]spotify.SimpleAlbum, error) { func GetArtistAlbums(artistID spotify.ID) ([]spotify.SimpleAlbum, error) {
c, err := Client.GetArtistAlbums(ctx(), artistID, albumtypes) c, err := Client.GetArtistAlbums(ctx, artistID, albumtypes)
if err != nil { if err != nil {
return []spotify.SimpleAlbum{}, err return []spotify.SimpleAlbum{}, err
} }
@@ -285,7 +285,7 @@ func GetArtistAlbums(artistID spotify.ID) ([]spotify.SimpleAlbum, error) {
} }
func Search(s string) (*spotify.SearchResult, error) { func Search(s string) (*spotify.SearchResult, error) {
return Client.Search(ctx(), s, return Client.Search(ctx, s,
spotify.SearchTypePlaylist| spotify.SearchTypePlaylist|
spotify.SearchTypeAlbum| spotify.SearchTypeAlbum|
spotify.SearchTypeTrack| spotify.SearchTypeTrack|
@@ -293,7 +293,7 @@ func Search(s string) (*spotify.SearchResult, error) {
} }
func UserDevices() ([]spotify.PlayerDevice, error) { func UserDevices() ([]spotify.PlayerDevice, error) {
return Client.PlayerDevices(ctx()) return Client.PlayerDevices(ctx)
} }
func TransferPlayback(deviceId spotify.ID) error { func TransferPlayback(deviceId spotify.ID) error {
@@ -301,10 +301,10 @@ func TransferPlayback(deviceId spotify.ID) error {
if err != nil { if err != nil {
return errors.New("Unable to get Current Player State!") return errors.New("Unable to get Current Player State!")
} }
err = Client.PauseOpt(ctx(), &spotify.PlayOptions{DeviceID: &s.Device.ID}) err = Client.PauseOpt(ctx, &spotify.PlayOptions{DeviceID: &s.Device.ID})
return Client.TransferPlayback(ctx(), deviceId, true) return Client.TransferPlayback(ctx, deviceId, true)
} }
func GetFullPlaylist(id spotify.ID) (*spotify.FullPlaylist, error) { func GetFullPlaylist(id spotify.ID) (*spotify.FullPlaylist, error) {
return Client.GetPlaylist(ctx(), id) return Client.GetPlaylist(ctx, id)
} }

View File

@@ -8,7 +8,7 @@ import (
) )
func play(options *spotify.PlayOptions) error { func play(options *spotify.PlayOptions) error {
return Client.PlayOpt(ctx(), options) return Client.PlayOpt(ctx, options)
} }
func PlaySong(uri spotify.URI) error { func PlaySong(uri spotify.URI) error {
@@ -38,16 +38,16 @@ func PlayContext(context spotify.URI) error {
} }
func TogglePlayback() error { func TogglePlayback() error {
p, err := Client.PlayerCurrentlyPlaying(ctx()) p, err := Client.PlayerCurrentlyPlaying(ctx)
if err != nil { if err != nil {
return err return err
} }
if p.Playing { if p.Playing {
if err := Client.Pause(ctx()); err != nil { if err := Client.Pause(ctx); err != nil {
return err return err
} }
} else { } else {
if err := Client.Play(ctx()); err != nil { if err := Client.Play(ctx); err != nil {
return err return err
} }
} }
@@ -63,11 +63,11 @@ func UriToID(uri spotify.URI) (spotify.ID, error) {
} }
func Next() error { func Next() error {
return Client.Next(ctx()) return Client.Next(ctx)
} }
func Previous() error { func Previous() error {
return Client.Previous(ctx()) return Client.Previous(ctx)
} }
func Shuffle() error { func Shuffle() error {
@@ -77,7 +77,7 @@ func Shuffle() error {
return err return err
} }
return Client.Shuffle(ctx(), !s.ShuffleState) return Client.Shuffle(ctx, !s.ShuffleState)
} }
func Repeat() error { func Repeat() error {
@@ -92,5 +92,5 @@ func Repeat() error {
return err return err
} }
return Client.Repeat(ctx(), next[s.RepeatState]) return Client.Repeat(ctx, next[s.RepeatState])
} }