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

@ -65,6 +65,9 @@ pub fn build(b: *std.Build) void {
.root_module = exe_mod, .root_module = exe_mod,
}); });
const zig_time_dep = b.dependency("zeit", .{});
exe.root_module.addImport("zeit", zig_time_dep.module("zeit"));
// This declares intent for the executable to be installed into the // This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default // standard location when the user invokes the "install" step (the default
// step when running `zig build`). // step when running `zig build`).

View file

@ -36,45 +36,11 @@
// Once all dependencies are fetched, `zig build` no longer requires // Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity. // internet connectivity.
.dependencies = .{ .dependencies = .{
// See `zig fetch --save <url>` for a command-line interface for adding dependencies. .zeit = .{
//.example = .{ .url = "https://github.com/rockorager/zeit/archive/refs/tags/v0.6.0.tar.gz",
// // When updating this field to a new URL, be sure to delete the corresponding .hash = "zeit-0.0.0-5I6bk_pZAgB03N1p1GmVOZ--gOFwwQSRKj1UXb5tnaKS",
// // `hash`, otherwise you are communicating that you expect to find the old hash at },
// // the new URL. If the contents of a URL change this will result in a hash mismatch
// // which will prevent zig from using it.
// .url = "https://example.com/foo.tar.gz",
//
// // This is computed from the file contents of the directory of files that is
// // obtained after fetching `url` and applying the inclusion rules given by
// // `paths`.
// //
// // This field is the source of truth; packages do not come from a `url`; they
// // come from a `hash`. `url` is just one of many possible mirrors for how to
// // obtain a package matching this `hash`.
// //
// // Uses the [multihash](https://multiformats.io/multihash/) format.
// .hash = "...",
//
// // When this is provided, the package is found in a directory relative to the
// // build root. In this case the package's hash is irrelevant and therefore not
// // computed. This field and `url` are mutually exclusive.
// .path = "foo",
//
// // When this is set to `true`, a package is declared to be lazily
// // fetched. This makes the dependency only get fetched if it is
// // actually used.
// .lazy = false,
//},
}, },
// Specifies the set of files and directories that are included in this package.
// Only files and directories listed here are included in the `hash` that
// is computed for this package. Only files listed here will remain on disk
// when using the zig package manager. As a rule of thumb, one should list
// files required for compilation plus any license(s).
// Paths are relative to the build root. Use the empty string (`""`) to refer to
// the build root itself.
// A directory listed here means that all files within, recursively, are included.
.paths = .{ .paths = .{
"build.zig", "build.zig",
"build.zig.zon", "build.zig.zon",

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 MBID = @import("MBID.zig");
const Alias = @import("Alias.zig"); const Alias = @import("Alias.zig");
//const zeit = @import("zeit");
pub const Artist = struct { pub const Artist = struct {
id: MBID, id: MBID,
type: enum([]const u8) { Person, Group, Orchestra, Choir, Character, Other },
type_id: MBID,
score: u8,
gender_id: ?MBID,
name: []const u8, name: []const u8,
sort_name: []const u8, sort_name: []const u8,
type_id: null, // Idk what this is gender: ?enum([]const u8) { male, female, other },
type: enum { person, group, orchestra, choir, character, other }, country: ?[]const u8,
disambiguation: []const u8,
gender: ?[]const u8,
//gender: ?enum {
// male,
// female,
// other
//};
gender_id: null,
country: []const u8,
area: ?Area, area: ?Area,
begin_area: ?Area, begin_area: ?Area,
end_area: ?Area, end_area: ?Area,
life_span: struct { ended: bool, begin: i64, end: i64 }, disambiguation: ?[]const u8,
isnis: ?[][]const u8,
ipis: ?[][]const u8, ipis: ?[][]const u8,
isnis: ?[][]const u8,
life_span: struct { ended: ?bool, begin: []const u8, end: ?[]const u8 },
aliases: ?[]Alias, 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 }; 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 std = @import("std");
const testing = std.testing; 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 { pub const user_agent: []const u8 = u8{"ZuletztMBClient/0.0.1 (https://swebbguy@gmail.com)"};
return a + b;
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" { //pub fn getAlbumMBID(query: QR.Result)
try testing.expect(add(3, 7) == 10); //pub fn getArtistMBID(query: QR.Result)
} //pub fn getSongMBID(query: QR.Result)
//test "basic add functionality" {
// try testing.expect(add(3, 7) == 10);
//}