Skip to content

Commit

Permalink
feat: introduce --library-limit flag
Browse files Browse the repository at this point in the history
  • Loading branch information
streambinder committed Sep 6, 2024
1 parent e41e525 commit 46895a5
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
4 changes: 3 additions & 1 deletion cmd/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func cmdLookup() *cobra.Command {
library, _ := cmd.Flags().GetBool("library")
random, _ := cmd.Flags().GetBool("random")
randomSize, _ := cmd.Flags().GetInt("random-size")
libraryLimit, _ := cmd.Flags().GetInt("library-limit")
if !library && !random && len(args) == 0 {
return errors.New("no track has been issued")
}
Expand All @@ -56,7 +57,7 @@ func cmdLookup() *cobra.Command {
return
}
} else if library {
if err := client.Library(providerChannel, lyricsChannel); err != nil {
if err := client.Library(libraryLimit, providerChannel, lyricsChannel); err != nil {
ch <- err
return
}
Expand Down Expand Up @@ -103,5 +104,6 @@ func cmdLookup() *cobra.Command {
cmd.Flags().BoolP("library", "l", false, "Lookup personal library tracks")
cmd.Flags().BoolP("random", "r", false, "Lookup random tracks")
cmd.Flags().Int("random-size", defaultRandomSize, "Number of random tracks to load")
cmd.Flags().Int("library-limit", 0, "Number of tracks to fetch from library (unlimited if 0)")
return cmd
}
12 changes: 6 additions & 6 deletions cmd/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestCmdLookup(t *testing.T) {
ApplyFunc(spotify.Authenticate, func() (*spotify.Client, error) {
return &spotify.Client{}, nil
}).
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, ch ...chan interface{}) error {
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, _ int, ch ...chan interface{}) error {
ch[0] <- _track
ch[1] <- _track
return nil
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestCmdLookupLibraryFailure(t *testing.T) {
ApplyFunc(spotify.Authenticate, func() (*spotify.Client, error) {
return &spotify.Client{}, nil
}).
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, ch ...chan interface{}) error {
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, _ int, ch ...chan interface{}) error {
return errors.New("ko")
}).
Reset()
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestCmdLookupSearchFailure(t *testing.T) {
ApplyFunc(spotify.Authenticate, func() (*spotify.Client, error) {
return &spotify.Client{}, nil
}).
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, ch ...chan interface{}) error {
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, _ int, ch ...chan interface{}) error {
ch[0] <- _track
ch[1] <- _track
return nil
Expand All @@ -180,7 +180,7 @@ func TestCmdLookupSearchNotFound(t *testing.T) {
ApplyFunc(spotify.Authenticate, func() (*spotify.Client, error) {
return &spotify.Client{}, nil
}).
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, ch ...chan interface{}) error {
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, _ int, ch ...chan interface{}) error {
ch[0] <- _track
ch[1] <- _track
return nil
Expand All @@ -205,7 +205,7 @@ func TestCmdLookupLyricsFailure(t *testing.T) {
ApplyFunc(spotify.Authenticate, func() (*spotify.Client, error) {
return &spotify.Client{}, nil
}).
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, ch ...chan interface{}) error {
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, _ int, ch ...chan interface{}) error {
ch[0] <- _track
ch[1] <- _track
return nil
Expand All @@ -230,7 +230,7 @@ func TestCmdLookupLyricsNotFound(t *testing.T) {
ApplyFunc(spotify.Authenticate, func() (*spotify.Client, error) {
return &spotify.Client{}, nil
}).
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, ch ...chan interface{}) error {
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, _ int, ch ...chan interface{}) error {
ch[0] <- _track
ch[1] <- _track
return nil
Expand Down
8 changes: 5 additions & 3 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func cmdSync() *cobra.Command {
albums, _ = cmd.Flags().GetStringArray("album")
tracks, _ = cmd.Flags().GetStringArray("track")
fixes, _ = cmd.Flags().GetStringArray("fix")
libraryLimit, _ = cmd.Flags().GetInt("library-limit")
)

for index, path := range fixes {
Expand All @@ -80,7 +81,7 @@ func cmdSync() *cobra.Command {
return nursery.RunConcurrently(
routineIndex,
routineAuth,
routineFetch(library, playlists, playlistsTracks, albums, tracks, fixes),
routineFetch(library, playlists, playlistsTracks, albums, tracks, fixes, libraryLimit),
routineDecide(manual),
routineCollect,
routineProcess,
Expand Down Expand Up @@ -127,6 +128,7 @@ func cmdSync() *cobra.Command {
cmd.Flags().StringArrayP("album", "a", []string{}, "Synchronize album")
cmd.Flags().StringArrayP("track", "t", []string{}, "Synchronize track")
cmd.Flags().StringArrayP("fix", "f", []string{}, "Fix local track")
cmd.Flags().Int("library-limit", 0, "Number of tracks to fetch from library (unlimited if 0)")
return cmd
}

Expand Down Expand Up @@ -170,7 +172,7 @@ func routineAuth(ctx context.Context, ch chan error) {

// fetcher pulls data from the upstream
// provider, i.e. Spotify
func routineFetch(library bool, playlists, playlistsTracks, albums, tracks, fixes []string) func(ctx context.Context, ch chan error) {
func routineFetch(library bool, playlists, playlistsTracks, albums, tracks, fixes []string, libraryLimit int) func(ctx context.Context, ch chan error) {
return func(ctx context.Context, ch chan error) {
// remember to stop passing data to decider and mixer
defer close(routineQueues[routineTypeDecide])
Expand All @@ -197,7 +199,7 @@ func routineFetch(library bool, playlists, playlistsTracks, albums, tracks, fixe

if library {
tui.Lot("fetch").Printf("library")
if err := spotifyClient.Library(routineQueues[routineTypeDecide], fetched); err != nil {
if err := spotifyClient.Library(libraryLimit, routineQueues[routineTypeDecide], fetched); err != nil {
ch <- err
return
}
Expand Down
20 changes: 10 additions & 10 deletions cmd/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestCmdSync(t *testing.T) {
ApplyFunc(spotify.Authenticate, func() (*spotify.Client, error) {
return &spotify.Client{}, nil
}).
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, ch ...chan interface{}) error {
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, _ int, ch ...chan interface{}) error {
for _, c := range ch {
c <- _track
c <- _track // to trigger duplicate check
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestCmdSyncOfflineIndex(t *testing.T) {
ApplyFunc(spotify.Authenticate, func() (*spotify.Client, error) {
return &spotify.Client{}, nil
}).
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, ch ...chan interface{}) error {
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, _ int, ch ...chan interface{}) error {
ch[0] <- _track
return nil
}).
Expand Down Expand Up @@ -409,7 +409,7 @@ func TestCmdSyncDecideManual(t *testing.T) {
return &spotify.Client{}, nil
}).
ApplyMethod(&spotify.Client{}, "Library",
func(_ *spotify.Client, ch ...chan interface{}) error {
func(_ *spotify.Client, _ int, ch ...chan interface{}) error {
ch[0] <- _track
return nil
}).
Expand Down Expand Up @@ -437,7 +437,7 @@ func TestCmdSyncDecideFailure(t *testing.T) {
return &spotify.Client{}, nil
}).
ApplyMethod(&spotify.Client{}, "Library",
func(_ *spotify.Client, ch ...chan interface{}) error {
func(_ *spotify.Client, _ int, ch ...chan interface{}) error {
ch[0] <- _track
return nil
}).
Expand Down Expand Up @@ -468,7 +468,7 @@ func TestCmdSyncDecideNotFound(t *testing.T) {
return &spotify.Client{}, nil
}).
ApplyMethod(&spotify.Client{}, "Library",
func(_ *spotify.Client, ch ...chan interface{}) error {
func(_ *spotify.Client, _ int, ch ...chan interface{}) error {
ch[0] <- _track
return nil
}).
Expand Down Expand Up @@ -499,7 +499,7 @@ func TestCmdSyncCollectFailure(t *testing.T) {
return &spotify.Client{}, nil
}).
ApplyMethod(&spotify.Client{}, "Library",
func(_ *spotify.Client, ch ...chan interface{}) error {
func(_ *spotify.Client, _ int, ch ...chan interface{}) error {
ch[0] <- _track
return nil
}).
Expand Down Expand Up @@ -541,7 +541,7 @@ func TestCmdSyncDownloadFailure(t *testing.T) {
ApplyFunc(spotify.Authenticate, func() (*spotify.Client, error) {
return &spotify.Client{}, nil
}).
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, ch ...chan interface{}) error {
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, _ int, ch ...chan interface{}) error {
ch[0] <- _track
return nil
}).
Expand Down Expand Up @@ -580,7 +580,7 @@ func TestCmdSyncLyricsFailure(t *testing.T) {
ApplyFunc(spotify.Authenticate, func() (*spotify.Client, error) {
return &spotify.Client{}, nil
}).
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, ch ...chan interface{}) error {
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, _ int, ch ...chan interface{}) error {
ch[0] <- _track
return nil
}).
Expand Down Expand Up @@ -619,7 +619,7 @@ func TestCmdSyncProcessorFailure(t *testing.T) {
ApplyFunc(spotify.Authenticate, func() (*spotify.Client, error) {
return &spotify.Client{}, nil
}).
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, ch ...chan interface{}) error {
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, _ int, ch ...chan interface{}) error {
ch[0] <- _track
return nil
}).
Expand Down Expand Up @@ -661,7 +661,7 @@ func TestCmdSyncInstallerFailure(t *testing.T) {
ApplyFunc(spotify.Authenticate, func() (*spotify.Client, error) {
return &spotify.Client{}, nil
}).
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, ch ...chan interface{}) error {
ApplyMethod(&spotify.Client{}, "Library", func(_ *spotify.Client, _ int, ch ...chan interface{}) error {
ch[0] <- _track
return nil
}).
Expand Down
6 changes: 5 additions & 1 deletion spotify/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/zmb3/spotify/v2"
)

func (client *Client) Library(channels ...chan interface{}) error {
func (client *Client) Library(limit int, channels ...chan interface{}) error {
var (
ctx = context.Background()
library, err = client.CurrentUsersTracks(ctx)
Expand All @@ -16,12 +16,16 @@ func (client *Client) Library(channels ...chan interface{}) error {
return err
}

ctr := 0
for {
for _, libraryTrack := range library.Tracks {
track := trackEntity(&libraryTrack.FullTrack)
for _, ch := range channels {
ch <- track
}
if ctr++; limit > 0 && ctr >= limit {
return nil
}
}

if err := client.NextPage(ctx, library); errors.Is(err, spotify.ErrNoMorePages) {
Expand Down
8 changes: 4 additions & 4 deletions spotify/library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestLibrary(t *testing.T) {
Reset()

// testing
assert.Nil(t, testClient().Library())
assert.Nil(t, testClient().Library(0))
}

func TestLibraryChannel(t *testing.T) {
Expand All @@ -50,7 +50,7 @@ func TestLibraryChannel(t *testing.T) {
// testing
channel := make(chan interface{}, 1)
defer close(channel)
err := testClient().Library(channel)
err := testClient().Library(1, channel)
assert.Nil(t, err)
assert.Equal(t, library.Tracks[0].Name, ((<-channel).(*entity.Track)).Title)
}
Expand All @@ -65,7 +65,7 @@ func TestLibraryFailure(t *testing.T) {
Reset()

// testing
assert.EqualError(t, util.ErrOnly(testClient().Library()), "ko")
assert.EqualError(t, util.ErrOnly(testClient().Library(0)), "ko")
}

func TestLibraryNextPageFailure(t *testing.T) {
Expand All @@ -84,5 +84,5 @@ func TestLibraryNextPageFailure(t *testing.T) {
Reset()

// testing
assert.True(t, errors.Is(util.ErrOnly(client.Library()), syscall.ECONNREFUSED))
assert.True(t, errors.Is(util.ErrOnly(client.Library(0)), syscall.ECONNREFUSED))
}

0 comments on commit 46895a5

Please sign in to comment.