Create mbSearch function

It would be so awesome... It would be so cool...
This commit is contained in:
mitteneer 2025-03-06 13:47:47 -05:00
parent 78a5d5e9d9
commit 1d4f79206a
5 changed files with 67 additions and 60 deletions

View file

@ -1,29 +1,29 @@
//! 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");
pub const Artist = struct {
id: MBID,
type: enum([]const u8) { Person, Group, Orchestra, Choir, Character, Other },
type_id: MBID,
score: u8,
gender_id: ?MBID,
name: []const u8,
sort_name: []const u8,
type_id: null, // Idk what this is
type: enum { person, group, orchestra, choir, character, other },
disambiguation: []const u8,
gender: ?[]const u8,
//gender: ?enum {
// male,
// female,
// other
//};
gender_id: null,
country: []const u8,
gender: ?enum([]const u8) { male, female, other },
country: ?[]const u8,
area: ?Area,
begin_area: ?Area,
end_area: ?Area,
life_span: struct { ended: bool, begin: i64, end: i64 },
isnis: ?[][]const u8,
disambiguation: ?[]const u8,
ipis: ?[][]const u8,
isnis: ?[][]const u8,
life_span: struct { ended: ?bool, begin: []const u8, end: ?[]const u8 },
aliases: ?[]Alias,
annotation: []const u8,
tags: ?[]struct { count: i32, name: []const u8 },
};
pub const ArtistCredit = struct { artists: []Artist, artists_names: ?[][]const u8, artists_join_phrase: ?[]const u8, mbid: MBID, name: []const u8 };

6
src/QueryResults.zig Normal file
View file

@ -0,0 +1,6 @@
const Entities = @import("Entities.zig");
const Artist = Entities.Artist;
const Release = Entities.Release;
pub const Result = struct { created: []const u8, count: i32, offset: i32, artists: ?[]Artist };

View file

@ -1,13 +1,45 @@
//! By convention, root.zig is the root source file when making a library. If
//! you are making an executable, the convention is to delete this file and
//! start with main.zig instead.
const std = @import("std");
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 export fn add(a: i32, b: i32) i32 {
return a + b;
pub const user_agent: []const u8 = u8{"ZuletztMBClient/0.0.1 (https://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 });
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) {
0...299 => std.log.debug("Success", .{}),
300...399 => {
std.log.debug("Redirected", .{});
return null;
},
400...511 => return Client.RequestError.ConnectionRefused,
512 => {
std.log.debug("Need to login", .{});
return null;
},
else => unreachable,
}
const result: QR.Result = try std.json.parseFromSlice(QR.Result, allocator, mb_result.items, .{});
return result;
}
test "basic add functionality" {
try testing.expect(add(3, 7) == 10);
}
//pub fn getAlbumMBID(query: QR.Result)
//pub fn getArtistMBID(query: QR.Result)
//pub fn getSongMBID(query: QR.Result)
//test "basic add functionality" {
// try testing.expect(add(3, 7) == 10);
//}