Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions plugins/lyrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ type Lyrics struct {

type geniusSearchResponse struct {
Response struct {
Hits []struct {
Result geniusSong `json:"result"`
} `json:"hits"`
Hits []geniusSearchHit `json:"hits"`
} `json:"response"`
}

type geniusSearchHit struct {
// Genius returns the hit type beside the nested result object. Keep this
// at the hit level so song results are not mistaken for empty results.
Type string `json:"type"`
Result geniusSong `json:"result"`
}

type geniusSong struct {
Type string `json:"type"`
Title string `json:"title"`
FullTitle string `json:"full_title"`
ArtistNames string `json:"artist_names"`
Expand Down Expand Up @@ -230,7 +234,7 @@ func lookupGeniusSong(ctx context.Context, query, token string) (geniusSong, err
return geniusSong{}, err
}
for _, hit := range payload.Response.Hits {
if !strings.EqualFold(strings.TrimSpace(hit.Result.Type), "song") {
if !strings.EqualFold(strings.TrimSpace(hit.Type), "song") {
continue
}
if !validGeniusSongURL(hit.Result.URL) {
Expand Down
10 changes: 5 additions & 5 deletions plugins/lyrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func TestLookupGeniusSongUsesTokenAndSelectsSong(t *testing.T) {
t.Fatalf("Authorization = %q", got)
}
return newPluginResponse(http.StatusOK, `{"response":{"hits":[
{"result":{"type":"artist","title":"Artist","url":"https://genius.com/artists/artist"}},
{"result":{"type":"song","title":"Song","artist_names":"Artist","url":"https://genius.com/Artist-song-lyrics"}}
{"type":"artist","result":{"title":"Artist","url":"https://genius.com/artists/artist"}},
{"type":"song","result":{"title":"Song","artist_names":"Artist","url":"https://genius.com/Artist-song-lyrics"}}
]}}`), nil
})}

Expand All @@ -87,8 +87,8 @@ func TestLookupGeniusSongRejectsInvalidResults(t *testing.T) {
t.Cleanup(func() { apiHTTPClient = old })
apiHTTPClient = &http.Client{Transport: newPluginRoundTripper(func(*http.Request) (*http.Response, error) {
return newPluginResponse(http.StatusOK, `{"response":{"hits":[
{"result":{"type":"artist","title":"Artist","url":"https://genius.com/artists/artist"}},
{"result":{"type":"song","title":"Bad host","url":"https://evil.example/song"}}
{"type":"artist","result":{"title":"Artist","url":"https://genius.com/artists/artist"}},
{"type":"song","result":{"title":"Bad host","url":"https://evil.example/song"}}
]}}`), nil
})}
if _, err := lookupGeniusSong(t.Context(), "song", "token"); !errors.Is(err, errGeniusNotFound) {
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestLyricsHandleReturnsOneBoundedLine(t *testing.T) {
t.Cleanup(func() { apiHTTPClient = old })
t.Setenv("BOT_GENIUS_ACCESS_TOKEN", "test-token")
apiHTTPClient = &http.Client{Transport: newPluginRoundTripper(func(*http.Request) (*http.Response, error) {
return newPluginResponse(http.StatusOK, `{"response":{"hits":[{"result":{"type":"song","title":"Song\ufe0f","artist_names":"Artist\u200d","url":"https://genius.com/Artist-song-lyrics"}}]}}`), nil
return newPluginResponse(http.StatusOK, `{"response":{"hits":[{"type":"song","result":{"title":"Song\ufe0f","artist_names":"Artist\u200d","url":"https://genius.com/Artist-song-lyrics"}}]}}`), nil
})}

sent := make(chan bot.Outgoing, 2)
Expand Down