Work on recording id test

Lots of optional fields. Going to pare down the structs to just the fields I need, ignore the other pieces, and hopefully make a little more sense out of it that way. Might also make (almost) everything optional just to make it easy (in some respect)
This commit is contained in:
Samuel Webb 2025-03-10 23:23:16 -04:00
parent 1bcbad8f80
commit 4634838a25
5 changed files with 84 additions and 61 deletions

View file

@ -6,7 +6,7 @@ pub const Alias = struct {
name: []const u8,
locale: ?[]const u8 = null,
type: ?[]const u8 = null,
primary: ?[]const u8 = null,
primary: ?bool = null,
@"begin-date": ?[]const u8 = null,
//begin_date: []const u8,
@"end-date": ?[]const u8 = null,

View file

@ -23,17 +23,23 @@ pub const Recording = struct {
id: []const u8,
score: u8,
title: []const u8,
length: u64,
video: ?bool,
@"artist-credit": []ArtistCredit,
length: ?u64 = null,
video: ?bool = null,
@"artist-credit": ?[]ArtistCredit = null,
//artist_credit: []ArtistCredit,
//@"first-release-date": ?[]const u8 = null,
@"first-release-date": ?union(enum) { slice: []const u8, dt: zeit.Date } = null,
@"first-release-date": ?[]const u8 = null,
dt: ?i64 = null,
//@"first-release-date": ?union(enum) { date: []const u8, dt: zeit.Date } = null,
//first_release_date: []const u8,
releases: []Release,
releases: ?[]Release = null,
isrcs: ?[][]const u8 = null,
tags: ?[]Tag = null,
disambiguation: ?[]const u8 = null,
pub fn lessThan(context: void, a: Recording, b: Recording) bool {
_ = context;
return a.dt.? < b.dt.?;
}
};
pub const Release = struct {
id: []const u8,
@ -41,13 +47,13 @@ pub const Release = struct {
//status_id: ?[]const u8 = null,
count: u8,
title: []const u8,
@"artist-credit": []ArtistCredit,
@"artist-credit": ?[]ArtistCredit = null,
//artist_credit: []ArtistCredit,
@"release-group": ReleaseGroup,
@"release-group": ?ReleaseGroup = null,
//release_group: ReleaseGroup,
@"track-count": u8,
@"track-count": u16,
//track_count: u8,
media: []Medium,
media: ?[]Medium = null,
status: ?[]const u8 = null,
date: ?[]const u8 = null,
country: ?[]const u8 = null,
@ -56,26 +62,26 @@ pub const Release = struct {
disambiguation: ?[]const u8 = null,
};
pub const Medium = struct {
position: u8,
position: ?u32 = null,
format: ?[]const u8 = null,
track: []Track,
@"track-count": u8,
track: ?[]Track = null,
@"track-count": u32,
//track_count: u8,
@"track-offset": u8,
@"track-offset": ?u32 = null,
//track_offset: u8,
};
pub const Track = struct {
id: []const u8,
number: []const u8,
title: []const u8,
length: u64,
length: ?u64 = null,
};
pub const Area = struct {
id: []const u8,
name: []const u8,
@"sort-name": []const u8,
//sort_name: []const u8,
@"iso-3166-1-codes": [][]const u8,
@"iso-3166-1-codes": ?[][]const u8 = null,
//iso_3166_1_codes: [][]const u8,
};
pub const ReleaseEvent = struct {
@ -84,12 +90,12 @@ pub const ReleaseEvent = struct {
};
pub const ReleaseGroup = struct {
id: []const u8,
@"type-id": []const u8,
@"type-id": ?[]const u8 = null,
//type_id: []const u8,
@"primary-type-id": []const u8,
@"primary-type-id": ?[]const u8 = null,
//primary_type_id: []const u8,
title: []const u8,
@"primary-type": []const u8,
@"primary-type": ?[]const u8 = null,
//primary_type: []const u8,
@"secondary-types": ?[][]const u8 = null,
@"secondary-type-ids": ?[][]const u8 = null,

View file

@ -1 +0,0 @@
pub const MBID = []const u8;

View file

@ -20,11 +20,11 @@ pub const Result = struct {
switch (count) {
0 => return null, // No results
1 => {
switch (self.recordings.?[0].@"artist-credit".len) {
switch (self.recordings.?[0].@"artist-credit".?.len) {
0 => unreachable, // All recordings have at least one ArtistCredit
1 => return self.recordings.?[0].@"artist-credit"[0].artist.id,
1 => return self.recordings.?[0].@"artist-credit".?[0].artist.id,
else => {
for (self.recordings.?[0].@"artist-credit") |ac| {
for (self.recordings.?[0].@"artist-credit".?) |ac| {
if (std.mem.eql(u8, ac.name, an)) return ac.artist.id;
}
},
@ -33,7 +33,7 @@ pub const Result = struct {
else => {
for (self.recordings.?) |rc| {
if (std.mem.eql(u8, rc.title, tn)) { // I'd really prefer not including track name, but for complete accuracy
for (rc.@"artist-credit") |ac| {
for (rc.@"artist-credit".?) |ac| {
if (std.mem.eql(u8, ac.name, an)) return ac.artist.id;
}
}
@ -50,11 +50,11 @@ pub const Result = struct {
switch (count) {
0 => return null,
1 => {
switch (self.recordings.?[0].releases.len) {
switch (self.recordings.?[0].releases.?.len) {
0 => unreachable,
1 => return self.recordings.?[0].releases[0].id,
1 => return self.recordings.?[0].releases.?[0].id,
else => {
for (self.recordings.?[0].releases) |re| {
for (self.recordings.?[0].releases.?) |re| {
if (std.mem.eql(u8, re.title, rgn)) return re.id;
}
},
@ -63,8 +63,8 @@ pub const Result = struct {
else => { // This is not ideal, limit the number of results
for (self.recordings.?) |rc| {
if (std.mem.eql(u8, rc.title, tn)) {
for (rc.releases) |re| {
if (std.mem.eql(u8, re.@"release-group".title, rgn)) return re.@"release-group".id;
for (rc.releases.?) |re| {
if (std.mem.eql(u8, re.@"release-group".?.title, rgn)) return re.@"release-group".?.id;
}
}
}
@ -74,18 +74,34 @@ pub const Result = struct {
return null;
}
pub fn getSpecifiedRID(self: *const Result, tn: []const u8, rgn: []const u8, an: []const u8) ?[]const u8 {
_ = tn;
_ = rgn;
_ = an;
pub fn getSpecifiedRID(self: *const Result, tn: []const u8, rgn: []const u8, an: []const u8) !?[]const u8 {
if (self.count) |count| {
switch (count) {
0 => return null,
1 => return self.recording.?[0].id,
1 => return self.recordings.?[0].id,
else => {
return null; // Sort by date
var rcs: []Recording = self.recordings.?;
for (rcs, 0..) |rc, i| {
rcs[i].dt = (try zeit.instant(.{ .source = .{ .iso8601 = rc.@"first-release-date".? } })).unixTimestamp();
}
std.mem.sort(Recording, rcs, {}, Recording.lessThan);
for (rcs) |rc| {
if (std.mem.eql(u8, tn, rc.title)) {
for (rc.@"artist-credit".?) |ac| {
if (std.mem.eql(u8, an, ac.name)) {
for (rc.releases.?) |re| {
if (std.mem.eql(u8, rgn, re.@"release-group".?.title)) {
//std.log.err("{s}", .{rc.id});
return rc.id;
}
}
}
}
}
}
},
}
}
return null;
}
};

View file

@ -59,6 +59,8 @@ test "arid_via_recording" {
const result: QR.Result = json.value;
std.Thread.sleep(1000000000);
try testing.expect(std.mem.eql(u8, result.getSpecifiedARID(artist, "Veni veni Emmanuel").?, iatm_id));
}
}
@ -81,7 +83,7 @@ test "arid_via_recording_multiple_artists_1" {
defer json.deinit();
const result: QR.Result = json.value;
std.Thread.sleep(1000000000);
try testing.expect(std.mem.eql(u8, result.getSpecifiedARID("Lyle Lovett", "Roll Me Up and Smoke Me When I Die").?, ll_id));
}
}
@ -104,7 +106,7 @@ test "rgid_via_recording" {
defer json.deinit();
const result: QR.Result = json.value;
std.Thread.sleep(1000000000);
try testing.expect(std.mem.eql(u8, result.getSpecifiedRGID("I of the Storm", "Beneath the Skin").?, bts_id));
}
}
@ -127,32 +129,32 @@ test "rgid_via_recording_multiple_artists_2" {
defer json.deinit();
const result: QR.Result = json.value;
std.Thread.sleep(1000000000);
try testing.expect(std.mem.eql(u8, result.getSpecifiedARID("Wilco", "Hesitating Beauty").?, wilco_id));
}
//try testing.expect(false);
}
//test "rid" {
// const test_alloc = std.testing.allocator;
// const track: []const u8 = "Battery";
// const album: []const u8 = "Master%20of%20Puppets";
// const artist: []const u8 = "Metallica";
//
// const battery_id: []const u8 = "3bfda26a-49fa-4bc4-a4d6-8bbfa0767ab7";
//
// var mb_result = std.ArrayList(u8).init(test_alloc);
// defer mb_result.deinit();
//
// const search_result = try mbSearch(test_alloc, &mb_result, track, album, artist);
//
// if (search_result) |sr| {
// const json = try std.json.parseFromSlice(QR.Result, test_alloc, sr, .{ .ignore_unknown_fields = true });
// defer json.deinit();
//
// const result: QR.Result = json.value;
//
// try testing.expect(std.mem.eql(u8, result.getSpecifiedARID("Wilco", "Hesitating Beauty").?, battery_id));
// }
//}
test "rid" {
const test_alloc = std.testing.allocator;
const track: []const u8 = "Aqualung";
const album: []const u8 = "Aqualung";
const artist: []const u8 = "Jethro%20Tull";
const battery_id: []const u8 = "2621d113-3a9f-41f9-a0f2-b9459f86e8e9";
var mb_result = std.ArrayList(u8).init(test_alloc);
defer mb_result.deinit();
const search_result = try mbSearch(test_alloc, &mb_result, track, album, artist);
if (search_result) |sr| {
const json = try std.json.parseFromSlice(QR.Result, test_alloc, sr, .{ .ignore_unknown_fields = true });
defer json.deinit();
const result: QR.Result = json.value;
try testing.expect(std.mem.eql(u8, (try result.getSpecifiedRID("Aqualung", "Aqualung", "Jethro Tull")).?, battery_id));
}
}