Complete preliminary find and replace rules
Tested by replacing AJR with John Van Derwood. Need to test on albums and artists, as well as matching on one piece of metadata, and replacing another
This commit is contained in:
parent
445ca45fa9
commit
87a2fe2d34
4 changed files with 41 additions and 15 deletions
|
|
@ -3,6 +3,9 @@ const jetzig = @import("jetzig");
|
||||||
const jetquery = @import("jetzig").jetquery;
|
const jetquery = @import("jetzig").jetquery;
|
||||||
const Scrobble = @import("../../types.zig").LastFMScrobble;
|
const Scrobble = @import("../../types.zig").LastFMScrobble;
|
||||||
const lastfm = @import("../../types.zig").LastFM;
|
const lastfm = @import("../../types.zig").LastFM;
|
||||||
|
//const Rules = @import("../../types.zig").Rules;
|
||||||
|
const Data = @import("../../types.zig");
|
||||||
|
const rules = @import("../../apply_rule.zig");
|
||||||
|
|
||||||
// The `run` function for a job is invoked every time the job is processed by a queue worker
|
// The `run` function for a job is invoked every time the job is processed by a queue worker
|
||||||
// (or by the Jetzig server if the job is processed in-line).
|
// (or by the Jetzig server if the job is processed in-line).
|
||||||
|
|
@ -18,14 +21,14 @@ pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig
|
||||||
const file = try std.fs.cwd().openFile("rules.json", .{ .mode = .read_only });
|
const file = try std.fs.cwd().openFile("rules.json", .{ .mode = .read_only });
|
||||||
const file_content = try file.readToEndAlloc(allocator, 16_000_000);
|
const file_content = try file.readToEndAlloc(allocator, 16_000_000);
|
||||||
|
|
||||||
const rules = try std.json.parseFromSliceLeaky(Rules, allocator, file_content, .{});
|
const rule_list = try std.json.parseFromSliceLeaky(Data.Rules, allocator, file_content, .{});
|
||||||
|
|
||||||
if (params.getT(.array, "scrobbles")) |scrobbles| {
|
if (params.getT(.array, "scrobbles")) |scrobbles| {
|
||||||
for (scrobbles.items()) |item| {
|
for (scrobbles.items()) |item| {
|
||||||
//const fixed_date: u32 = @as(u32, item.getT(.integer, "date").?);
|
//const fixed_date: u32 = @as(u32, item.getT(.integer, "date").?);
|
||||||
const scrobble: Scrobble = .{ .track = item.getT(.string, "track").?, .artist = item.getT(.string, "artist").?, .album = item.getT(.string, "album") orelse "", .date = @as(u64, @bitCast(@as(i64, @truncate(item.getT(.integer, "date").? * 1000)))) };
|
const pre_scrobble: Scrobble = .{ .track = item.getT(.string, "track").?, .artist = item.getT(.string, "artist").?, .album = item.getT(.string, "album") orelse "", .date = @as(u64, @bitCast(@as(i64, @truncate(item.getT(.integer, "date").? * 1000)))) };
|
||||||
|
|
||||||
for (rules) |rule| {}
|
const scrobble = rules.applyScrobbleRule(pre_scrobble, rule_list);
|
||||||
|
|
||||||
// Make hashes
|
// Make hashes
|
||||||
//const album_hash = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(scrobble.album)));
|
//const album_hash = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(scrobble.album)));
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ If
|
||||||
<select name="match-cond" id="match-cond">
|
<select name="match-cond" id="match-cond">
|
||||||
<option value="is">is</option>
|
<option value="is">is</option>
|
||||||
<option value="contains">contains</option>
|
<option value="contains">contains</option>
|
||||||
<option value"matches">matches regex</option>
|
<option value="matches">matches regex</option>
|
||||||
</select>
|
</select>
|
||||||
<input type="text" name="match-txt" id="match-txt">
|
<input type="text" name="match-txt" id="match-txt">
|
||||||
<button type="button" onclick="condAdd()">
|
<button type="button" onclick="condAdd()">
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,34 @@
|
||||||
const Scrobble = @import("types").LastFMScrobble;
|
const std = @import("std");
|
||||||
const Rules = @import("types").Rules;
|
const Scrobble = @import("./types.zig").LastFMScrobble;
|
||||||
|
const Rules = @import("./types.zig").Rules;
|
||||||
|
|
||||||
pub fn applyRule(scrobble: Scrobble, rules: Rules) !Scrobble {
|
pub fn applyScrobbleRule(scrobble: Scrobble, rules: Rules) Scrobble {
|
||||||
|
var match_found: bool = true;
|
||||||
var output_scrobble: Scrobble = scrobble;
|
var output_scrobble: Scrobble = scrobble;
|
||||||
for (rules) |rule| {
|
for (rules.rules) |rule| {
|
||||||
for (rule.conditionals) |cond| {
|
for (rule.conditionals) |cond| {
|
||||||
switch (cond.match_cond) {}
|
switch (cond.match_cond) {
|
||||||
|
.is => switch (cond.match_on) {
|
||||||
|
inline else => |on| match_found = match_found and std.mem.eql(u8, @field(scrobble, @tagName(on)), cond.match_txt),
|
||||||
|
},
|
||||||
|
.contains => switch (cond.match_on) {
|
||||||
|
inline else => |on| match_found = match_found and (std.mem.count(u8, @field(scrobble, @tagName(on)), cond.match_txt) > 0),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (match_found) {
|
||||||
|
for (rule.actions) |act| {
|
||||||
|
switch (act.action) {
|
||||||
|
.add => {},
|
||||||
|
.replace => switch (act.action_on) {
|
||||||
|
inline else => |on| @field(output_scrobble, @tagName(on)) = act.action_txt,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return output_scrobble;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//pub fn applyAlbumRule() !Album {}
|
||||||
|
|
|
||||||
|
|
@ -38,23 +38,23 @@ pub const SpotifyScrobble = struct {
|
||||||
const Rule = struct {
|
const Rule = struct {
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
conditionals: []struct {
|
conditionals: []struct {
|
||||||
match_on: MatchOn,
|
match_on: ScrobbleFields,
|
||||||
match_cond: enum { is, contains },
|
match_cond: enum { is, contains },
|
||||||
match_txt: []const u8,
|
match_txt: []const u8,
|
||||||
},
|
},
|
||||||
actions: []struct {
|
actions: []struct {
|
||||||
action: []const u8,
|
action: enum { replace, add },
|
||||||
action_on: enum { is, contains },
|
action_on: ScrobbleFields,
|
||||||
action_txt: []const u8,
|
action_txt: []const u8,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const Rules = struct {
|
pub const Rules = struct {
|
||||||
rules: []const Rule,
|
rules: []const Rule,
|
||||||
};
|
};
|
||||||
|
|
||||||
const MatchOn = enum {
|
pub const ScrobbleFields = enum {
|
||||||
artist,
|
artist,
|
||||||
album,
|
album,
|
||||||
song,
|
track,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue