From a2aa946b3b8e4de55fe751fd94be1bad511193b7 Mon Sep 17 00:00:00 2001 From: mitteneer Date: Tue, 18 Feb 2025 16:27:24 -0500 Subject: [PATCH] Fix null albums We're so back --- src/app/jobs/process_scrobbles.zig | 8 ++++---- src/app/views/upload.zig | 7 +++---- src/types.zig | 4 ++-- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/app/jobs/process_scrobbles.zig b/src/app/jobs/process_scrobbles.zig index 2de34f1..e77c310 100644 --- a/src/app/jobs/process_scrobbles.zig +++ b/src/app/jobs/process_scrobbles.zig @@ -25,9 +25,9 @@ pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig //std.log.debug("{s}", .{scrobble.track}); // Make hashes - const album_hash = std.hash.Fnv1a_64.hash(scrobble.album); - const artist_hash = std.hash.Fnv1a_64.hash(scrobble.artist); - const song_hash = std.hash.Fnv1a_64.hash(scrobble.track); + const album_hash = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(scrobble.album))); + const artist_hash = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(scrobble.artist))); + const song_hash = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(scrobble.track))); // Make IDs // Song: Song hash XOR artist hash XOR album hash @@ -48,7 +48,7 @@ pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig // then a descriptive string can be provided to // differentiate after the fact, or in a rule. - var album_id: u64 = 0; + var album_id: i32 = 0; const song_id = (song_hash ^ artist_hash ^ album_hash); if (artist_hash == album_hash) { album_id = album_hash; diff --git a/src/app/views/upload.zig b/src/app/views/upload.zig index f6e19d3..301f4f6 100644 --- a/src/app/views/upload.zig +++ b/src/app/views/upload.zig @@ -19,16 +19,15 @@ pub fn post(request: *jetzig.Request) !jetzig.View { var root = try request.data(.object); if (try request.file("upload")) |file| { - const content = try std.json.parseFromSlice(lastfm, request.allocator, file.content, .{}); - defer content.deinit(); - const history = content.value; + //std.debug.print("{s}", .{file.content}); + const content = try std.json.parseFromSliceLeaky(lastfm, request.allocator, file.content, .{ .ignore_unknown_fields = true }); var scrobbles_view = try root.put("scrobbles", .array); var job = try request.job("process_scrobbles"); var scrobbles_data = try job.params.put("scrobbles", .array); - for (history.scrobbles) |scrobble| { + for (content.scrobbles) |scrobble| { var value = try scrobbles_data.append(.object); // This is so unnecessary, probably useful once I start doing Spotify integration though inline for (std.meta.fields(Scrobble)) |f| { diff --git a/src/types.zig b/src/types.zig index 5df2b23..e10fb05 100644 --- a/src/types.zig +++ b/src/types.zig @@ -1,7 +1,7 @@ pub const LastFMScrobble = struct { track: []const u8, artist: []const u8, - album: ?[]const u8, + album: ?[]const u8 = "", date: i128, }; @@ -13,7 +13,7 @@ pub const SafeLastFMScrobble = struct { }; // From lastfmstats.com -pub const LastFM = struct { username: []u8, scrobbles: []LastFMScrobble }; +pub const LastFM = struct { username: []const u8, scrobbles: []LastFMScrobble }; pub const SpotifyScrobble = struct { ts: []u8,