Skip to content

Commit

Permalink
feat(lyrics): expose Get API as well as Search
Browse files Browse the repository at this point in the history
  • Loading branch information
streambinder committed Nov 24, 2023
1 parent 03e943b commit bfde965
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 7 deletions.
30 changes: 30 additions & 0 deletions lyrics/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var composers = []Composer{}

type Composer interface {
search(*entity.Track, ...context.Context) ([]byte, error)
get(string, ...context.Context) ([]byte, error)
}

// not found entries return no error
Expand Down Expand Up @@ -60,3 +61,32 @@ func Search(track *entity.Track) (string, error) {

return string(result), os.WriteFile(track.Path().Lyrics(), result, 0o644)
}

func Get(url string) (string, error) {
var (
workers []nursery.ConcurrentJob
result []byte
ctxBackground = context.Background()
ctx, ctxCancel = context.WithCancel(ctxBackground)
)
defer ctxCancel()

for _, composer := range composers {
workers = append(workers, func(c Composer) func(context.Context, chan error) {
return func(ctx context.Context, ch chan error) {
scopedLyrics, err := c.get(url, ctx)
if err != nil {
ch <- err
return
}

if len(scopedLyrics) > len(result) {
result = scopedLyrics
ctxCancel()
}
}
}(composer))
}

return string(result), nursery.RunConcurrentlyWithContext(ctx, workers...)
}
35 changes: 35 additions & 0 deletions lyrics/composer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,38 @@ func TestSearchCannotCreateDir(t *testing.T) {
// testing
assert.EqualError(t, util.ErrOnly(Search(track)), "ko")
}

func TestGet(t *testing.T) {
// monkey patching
ch := make(chan bool, 1)
defer gomonkey.NewPatches().
ApplyPrivateMethod(reflect.TypeOf(genius{}), "get", func() ([]byte, error) {
close(ch)
return []byte("glyrics"), nil
}).
ApplyPrivateMethod(reflect.TypeOf(lyricsOvh{}), "get", func() ([]byte, error) {
<-ch
return []byte("olyrics"), nil
}).
Reset()

// testing
lyrics, err := Get("http://localhost")
assert.Nil(t, err)
assert.Equal(t, "glyrics", lyrics)
}

func TestGetFailure(t *testing.T) {
// monkey patching
defer gomonkey.NewPatches().
ApplyPrivateMethod(reflect.TypeOf(genius{}), "get", func() ([]byte, error) {
return nil, errors.New("ko")
}).
ApplyPrivateMethod(reflect.TypeOf(lyricsOvh{}), "get", func() ([]byte, error) {
return nil, errors.New("ko")
}).
Reset()

// testing
assert.EqualError(t, util.ErrOnly(Get("http://localhost")), "ko")
}
11 changes: 8 additions & 3 deletions lyrics/genius.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,15 @@ func (composer genius) search(track *entity.Track, ctxs ...context.Context) ([]b
track, context.WithValue(ctx, contextValueLabel(contextValueLabelMainArtist), true))
}

return composer.fromGeniusURL(url, ctx)
return composer.get(url, ctx)
}

func (composer genius) fromGeniusURL(url string, ctx context.Context) ([]byte, error) {
func (composer genius) get(url string, ctxs ...context.Context) ([]byte, error) {
ctx := context.Background()
if len(ctxs) > 0 {
ctx = ctxs[0]
}

request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
Expand All @@ -139,7 +144,7 @@ func (composer genius) fromGeniusURL(url string, ctx context.Context) ([]byte, e

if response.StatusCode == 429 {
util.SleepUntilRetry(response.Header)
return composer.fromGeniusURL(url, ctx)
return composer.get(url, ctx)
} else if response.StatusCode != 200 {
return nil, errors.New("cannot fetch lyrics on genius: " + response.Status)
}
Expand Down
2 changes: 1 addition & 1 deletion lyrics/genius_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestGeniusLyricsNewRequestFailure(t *testing.T) {
}).Reset()

// testing
assert.EqualError(t, util.ErrOnly(genius{}.fromGeniusURL("http://genius.com/test", context.Background())), "ko")
assert.EqualError(t, util.ErrOnly(genius{}.get("http://genius.com/test", context.Background())), "ko")
}

func TestGeniusLyricsNewRequestContextCanceled(t *testing.T) {
Expand Down
15 changes: 12 additions & 3 deletions lyrics/lyricsovh.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,18 @@ func (composer lyricsOvh) search(track *entity.Track, ctxs ...context.Context) (
ctx = ctxs[0]
}

request, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("https://api.lyrics.ovh/v1/%s/%s",
return composer.get(fmt.Sprintf("https://api.lyrics.ovh/v1/%s/%s",
url.QueryEscape(track.Artists[0]),
url.QueryEscape(track.Title)), nil)
url.QueryEscape(track.Title)), ctx)
}

func (composer lyricsOvh) get(url string, ctxs ...context.Context) ([]byte, error) {
ctx := context.Background()
if len(ctxs) > 0 {
ctx = ctxs[0]
}

request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
Expand All @@ -50,7 +59,7 @@ func (composer lyricsOvh) search(track *entity.Track, ctxs ...context.Context) (
return nil, nil
} else if response.StatusCode == 429 {
util.SleepUntilRetry(response.Header)
return composer.search(track, ctx)
return composer.get(url, ctx)
} else if response.StatusCode != 200 {
return nil, errors.New("cannot fetch results on lyrics.ovh: " + response.Status)
}
Expand Down

0 comments on commit bfde965

Please sign in to comment.