Implement mbSearch()
Getting a segfault, not sure why
This commit is contained in:
parent
1d4f79206a
commit
454505335c
4 changed files with 120 additions and 173 deletions
|
|
@ -1,13 +1,14 @@
|
|||
const MBID = @import("MBID.zig");
|
||||
|
||||
pub const Alias = struct {
|
||||
@"sort-name": []const u8,
|
||||
//sort_name: []const u8,
|
||||
@"type-id": []const u8,
|
||||
//type_id: []const u8,
|
||||
name: []const u8,
|
||||
sort_name: []const u8,
|
||||
type_id: ?MBID,
|
||||
type: ?[]const u8, // Could be an enum
|
||||
locale: ?[]const u8,
|
||||
primary: ?bool,
|
||||
begin: ?i64,
|
||||
end: ?i64,
|
||||
ended: bool,
|
||||
type: []const u8,
|
||||
primary: ?[]const u8,
|
||||
@"begin-date": ?[]const u8,
|
||||
//begin_date: []const u8,
|
||||
@"end-date": ?[]const u8,
|
||||
//end_date: []const u8,
|
||||
};
|
||||
|
|
|
|||
214
src/Entities.zig
214
src/Entities.zig
|
|
@ -1,173 +1,85 @@
|
|||
//! Types of MusicBrainz Entities described here: https://musicbrainz.org/doc/MusicBrainz_Entity
|
||||
//! Fields based on JSON response fields
|
||||
|
||||
const MBID = @import("MBID.zig");
|
||||
const Alias = @import("Alias.zig");
|
||||
//const zeit = @import("zeit");
|
||||
const Alias = @import("Alias.zig").Alias;
|
||||
|
||||
pub const Artist = struct {
|
||||
id: MBID,
|
||||
type: enum([]const u8) { Person, Group, Orchestra, Choir, Character, Other },
|
||||
type_id: MBID,
|
||||
score: u8,
|
||||
gender_id: ?MBID,
|
||||
id: []const u8,
|
||||
name: []const u8,
|
||||
sort_name: []const u8,
|
||||
gender: ?enum([]const u8) { male, female, other },
|
||||
country: ?[]const u8,
|
||||
area: ?Area,
|
||||
begin_area: ?Area,
|
||||
end_area: ?Area,
|
||||
disambiguation: ?[]const u8,
|
||||
ipis: ?[][]const u8,
|
||||
isnis: ?[][]const u8,
|
||||
life_span: struct { ended: ?bool, begin: []const u8, end: ?[]const u8 },
|
||||
aliases: ?[]Alias,
|
||||
tags: ?[]struct { count: i32, name: []const u8 },
|
||||
@"sort-name": []const u8,
|
||||
//sort_name: []const u8,
|
||||
aliases: ?[]Alias = null,
|
||||
};
|
||||
|
||||
pub const ArtistCredit = struct { artists: []Artist, artists_names: ?[][]const u8, artists_join_phrase: ?[]const u8, mbid: MBID, name: []const u8 };
|
||||
|
||||
pub const Event = struct {
|
||||
pub const ArtistCredit = struct {
|
||||
name: []const u8,
|
||||
type: enum { concert, festival, stage_performance, award_ceremony, launch_event, convention_expo, masterclass_clinic },
|
||||
cancelled: bool,
|
||||
dates: struct { begin: i64, end: i64 },
|
||||
selist: []const u8, //Will need to write a parser for this
|
||||
time: []const u8,
|
||||
aliases: ?[]Alias,
|
||||
disambiguation: []const u8,
|
||||
annotation: []const u8,
|
||||
artist: Artist,
|
||||
};
|
||||
|
||||
pub const Label = struct {
|
||||
name: []const u8,
|
||||
disambiguation: []const u8,
|
||||
type: []enum {
|
||||
imprint,
|
||||
original_production,
|
||||
bootleg_production,
|
||||
reissue_production,
|
||||
distributor,
|
||||
holding,
|
||||
rights_society,
|
||||
},
|
||||
area: []const u8, // This in ISO-3166 format, and is referred to as "Country" on a different page
|
||||
label_code: []const u8,
|
||||
ipi_codes: [][]const u8, // There could be multiple for artists, unsure about albums
|
||||
isni_codes: [][]const u8, // See above
|
||||
date_period: struct { begin_date: i64, end_date: i64 },
|
||||
aliases: ?[]Alias,
|
||||
annotation: []const u8,
|
||||
mbid: MBID,
|
||||
};
|
||||
|
||||
pub const Place = struct { name: []const u8, type: enum([]const u8) { studio, venue = enum { ampitheatre, club, concert_hall_theatre, festival_stage }, stadium, indoor_arena, educational_institution, park, religious_building, pressing_plant, other }, address: []const u8, coordinates: struct { lat: i32, lon: i32 }, area: ?Area, dates: struct { begin: i64, end: i64 }, aliases: ?[]Alias, mbid: MBID, disambiguation: []const u8, annotation: []const u8 };
|
||||
|
||||
pub const Recording = struct {
|
||||
id: []const u8,
|
||||
score: u8,
|
||||
title: []const u8,
|
||||
artists: []Artist,
|
||||
length: f64, // I don't know what this looks like yet
|
||||
isrc: []const u8,
|
||||
mbid: MBID,
|
||||
disambiguation: []const u8,
|
||||
annotation: []const u8,
|
||||
length: u64,
|
||||
video: ?bool,
|
||||
@"artist-credit": []ArtistCredit,
|
||||
//artist_credit: []ArtistCredit,
|
||||
@"first-release-date": []const u8,
|
||||
//first_release_date: []const u8,
|
||||
releases: []Release,
|
||||
};
|
||||
|
||||
pub const Release = struct {
|
||||
id: []const u8,
|
||||
@"status-id": ?[]const u8 = null,
|
||||
//status_id: ?[]const u8 = null,
|
||||
count: i32,
|
||||
title: []const u8,
|
||||
artists: []Artist,
|
||||
date: i64,
|
||||
country: []const u8, // Consider making a file that exhaustievly lists ISO-3166 codes
|
||||
catalogue_number: []const u8,
|
||||
barcode: u64,
|
||||
status: enum { official, promotion, bootleg, pseudo_release, withdrawn, expunged, cancelled },
|
||||
packaging: enum { book, box, cardboard_paper_sleeve, cassette_case, clamshell_case, digibook, digifile, digipak, discbox_slider, fatbox, gatefold_cover, jewel_case, keep_case, longbox, metal_tin, plastic_sleeve, slidepack, slim_jewel, snap_case, super_jewel_box, other, none },
|
||||
language: []const u8, // ISO-639-3
|
||||
script: []const u8, // ISO-15924
|
||||
mbid: MBID,
|
||||
disambiguation: []const u8,
|
||||
annotation: []const u8,
|
||||
data_quality: enum {
|
||||
high,
|
||||
default,
|
||||
low,
|
||||
},
|
||||
@"artist-credit": []ArtistCredit,
|
||||
//artist_credit: []ArtistCredit,
|
||||
@"release-group": ReleaseGroup,
|
||||
//release_group: ReleaseGroup,
|
||||
@"track-count": u8,
|
||||
//track_count: u8,
|
||||
media: []Medium,
|
||||
status: ?[]const u8 = null,
|
||||
date: ?[]const u8 = null,
|
||||
country: ?[]const u8 = null,
|
||||
@"release-events": ?[]ReleaseEvent = null,
|
||||
//release_events: ?[]ReleaseEvent = null,
|
||||
};
|
||||
|
||||
pub const Medium = struct {
|
||||
title: []const u8,
|
||||
format: enum([]const u8) {
|
||||
compact_disc = enum([]const u8) {
|
||||
compact_disc,
|
||||
copy_control_cd,
|
||||
data_cd,
|
||||
dts_cd,
|
||||
enhanced_cd,
|
||||
hdcd,
|
||||
mixed_mode_cd,
|
||||
cd_r,
|
||||
eight_cm_cd,
|
||||
blu_spec_cd,
|
||||
minimax_cd,
|
||||
shm_cd,
|
||||
hqcd,
|
||||
cd_plus_g = enum { cd_plug_g, eight_cm_cd_plus_g },
|
||||
},
|
||||
vinyl = enum([]const u8) { seven_inch_vinyl, ten_inch_vinyl, twelve_inch_vinyl, flexi_disc = enum { flexi_disc, seven_inch_flexi_disc } },
|
||||
digital_media,
|
||||
cassette = enum { cassette, microcassette },
|
||||
dvd = enum { dvd, dvd_audio, dvd_video },
|
||||
sacd = enum([]const u8) { hybrid_sacd = enum { hybrid_sacd, hybrid_sacd_cd_layer, hybrig_sacd_sacd_layer }, shm_sacd },
|
||||
dualdisc = enum {
|
||||
dualdisc,
|
||||
dualdisc_cd_side,
|
||||
dualdisc_dvd_video_side,
|
||||
dualdisc_dvd_audio_side,
|
||||
},
|
||||
minidisc,
|
||||
blu_ray = enum { blu_ray, blu_ray_r },
|
||||
hd_dvd,
|
||||
vcd = enum { vcd, svcd },
|
||||
cdv,
|
||||
umd,
|
||||
shellac = enum {
|
||||
shellac,
|
||||
seven_inch_shellac,
|
||||
ten_inch_shellac,
|
||||
twelve_inch_shellac,
|
||||
},
|
||||
acetate = enum {
|
||||
acetate,
|
||||
seven_inch_acetate,
|
||||
ten_inch_acetate,
|
||||
twelve_inch_acetate,
|
||||
},
|
||||
sd_card = enum { sd_card, slotmusic },
|
||||
other = enum([]const u8) { betamax, cartridge = enum { cartridge, hipac, playtape }, ced, dat, dcc, dvdplus = enum { dvdplus_cd_side, dvd_plus_dvd_video_side, dvd_plus_dvd_audio_side }, edison_diamond_disc, floppy_disk = enum { floppy_disk, three_half_inch_floppy_disk, five_quarter_inch_floppy_disk, zip_disk }, laserdisc = enum { laserdisc, eight_inch_laserdisc, twelve_inch_laserdisc }, music_card, pathe_disc, piano_roll, playbutton, reel_to_reel, tefifon, usb_flash_drive, vhd, vhs, vinyldisc, wax_cylinder },
|
||||
},
|
||||
position: u8,
|
||||
format: []const u8,
|
||||
track: []Track,
|
||||
@"track-count": u8,
|
||||
//track_count: u8,
|
||||
@"track-offset": u8,
|
||||
//track_offset: u8,
|
||||
};
|
||||
pub const Track = struct {
|
||||
id: []const u8,
|
||||
number: []const u8,
|
||||
title: []const u8,
|
||||
length: u64,
|
||||
};
|
||||
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,
|
||||
};
|
||||
pub const ReleaseEvent = struct {
|
||||
date: []const u8,
|
||||
area: Area,
|
||||
};
|
||||
|
||||
//pub const Track
|
||||
|
||||
pub const ReleaseGroup = struct {
|
||||
id: []const u8,
|
||||
@"type-id": []const u8,
|
||||
//type_id: []const u8,
|
||||
@"primary-type-id": []const u8,
|
||||
//primary_type_id: []const u8,
|
||||
title: []const u8,
|
||||
artists: []Artist, // Mentions using ArtistCredits for multiple artists. Will need to see a JSOn response to know what to do here
|
||||
types: struct { primary: enum([]const u8) { single = enum { us, uk, japan }, ep, broadcast, other }, secondary: ?[]enum([]const u8) { compilation, soundtrack, spokenword, interview, audiobook, audio_drama, live, remix, dj_mix, mixtape_street, demo, field_recording } },
|
||||
mbid: MBID,
|
||||
disambiguation: []const u8,
|
||||
annotation: []const u8,
|
||||
@"primary-type": []const u8,
|
||||
//primary_type: []const u8,
|
||||
};
|
||||
|
||||
pub const Series = struct { name: []const u8, type: enum([]const u8) { release_group_series = enum { release_group_series, release_group_award, podcast }, release_series, recording_series = enum { recording_series, recording_award }, work_series = enum { work_series, catalogue, work_award }, artist_series = enum { artist_series, artist_award }, event_series = enum { even_series, tour, festival, run, residency, award_ceremony } }, ordering_type: []const u8, aliases: []Alias, mbid: MBID, disambiguation: []const u8, annotation: []const u8 };
|
||||
|
||||
pub const URL = []const u8;
|
||||
|
||||
pub const Work = struct { name: []const u8, type: enum { discrete, aggregate }, aliases: []Alias, lyrics_languages: [][]const u8, iswc: []const u8, mbid: MBID, disambiguation: []const u8, annotation: []const u8 };
|
||||
|
||||
pub const Area = struct { name: []const u8, type: enum { country, subdivision, county, municipality, city, district, island }, dates: struct { begin: i64, end: i64 }, iso_3166_codes: [][]const u8, aliases: []Alias, mbid: MBID, annotation: []const u8 };
|
||||
|
||||
pub const Genre = [][]const u8;
|
||||
|
||||
pub const Instrument = struct { name: []const u8, type: enum { wind, string, percussion, electronic, family, ensemble, other }, description: []const u8, aliases: []Alias, mbid: MBID, disambiguation: []const u8, annotation: []const u8 };
|
||||
|
|
|
|||
|
|
@ -1,6 +1,11 @@
|
|||
const Entities = @import("Entities.zig");
|
||||
const Recording = Entities.Recording;
|
||||
|
||||
const Artist = Entities.Artist;
|
||||
const Release = Entities.Release;
|
||||
|
||||
pub const Result = struct { created: []const u8, count: i32, offset: i32, artists: ?[]Artist };
|
||||
pub const Result = struct {
|
||||
created: ?[]const u8 = null,
|
||||
count: ?u32 = null,
|
||||
offset: ?u32 = null,
|
||||
recordings: ?[]Recording = null,
|
||||
@"error": ?[]const u8 = null,
|
||||
help: ?[]const u8 = null,
|
||||
};
|
||||
|
|
|
|||
47
src/root.zig
47
src/root.zig
|
|
@ -3,27 +3,33 @@ const testing = std.testing;
|
|||
const zeit = @import("zeit");
|
||||
const Entities = @import("Entities.zig");
|
||||
const Alias = @import("Alias.zig");
|
||||
const MBID = @import("MBID.zig");
|
||||
const QR = @import("QueryResults.zig");
|
||||
const Client = std.http.Client;
|
||||
|
||||
pub const user_agent: []const u8 = u8{"ZuletztMBClient/0.0.1 (https://swebbguy@gmail.com)"};
|
||||
pub const user_agent: []const u8 = "ZuletztMBClient/0.0.1 (swebbguy@gmail.com)";
|
||||
|
||||
pub fn mbSearch(allocator: std.mem.Allocator, track_name: []const u8, album_name: []const u8, artist_name: []const u8) !?QR.Result {
|
||||
var client = Client{ .allocator = allocator };
|
||||
defer client.deinit();
|
||||
|
||||
const query: []const u8 = try std.fmt.allocPrint(allocator, "https://musicbrainz.org/ws/2/recording/?query={s}%20AND%20artist:{s}%20AND%20release:{s}", .{ track_name, artist_name, album_name });
|
||||
const query: []const u8 = try std.fmt.allocPrint(allocator, "https://musicbrainz.org/ws/2/recording/?query=\"{s}\"%20AND%20artist:\"{s}\"%20AND%20release:\"{s}\"&fmt=json", .{ track_name, artist_name, album_name });
|
||||
defer allocator.free(query);
|
||||
|
||||
var mb_result = std.ArrayList(u8).init(allocator);
|
||||
defer mb_result.deinit();
|
||||
const response: std.http.Status = try client.fetch(.{ .response_storage = mb_result, .location = .{ .url = query }, .method = .GET, .headers = .{ .user_agent = user_agent } });
|
||||
switch (response) {
|
||||
errdefer mb_result.deinit();
|
||||
|
||||
const response: Client.FetchResult = try client.fetch(.{ .response_storage = .{ .dynamic = &mb_result }, .location = .{ .url = query }, .method = .GET, .headers = .{ .user_agent = .{ .override = user_agent } } });
|
||||
|
||||
switch (@intFromEnum(response.status)) {
|
||||
0...299 => std.log.debug("Success", .{}),
|
||||
300...399 => {
|
||||
std.log.debug("Redirected", .{});
|
||||
return null;
|
||||
},
|
||||
400...511 => return Client.RequestError.ConnectionRefused,
|
||||
400...511 => {
|
||||
std.log.err("Get rekt\n{s}", .{mb_result.items});
|
||||
return Client.ConnectError.ConnectionRefused;
|
||||
},
|
||||
512 => {
|
||||
std.log.debug("Need to login", .{});
|
||||
return null;
|
||||
|
|
@ -31,9 +37,14 @@ pub fn mbSearch(allocator: std.mem.Allocator, track_name: []const u8, album_name
|
|||
else => unreachable,
|
||||
}
|
||||
|
||||
const result: QR.Result = try std.json.parseFromSlice(QR.Result, allocator, mb_result.items, .{});
|
||||
//std.log.err("{s}", .{mb_result_slice});
|
||||
|
||||
return result;
|
||||
const result = try std.json.parseFromSlice(QR.Result, allocator, mb_result.items, .{ .ignore_unknown_fields = true });
|
||||
defer result.deinit();
|
||||
|
||||
const query_result = result.value;
|
||||
|
||||
return query_result;
|
||||
}
|
||||
|
||||
//pub fn getAlbumMBID(query: QR.Result)
|
||||
|
|
@ -43,3 +54,21 @@ pub fn mbSearch(allocator: std.mem.Allocator, track_name: []const u8, album_name
|
|||
//test "basic add functionality" {
|
||||
// try testing.expect(add(3, 7) == 10);
|
||||
//}
|
||||
|
||||
// This test is very volatile, but I think
|
||||
// these params are specific enough that
|
||||
// it shouldn't need changing too often
|
||||
test "iamthemorning" {
|
||||
const test_alloc = std.testing.allocator;
|
||||
const track: []const u8 = "Veni%20Veni%20Emmanuel";
|
||||
const album: []const u8 = "Counting%20the%20Ghosts";
|
||||
const artist: []const u8 = "Iamthemorning";
|
||||
|
||||
const iatm_id: []const u8 = "5854a6de-af8f-4b99-8710-cb47d6436a19";
|
||||
|
||||
const search_result: ?QR.Result = try mbSearch(test_alloc, track, album, artist);
|
||||
if (search_result) |sr| {
|
||||
const recording = sr.recordings.?[0];
|
||||
try testing.expect(std.mem.eql(u8, recording.id, iatm_id));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue