Skip to content

Commit

Permalink
Correctly handle MusicBrainz responses for multi-disc collections
Browse files Browse the repository at this point in the history
Fixes #37
Fixes #8
  • Loading branch information
sbooth committed Sep 17, 2020
1 parent 42e7a3f commit b18c2fe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions CompactDisc/CompactDiscDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,7 @@ - (void) updateMetadataFromMusicBrainz:(NSDictionary *)releaseDictionary
[self setArtist:[releaseDictionary valueForKey:@"artist"]];
[self setComposer:[releaseDictionary valueForKey:@"composer"]];
[self setDate:[releaseDictionary valueForKey:@"date"]];
[self setDiscNumber:[releaseDictionary valueForKey:@"position"]];
[self setMusicbrainzAlbumId:[releaseDictionary valueForKey:@"albumId"]];
[self setMusicbrainzArtistId:[releaseDictionary valueForKey:@"artistId"]];

Expand Down
17 changes: 16 additions & 1 deletion MusicBrainz/MusicBrainzHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ void PerformMusicBrainzQuery(NSString *discID, void (^completionHandler)(NSArray

NSError *err = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];
// NSLog(@"%@",json);

NSMutableArray *releaseArray = [NSMutableArray array];

Expand All @@ -65,6 +64,22 @@ void PerformMusicBrainzQuery(NSString *discID, void (^completionHandler)(NSArray

NSArray *media = [release objectForKey:@"media"];
for(NSDictionary *medium in media) {
// Multi-disc releases contain information on all discs in the release, so
// filter out media that don't have matching disc IDs
BOOL mediumHasMatchingDiscID = NO;
NSArray *discs = [medium objectForKey:@"discs"];
for(NSDictionary *disc in discs) {
if([discID isEqualToString:[disc objectForKey:@"id"]]) {
mediumHasMatchingDiscID = YES;
break;
}
}

if(!mediumHasMatchingDiscID)
continue;

[releaseDictionary setValue:[medium objectForKey:@"position"] forKey:@"position"];

NSArray *tracks = [medium objectForKey:@"tracks"];
for(NSDictionary *track in tracks) {
NSMutableDictionary *trackDictionary = [NSMutableDictionary dictionary];
Expand Down

0 comments on commit b18c2fe

Please sign in to comment.