Skip to content

Commit

Permalink
Merge pull request #37 from tomjowitt/17-get-tracks-by-isrc
Browse files Browse the repository at this point in the history
17 get tracks by isrc
  • Loading branch information
tomjowitt authored Apr 6, 2024
2 parents 9c95d9d + d41c3cc commit 392c6a4
Show file tree
Hide file tree
Showing 6 changed files with 356 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ jobs:
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3.7.0
with:
version: v1.55
version: v1.57
install-mode: "goinstall"
37 changes: 34 additions & 3 deletions album.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
)
Expand Down Expand Up @@ -34,14 +33,17 @@ type AlbumResource struct {
ProviderInfo ProviderInfo `json:"providerInfo"`
}

// MediaMetaData represents the metadata of an album.
type MediaMetaData struct {
Tags []string `json:"tags"`
}

// AlbumProperties represents the properties of an album.
type AlbumProperties struct {
Content []string `json:"content"`
}

// ProviderInfo represents the provider of an album.
type ProviderInfo struct {
ID string `json:"providerId"`
Name string `json:"providerName"`
Expand Down Expand Up @@ -238,6 +240,7 @@ func (c *Client) GetSimilarAlbums(ctx context.Context, id string, params Paginat
return albumIDs, nil
}

// GetAlbumsByArtist returns a list of albums that match an artist ID.
func (c *Client) GetSingleTrack(ctx context.Context, id string) (*Track, error) {
response, err := c.request(ctx, http.MethodGet, concat("/tracks/", id), nil)
if err != nil {
Expand All @@ -251,7 +254,35 @@ func (c *Client) GetSingleTrack(ctx context.Context, id string) (*Track, error)
return nil, fmt.Errorf("failed to unmarshal the tracks response body: %w", err)
}

log.Println(result.Artists)

return &result, nil
}

// GetTracksByISRC returns a list of tracks that match an ISRC.
//
// ISRC lookup can be found here. This is a useful tool for finding ISRCs for testing purposes:
// https://isrcsearch.ifpi.org/
func (c *Client) GetTracksByISRC(ctx context.Context, isrc string, params PaginationParams) ([]Track, error) {
type isrcParams struct {
isrc string
Limit int
Offset int
}

response, err := c.request(ctx, http.MethodGet, "/tracks/byIsrc", isrcParams{
isrc: isrc,
Limit: params.Limit,
Offset: params.Offset,
})
if err != nil {
return nil, fmt.Errorf("failed to connect to the tracks endpoint: %w", err)
}

var result trackResults

err = json.Unmarshal(response, &result)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal the tracks response body: %w", err)
}

return result.Data, nil
}
50 changes: 50 additions & 0 deletions album_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,53 @@ func TestGetSingleTrack(t *testing.T) {
})
}
}

func TestGetTracksByISRC(t *testing.T) {
t.Parallel()

type args struct {
httpClient HTTPClient
id string
}

type expected struct {
count int
}

tests := []struct {
name string
args args
expected expected
}{
{
"Count of tracks by ISRC",
args{
httpClient: &mockHTTPClient{FilePath: "testdata/tracks-by-isrc.json", StatusCode: http.StatusOK},
id: "51584179",
},
expected{
count: 2,
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

c := &Client{
httpClient: tt.args.httpClient,
}

tracks, err := c.GetTracksByISRC(context.Background(), tt.args.id, PaginationParams{Limit: 5})
if err != nil {
t.Errorf("Client.GetTracksByISRC() error = %v", err)
return
}

if len(tracks) != tt.expected.count {
t.Errorf("Client.GetTracksByISRC() artist count %v, want %v", len(tracks), tt.expected.count)
}
})
}
}
13 changes: 13 additions & 0 deletions examples/track/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,17 @@ func main() {
log.Println("-------------------------------------------------")

log.Printf("%s - %s", track.Title, track.Artists[0].Name)

tracks, err := client.GetTracksByISRC(ctx, "GBBLY1600675", gotidal.PaginationParams{Limit: 5})
if err != nil {
log.Fatal(err)
}

log.Println("-------------------------------------------------")
log.Println("Tracks By ISRC")
log.Println("-------------------------------------------------")

for _, track := range tracks {
log.Printf("%s - %s - %s", track.Title, track.Artists[0].Name, track.Album.Title)
}
}
1 change: 1 addition & 0 deletions search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestClient_Search(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

c := &Client{}
_, err := c.Search(context.Background(), tt.args.params)

Expand Down
Loading

0 comments on commit 392c6a4

Please sign in to comment.