Skip to content

Commit

Permalink
Merge pull request #131 from brotherlogic/add_get_col_rel
Browse files Browse the repository at this point in the history
Supports collection release pull
  • Loading branch information
brotherlogic committed Jul 7, 2024
2 parents 439f08e + 3f2c5f3 commit b17d4be
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -43,7 +43,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -57,4 +57,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v2
1 change: 1 addition & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Discogs interface {
GetDiscogsUser(ctx context.Context) (*pb.User, error)

GetCollection(ctx context.Context, page int32) ([]*pb.Release, *pb.Pagination, error)
GetCollectionRelease(ctx context.Context, iid int64, page int32) ([]*pb.Release, *pb.Pagination, error)

GetUserId() int32

Expand Down
4 changes: 4 additions & 0 deletions testdata/build.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
mkdir -p users/brotherlogic/collection/releases/
curl -X GET -H "Content-Type:application/json" --user-agent "GoDiscogsTestData" "https://api.discogs.com//users/brotherlogic/collection/releases/550078?token=$1" | sed "s/$1/token/g" > users/brotherlogic/collection/releases/550078_page=1
exit


mkdir release
curl https://www.discogs.com/release/625928 > release/625928
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"pagination": {"page": 1, "pages": 1, "per_page": 50, "items": 1, "urls": {}}, "releases": [{"id": 550078, "instance_id": 19867159, "date_added": "2013-05-12T16:39:55-07:00", "rating": 3, "basic_information": {"id": 550078, "master_id": 1410454, "master_url": "https://api.discogs.com/masters/1410454", "resource_url": "https://api.discogs.com/releases/550078", "thumb": "https://i.discogs.com/f3WjcgnnM76Z8ZCIivZ5EyWbk2z4GBt3CXbI3sb6bwU/rs:fit/g:sm/q:40/h:150/w:150/czM6Ly9kaXNjb2dz/LWRhdGFiYXNlLWlt/YWdlcy9SLTU1MDA3/OC0xNDcwMDMzNzEz/LTUzNzEuanBlZw.jpeg", "cover_image": "https://i.discogs.com/ZZVcROhKgIriATLYE7JcInfOACeahSNecq89mzeCsfw/rs:fit/g:sm/q:90/h:601/w:600/czM6Ly9kaXNjb2dz/LWRhdGFiYXNlLWlt/YWdlcy9SLTU1MDA3/OC0xNDcwMDMzNzEz/LTUzNzEuanBlZw.jpeg", "title": "Tra La La / Stand Up And Be A Man", "year": 2002, "formats": [{"name": "Vinyl", "qty": "1", "descriptions": ["7\"", "45 RPM", "Reissue"]}], "artists": [{"name": "The 'Great' Deltas", "anv": "The \"Great\" Deltas", "join": "", "role": "", "tracks": "", "id": 386412, "resource_url": "https://api.discogs.com/artists/386412"}], "labels": [{"name": "Funk45", "catno": "FUNK45.005", "entity_type": "1", "entity_type_name": "Label", "id": 40206, "resource_url": "https://api.discogs.com/labels/40206"}], "genres": ["Funk / Soul"], "styles": ["Funk"]}, "folder_id": 1613206, "notes": [{"field_id": 1, "value": "Near Mint (NM or M-)"}, {"field_id": 2, "value": "Near Mint (NM or M-)"}, {"field_id": 5, "value": "2023-05-25"}, {"field_id": 6, "value": "2020-02-09"}, {"field_id": 8, "value": "7s"}, {"field_id": 10, "value": "NO_KEEP"}]}]}
10 changes: 10 additions & 0 deletions testdiscogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ func (t *TestDiscogsClient) AddCollectionRelease(r *pb.Release) {
t.collectionRecords = append(t.collectionRecords, r)
}

func (t *TestDiscogsClient) GetCollectionRelease(ctx context.Context, id int64, page int32) ([]*pb.Release, *pb.Pagination, error) {
var ret []*pb.Release
for _, r := range t.collectionRecords {
if r.Id == id {
ret = append(ret, r)
}
}
return ret, &pb.Pagination{}, nil
}

func (t *TestDiscogsClient) AddCNonollectionRelease(r *pb.Release) {
if t.collectionRecords == nil {
t.nonCollectionRecords = make([]*pb.Release, 0)
Expand Down
65 changes: 65 additions & 0 deletions usercollection.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,71 @@ func (d *prodClient) CreateFolder(ctx context.Context, folderName string) (*pb.F
}, nil
}

func (d *prodClient) GetCollectionRelease(ctx context.Context, id int64, page int32) ([]*pb.Release, *pb.Pagination, error) {
cr := &CollectionResponse{}
err := d.makeDiscogsRequest("GET", fmt.Sprintf("/users/%v/collection/releases/%v?page=%v", d.user.GetUsername(), id, page), "", "/users/uname/collection/folders/0/releases", cr)
if err != nil {
return nil, nil, err
}

if len(cr.Message) > 0 {
if strings.Contains(cr.Message, "is outside of valid range") {
return nil, nil, status.Errorf(codes.OutOfRange, cr.Message)
}
}

var rs []*pb.Release
for _, release := range cr.Releases {
r := &pb.Release{
InstanceId: int64(release.InstanceId),
Id: int64(release.Id),
FolderId: int32(release.FolderId),
Rating: int32(release.Rating),
Title: release.BasicInformation.Title,
Notes: make(map[int32]string),
}

for _, note := range release.Notes {
r.Notes[int32(note.FieldId)] = note.Value
}

var formats []*pb.Format
for _, form := range release.BasicInformation.Formats {
val, _ := strconv.ParseInt(form.Qty, 10, 32)
formats = append(formats, &pb.Format{
Name: form.Name,
Descriptions: form.Descriptions,
Quantity: int32(val),
})
}
r.Formats = formats

var labels []*pb.Label
for _, label := range release.BasicInformation.Labels {
labels = append(labels, &pb.Label{
Name: label.Name,
Catno: label.Catno,
Id: int32(label.Id),
})
}
r.Labels = labels

var artists []*pb.Artist
for _, artist := range release.BasicInformation.Artists {
artists = append(artists, &pb.Artist{
Name: artist.Name,
Id: int64(artist.Id),
})
}
r.Artists = artists

rs = append(rs, r)
}

return rs, &pb.Pagination{Page: int32(cr.Pagination.Page), Pages: int32(cr.Pagination.Pages)}, nil

}

func (d *prodClient) GetCollection(ctx context.Context, page int32) ([]*pb.Release, *pb.Pagination, error) {
cr := &CollectionResponse{}
err := d.makeDiscogsRequest("GET", fmt.Sprintf("/users/%v/collection/folders/0/releases?page=%v", d.user.GetUsername(), page), "", "/users/uname/collection/folders/0/releases", cr)
Expand Down
20 changes: 20 additions & 0 deletions usercollection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,23 @@ func TestDeleteFolder(t *testing.T) {
t.Fatalf("Error deleting folder: %v", err)
}
}

func TestGetCollectionRelease(t *testing.T) {
td := GetTestDiscogs()
r, _, err := td.GetCollectionRelease(context.Background(), 550078, 1)
if err != nil {
t.Fatalf("Unable to get release: %v", err)
}

if len(r) != 1 {
t.Fatalf("Bad pull: %v", len(r))
}

if r[0].GetTitle() != "Tra La La / Stand Up And Be A Man" {
t.Errorf("Bad title pull: %v", r[0])
}

if r[0].GetFolderId() != 1613206 {
t.Errorf("Bad folder id: %v", r[0])
}
}

0 comments on commit b17d4be

Please sign in to comment.