91 lines
3.4 KiB
Zig
91 lines
3.4 KiB
Zig
const std = @import("std");
|
|
const zeit = @import("zeit");
|
|
|
|
const Entities = @import("Entities.zig");
|
|
const Artist = Entities.Artist;
|
|
const Recording = Entities.Recording;
|
|
|
|
pub const Result = struct {
|
|
created: ?[]const u8 = null,
|
|
//created: ?zeit.Instant.Source.iso8601 = null,
|
|
count: ?u32 = null,
|
|
offset: ?u32 = null,
|
|
artists: ?[]Artist = null,
|
|
recordings: ?[]Recording = null,
|
|
@"error": ?[]const u8 = null,
|
|
help: ?[]const u8 = null,
|
|
|
|
pub fn getSpecifiedARID(self: *const Result, an: []const u8, tn: []const u8) ?[]const u8 {
|
|
if (self.count) |count| { // Otherwise there was an error
|
|
switch (count) {
|
|
0 => return null, // No results
|
|
1 => {
|
|
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,
|
|
else => {
|
|
for (self.recordings.?[0].@"artist-credit") |ac| {
|
|
if (std.mem.eql(u8, ac.name, an)) return ac.artist.id;
|
|
}
|
|
},
|
|
}
|
|
},
|
|
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| {
|
|
if (std.mem.eql(u8, ac.name, an)) return ac.artist.id;
|
|
}
|
|
}
|
|
}
|
|
},
|
|
}
|
|
}
|
|
return null; // Maybe return error here instead
|
|
}
|
|
|
|
// Not sure if I want to get a ReleaseGroup or Release yet. Start with ReleaseGroup
|
|
pub fn getSpecifiedRGID(self: *const Result, tn: []const u8, rgn: []const u8) ?[]const u8 {
|
|
if (self.count) |count| {
|
|
switch (count) {
|
|
0 => return null,
|
|
1 => {
|
|
switch (self.recordings.?[0].releases.len) {
|
|
0 => unreachable,
|
|
1 => return self.recordings.?[0].releases[0].id,
|
|
else => {
|
|
for (self.recordings.?[0].releases) |re| {
|
|
if (std.mem.eql(u8, re.title, rgn)) return re.id;
|
|
}
|
|
},
|
|
}
|
|
},
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
},
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
pub fn getSpecifiedRID(self: *const Result, tn: []const u8, rgn: []const u8, an: []const u8) ?[]const u8 {
|
|
_ = tn;
|
|
_ = rgn;
|
|
_ = an;
|
|
if (self.count) |count| {
|
|
switch (count) {
|
|
0 => return null,
|
|
1 => return self.recording.?[0].id,
|
|
else => {
|
|
return null; // Sort by date
|
|
},
|
|
}
|
|
}
|
|
}
|
|
};
|