From f59eec79a82584bd7bcc88277b3a0b6f86ba0eb8 Mon Sep 17 00:00:00 2001 From: mitteneer Date: Mon, 26 May 2025 11:15:19 -0400 Subject: [PATCH] Removed inline else from upload.zig If I can figure out a way to get an array of a union instead of a union of arrays, we're in business to make this even better, but this is fine right now. The inline else was just a dumb way to keep the for on the outside --- src/app/views/upload.zig | 108 +++++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 49 deletions(-) diff --git a/src/app/views/upload.zig b/src/app/views/upload.zig index f605982..bdb5cef 100644 --- a/src/app/views/upload.zig +++ b/src/app/views/upload.zig @@ -81,58 +81,68 @@ pub fn post(request: *jetzig.Request) !jetzig.View { // Not sure if I should be proud or feel sick switch (imported_scrobbles) { - inline else => |scrobbles| { + .LastFMStats => |scrobbles| { appends: for (scrobbles) |scrobble| { - const filtered_scrobble: Data.Scrobble = blk: switch (@TypeOf(scrobble)) { - Data.IgnorantScrobble => { - if (scrobble.date > latest_timestamp * 1_000 or scrobble.date < earliest_timestamp * 1_000) { - limited_tracks += 1; - continue :appends; - } - break :blk Data.Scrobble{ - .album = scrobble.album, - .artists_album = &[_][]const u8{scrobble.artist}, - .track = scrobble.track, - .artists_track = &[_][]const u8{scrobble.artist}, - .date = scrobble.date * 1_000, - }; - }, - Data.SpotifyScrobble => { - if (scrobble.ms_played < 30_000 and (scrobble.reason_end == null or !std.mem.eql(u8, scrobble.reason_end.?, "trackdone"))) { - skipped_tracks += 1; - continue :appends; - } - if (scrobble.master_metadata_album_artist_name == null or scrobble.master_metadata_track_name == null) { - skipped_tracks += 1; - continue :appends; - } - - const iso_ts = try zeit.Time.fromISO8601(scrobble.ts); - if ((iso_ts.after(latest_date) or iso_ts.before(earliest_date))) { - limited_tracks += 1; - continue :appends; - } - - break :blk Data.Scrobble{ - .album = scrobble.master_metadata_album_album_name.?, - .artists_album = &[_][]const u8{scrobble.master_metadata_album_artist_name.?}, - .track = scrobble.master_metadata_track_name.?, - .artists_track = &[_][]const u8{scrobble.master_metadata_album_artist_name.?}, - .date = (try zeit.instant(.{ .source = .{ .iso8601 = scrobble.ts } })).unixTimestamp() * 1_000_000, - }; - }, - Data.LastFMWebScrobble => { - break :blk Data.Scrobble{ - .album = if (scrobble.album) |album| album.@"#text" else "Not Provided", - .artists_album = &[_][]const u8{scrobble.artist.@"#text"}, - .track = scrobble.name, - .artists_track = &[_][]const u8{scrobble.artist.@"#text"}, - .date = try std.fmt.parseInt(i64, scrobble.date.?.uts, 10) * 1_000_000, - }; - }, - else => unreachable, + if (scrobble.date > latest_timestamp * 1_000 or scrobble.date < earliest_timestamp * 1_000) { + limited_tracks += 1; + continue :appends; + } + const filtered_scrobble = Data.Scrobble{ + .album = scrobble.album, + .artists_album = &[_][]const u8{scrobble.artist}, + .track = scrobble.track, + .artists_track = &[_][]const u8{scrobble.artist}, + .date = scrobble.date * 1_000, }; + const complete_scrobble = if (rule_list) |rl| try rules.applyScrobbleRule(request.allocator, filtered_scrobble, rl) else filtered_scrobble; + const row = try Utils.scrobbleToRow(request.allocator, complete_scrobble); + + try view_params.append(row); + try job_params.append(complete_scrobble); + } + }, + .LastFMWeb => |scrobbles| { + for (scrobbles) |scrobble| { + const filtered_scrobble = Data.Scrobble{ + .album = if (scrobble.album) |album| album.@"#text" else "Not Provided", + .artists_album = &[_][]const u8{scrobble.artist.@"#text"}, + .track = scrobble.name, + .artists_track = &[_][]const u8{scrobble.artist.@"#text"}, + .date = try std.fmt.parseInt(i64, scrobble.date.?.uts, 10) * 1_000_000, + }; + const complete_scrobble = if (rule_list) |rl| try rules.applyScrobbleRule(request.allocator, filtered_scrobble, rl) else filtered_scrobble; + + const row = try Utils.scrobbleToRow(request.allocator, complete_scrobble); + + try view_params.append(row); + try job_params.append(complete_scrobble); + } + }, + .Spotify => |scrobbles| { + appends: for (scrobbles) |scrobble| { + if (scrobble.ms_played < 30_000 and (scrobble.reason_end == null or !std.mem.eql(u8, scrobble.reason_end.?, "trackdone"))) { + skipped_tracks += 1; + continue :appends; + } + if (scrobble.master_metadata_album_artist_name == null or scrobble.master_metadata_track_name == null) { + skipped_tracks += 1; + continue :appends; + } + + const iso_ts = try zeit.Time.fromISO8601(scrobble.ts); + if ((iso_ts.after(latest_date) or iso_ts.before(earliest_date))) { + limited_tracks += 1; + continue :appends; + } + + const filtered_scrobble = Data.Scrobble{ + .album = scrobble.master_metadata_album_album_name.?, + .artists_album = &[_][]const u8{scrobble.master_metadata_album_artist_name.?}, + .track = scrobble.master_metadata_track_name.?, + .artists_track = &[_][]const u8{scrobble.master_metadata_album_artist_name.?}, + .date = (try zeit.instant(.{ .source = .{ .iso8601 = scrobble.ts } })).unixTimestamp() * 1_000_000, + }; const complete_scrobble = if (rule_list) |rl| try rules.applyScrobbleRule(request.allocator, filtered_scrobble, rl) else filtered_scrobble; const row = try Utils.scrobbleToRow(request.allocator, complete_scrobble);