Something went wrong. Try again.
semantic bufo search find-bufo.com
bufo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432//! bufo of the month — ported from the retired rust backend.//!//! GET /api/bufo-of-the-month → json; GET /bufo-of-the-month → html page.//! the winner comes from the bot's public stats: last month's top match from//! monthly.json, else a deterministic rotation over the cumulative top-24,//! with published winners pinned so history never rewrites itself.
const std = @import("std");const http = std.http;const json = std.json;const mem = std.mem;const Allocator = mem.Allocator;const Io = std.Io;
const ROTATION_POOL_SIZE: usize = 24;const Pinned = struct { month: []const u8, name: []const u8 };const PINNED_MONTHLY_BUFOS = [_]Pinned{.{ .month = "2026-06", .name = "bufo-on-the-ceiling" }};
pub const BotBufo = struct { name: []const u8, url: []const u8, matches: u64, rank: usize,};
pub const Winner = struct { month: []const u8, name: []const u8, url: []const u8, bot_matches: u64, bot_rank: usize, basis_month: ?[]const u8, source: []const u8,};
const YearMonth = struct { year: i32, month: u32 };
const PreviousWinner = struct { bufo: BotBufo, basis_month: []const u8 };
pub fn currentMonth(io: Io) YearMonth { const secs: i64 = @intCast(@divFloor(Io.Timestamp.now(io, .real).nanoseconds, std.time.ns_per_s)); return civilFromDays(@divFloor(secs, 86_400));}
fn civilFromDays(days_since_epoch: i64) YearMonth { const z = days_since_epoch + 719_468; const era = @divFloor(if (z >= 0) z else z - 146_096, 146_097); const doe = z - era * 146_097; const yoe = @divFloor(doe - @divFloor(doe, 1460) + @divFloor(doe, 36_524) - @divFloor(doe, 146_096), 365); const y = yoe + era * 400; const doy = doe - (365 * yoe + @divFloor(yoe, 4) - @divFloor(yoe, 100)); const mp = @divFloor(5 * doy + 2, 153); const month: i64 = mp + (if (mp < 10) @as(i64, 3) else @as(i64, -9)); const year = y + (if (month <= 2) @as(i64, 1) else @as(i64, 0)); return .{ .year = @intCast(year), .month = @intCast(month) };}
const month_names = [_][]const u8{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December",};
fn monthName(alloc: Allocator, ym: YearMonth) ![]const u8 { const name = if (ym.month >= 1 and ym.month <= 12) month_names[ym.month - 1] else "Unknown"; return std.fmt.allocPrint(alloc, "{s} {d}", .{ name, ym.year });}
fn monthKey(alloc: Allocator, ym: YearMonth) ![]const u8 { return std.fmt.allocPrint(alloc, "{d:0>4}-{d:0>2}", .{ @as(u32, @intCast(ym.year)), ym.month });}
pub fn previousMonth(ym: YearMonth) YearMonth { return if (ym.month == 1) .{ .year = ym.year - 1, .month = 12 } else .{ .year = ym.year, .month = ym.month - 1 };}
/// parse the bot stats page: each card is/// `<div class="bufo-card" ... title="NAME (N matches)" ...>\s*<img src="URL"`pub fn parseBotStats(alloc: Allocator, html: []const u8) ![]BotBufo { var out: std.ArrayList(BotBufo) = .empty; var from: usize = 0; while (mem.indexOfPos(u8, html, from, "<div class=\"bufo-card\"")) |card| { from = card + 1; const tag_end = mem.indexOfScalarPos(u8, html, card, '>') orelse break; const tag = html[card..tag_end]; const title_start = (mem.indexOf(u8, tag, "title=\"") orelse continue) + "title=\"".len; const title_end = mem.indexOfScalarPos(u8, tag, title_start, '"') orelse continue; const title = tag[title_start..title_end]; const paren = mem.lastIndexOf(u8, title, " (") orelse continue; if (!mem.endsWith(u8, title, " matches)")) continue; const name = title[0..paren]; const count_str = title[paren + 2 .. title.len - " matches)".len]; const matches = std.fmt.parseInt(u64, count_str, 10) catch continue;
const img = mem.indexOfPos(u8, html, tag_end, "<img src=\"") orelse continue; const url_start = img + "<img src=\"".len; const url_end = mem.indexOfScalarPos(u8, html, url_start, '"') orelse continue; // the img must belong to this card, not a later one if (mem.indexOfPos(u8, html, tag_end, "<div class=\"bufo-card\"")) |next| { if (img > next) continue; } try out.append(alloc, .{ .name = name, .url = html[url_start..url_end], .matches = matches, .rank = out.items.len + 1, }); } return out.items;}
fn byName(_: void, a: BotBufo, b: BotBufo) bool { return mem.lessThan(u8, a.name, b.name);}
pub fn pickMonthlyBufo(alloc: Allocator, bufos: []const BotBufo, ym: YearMonth) !?BotBufo { const key = try monthKey(alloc, ym); for (PINNED_MONTHLY_BUFOS) |pin| { if (mem.eql(u8, pin.month, key)) { for (bufos) |b| if (mem.eql(u8, b.name, pin.name)) return b; } } const pool_len = @min(bufos.len, ROTATION_POOL_SIZE); if (pool_len == 0) return null; const pool = try alloc.dupe(BotBufo, bufos[0..pool_len]); mem.sort(BotBufo, pool, {}, byName); const basis = previousMonth(ym); const month_index: i64 = @as(i64, basis.year) * 12 + (@as(i64, basis.month) - 1); const idx: usize = @intCast(@mod(month_index, @as(i64, @intCast(pool_len)))); return pool[idx];}
fn byMatchesDesc(_: void, a: BotBufo, b: BotBufo) bool { if (a.matches != b.matches) return a.matches > b.matches; return mem.lessThan(u8, a.name, b.name);}
/// monthly.json: {"monthly_bufo_matches": {"YYYY-MM\tname": {"count": N, "url": "..."}}}pub fn pickPreviousMonthWinner(alloc: Allocator, monthly: json.ObjectMap, ym: YearMonth) !?PreviousWinner { const basis = previousMonth(ym); const basis_key = try monthKey(alloc, basis); var candidates: std.ArrayList(BotBufo) = .empty; var it = monthly.iterator(); while (it.next()) |entry| { const key = entry.key_ptr.*; const tab = mem.indexOfScalar(u8, key, '\t') orelse continue; if (!mem.eql(u8, key[0..tab], basis_key)) continue; if (entry.value_ptr.* != .object) continue; const stats = entry.value_ptr.object; const count = switch (stats.get("count") orelse continue) { .integer => |n| @as(u64, @intCast(@max(n, 0))), else => continue, }; const url = switch (stats.get("url") orelse continue) { .string => |s| s, else => continue, }; try candidates.append(alloc, .{ .name = key[tab + 1 ..], .url = url, .matches = count, .rank = 0 }); } if (candidates.items.len == 0) return null; mem.sort(BotBufo, candidates.items, {}, byMatchesDesc); var winner = candidates.items[0]; winner.rank = 1; return .{ .bufo = winner, .basis_month = try monthName(alloc, basis) };}
pub const Source = struct { io: Io, http: *http.Client, bot_stats_url: []const u8, public_url: []const u8,
fn get(self: Source, alloc: Allocator, url: []const u8) ![]const u8 { var body: Io.Writer.Allocating = .init(alloc); const res = self.http.fetch(.{ .location = .{ .url = url }, .method = .GET, .response_writer = &body.writer }) catch return error.Upstream; if (res.status != .ok) return error.Upstream; return body.written(); }
pub fn winner(self: Source, alloc: Allocator) !Winner { const stats_url = mem.trimEnd(u8, self.bot_stats_url, "/"); const html = try self.get(alloc, stats_url); const bufos = try parseBotStats(alloc, html); const ym = currentMonth(self.io); const current_key = try monthKey(alloc, ym);
var pinned = false; for (PINNED_MONTHLY_BUFOS) |pin| if (mem.eql(u8, pin.month, current_key)) { pinned = true; };
var chosen: BotBufo = undefined; var basis_month: ?[]const u8 = null; var source: []const u8 = undefined; if (pinned) { chosen = (try pickMonthlyBufo(alloc, bufos, ym)) orelse return error.NotFound; source = "published monthly winner"; } else if (try self.previousMonthWinner(alloc, stats_url, ym)) |prev| { chosen = prev.bufo; basis_month = prev.basis_month; source = "previous month bot match stats"; } else { chosen = (try pickMonthlyBufo(alloc, bufos, ym)) orelse return error.NotFound; source = "deterministic cumulative bot match stats fallback"; } return .{ .month = try monthName(alloc, ym), .name = chosen.name, .url = chosen.url, .bot_matches = chosen.matches, .bot_rank = chosen.rank, .basis_month = basis_month, .source = source, }; }
fn previousMonthWinner(self: Source, alloc: Allocator, stats_url: []const u8, ym: YearMonth) !?PreviousWinner { const url = try std.fmt.allocPrint(alloc, "{s}/monthly.json", .{stats_url}); const body = self.get(alloc, url) catch return null; const parsed = json.parseFromSliceLeaky(json.Value, alloc, body, .{}) catch return null; if (parsed != .object) return null; const monthly = parsed.object.get("monthly_bufo_matches") orelse return null; if (monthly != .object) return null; return try pickPreviousMonthWinner(alloc, monthly.object, ym); }
pub fn writeJson(self: Source, alloc: Allocator, w: Winner) ![]const u8 { return json.Stringify.valueAlloc(alloc, .{ .month = w.month, .name = w.name, .url = w.url, .bot_matches = w.bot_matches, .bot_rank = w.bot_rank, .bot_stats_url = self.bot_stats_url, .basis_month = w.basis_month, .source = w.source, }, .{}); }
pub fn writePage(self: Source, alloc: Allocator, w: Winner) ![]const u8 { const public_url = mem.trimEnd(u8, self.public_url, "/"); const page_url = try std.fmt.allocPrint(alloc, "{s}/bufo-of-the-month", .{public_url}); const title = try std.fmt.allocPrint(alloc, "{s} Bufo of the Month: {s}", .{ w.month, w.name }); const stats_phrase = if (w.basis_month) |bm| try std.fmt.allocPrint(alloc, "{d} {s} bot matches and rank #{d} for that month", .{ w.bot_matches, bm, w.bot_rank }) else try std.fmt.allocPrint(alloc, "{d} bot matches and current public rank #{d}", .{ w.bot_matches, w.bot_rank }); const description = try std.fmt.allocPrint(alloc, "{s} is the {s} Bufo of the Month with {s}.", .{ w.name, w.month, stats_phrase });
const e = struct { fn f(a: Allocator, s: []const u8) ![]const u8 { return escapeHtml(a, s); } }.f; return std.fmt.allocPrint(alloc, page_template, .{ .title = try e(alloc, title), .page_url = try e(alloc, page_url), .description = try e(alloc, description), .image = try e(alloc, w.url), .alt = try e(alloc, w.name), .name = try e(alloc, w.name), .month = try e(alloc, w.month), .stats_phrase = try e(alloc, stats_phrase), .stats_url = try e(alloc, self.bot_stats_url), }); }};
pub fn escapeHtml(alloc: Allocator, s: []const u8) ![]const u8 { var out: std.ArrayList(u8) = .empty; for (s) |c| switch (c) { '&' => try out.appendSlice(alloc, "&"), '<' => try out.appendSlice(alloc, "<"), '>' => try out.appendSlice(alloc, ">"), '"' => try out.appendSlice(alloc, """), else => try out.append(alloc, c), }; return out.items;}
const page_template = \\<!DOCTYPE html> \\<html lang="en"> \\<head> \\ <meta charset="UTF-8"> \\ <meta name="viewport" content="width=device-width, initial-scale=1.0"> \\ <title>{[title]s}</title> \\ <link rel="canonical" href="{[page_url]s}"> \\ <meta name="description" content="{[description]s}"> \\ <meta property="og:type" content="website"> \\ <meta property="og:site_name" content="find bufo"> \\ <meta property="og:url" content="{[page_url]s}"> \\ <meta property="og:title" content="{[title]s}"> \\ <meta property="og:description" content="{[description]s}"> \\ <meta property="og:image" content="{[image]s}"> \\ <meta name="twitter:card" content="summary_large_image"> \\ <meta name="twitter:title" content="{[title]s}"> \\ <meta name="twitter:description" content="{[description]s}"> \\ <meta name="twitter:image" content="{[image]s}"> \\ <link rel="icon" type="image/png" href="/static/favicon.png"> \\ <style> \\ * {{ box-sizing: border-box; margin: 0; padding: 0; }} \\ body {{ \\ align-items: center; \\ background: #8ba888; \\ color: white; \\ display: flex; \\ font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Fira Code', 'Fira Mono', 'Roboto Mono', monospace; \\ justify-content: center; \\ min-height: 100vh; \\ padding: 24px; \\ text-align: center; \\ }} \\ main {{ max-width: 680px; width: 100%; }} \\ h1 {{ font-size: 2.4rem; line-height: 1.1; margin-bottom: 18px; text-shadow: 2px 2px 4px rgba(0,0,0,0.2); }} \\ .image-wrap {{ \\ background: rgba(255,255,255,0.92); \\ border-radius: 8px; \\ box-shadow: 0 10px 40px rgba(0,0,0,0.2); \\ margin: 0 auto 18px; \\ padding: 18px; \\ }} \\ img {{ display: block; max-height: 420px; object-fit: contain; width: 100%; }} \\ p {{ color: rgba(255,255,255,0.9); font-size: 0.95rem; line-height: 1.6; margin-bottom: 20px; }} \\ p a {{ \\ color: white; \\ font-weight: 700; \\ text-decoration: underline; \\ text-decoration-color: rgba(255,255,255,0.55); \\ text-underline-offset: 3px; \\ }} \\ p a:hover {{ text-decoration-color: white; }} \\ .home-link {{ \\ background: #667eea; \\ border-radius: 8px; \\ color: white; \\ display: inline-block; \\ font-weight: 700; \\ padding: 12px 18px; \\ text-decoration: none; \\ }} \\ .home-link:hover {{ background: #5568d3; }} \\ </style> \\</head> \\<body> \\ <main> \\ <h1>{[title]s}</h1> \\ <div class="image-wrap"> \\ <img src="{[image]s}" alt="{[alt]s}"> \\ </div> \\ <p>{[name]s} is the {[month]s} Bufo of the Month with {[stats_phrase]s} in the <a href="{[stats_url]s}" target="_blank" rel="noopener">public bot stats</a>.</p> \\ <a class="home-link" href="/">find more bufos</a> \\ </main> \\</body> \\</html>;
// --- tests: the rust suite, ported ---
const t = std.testing;
fn bufo(name: []const u8, matches: u64, rank: usize) BotBufo { return .{ .name = name, .url = "https://all-the.bufo.zone/x.png", .matches = matches, .rank = rank };}
test "pins published june 2026 winner" { var arena = std.heap.ArenaAllocator.init(t.allocator); defer arena.deinit(); const bufos = [_]BotBufo{ bufo("bufo-take-my-money", 1874, 6), bufo("bufo-on-the-ceiling", 1866, 7) }; const w = (try pickMonthlyBufo(arena.allocator(), &bufos, .{ .year = 2026, .month = 6 })).?; try t.expectEqualStrings("bufo-on-the-ceiling", w.name); try t.expectEqual(@as(usize, 7), w.rank);}
test "stable pool does not depend on live rank order" { var arena = std.heap.ArenaAllocator.init(t.allocator); defer arena.deinit(); const ranked = [_]BotBufo{ bufo("bufo-z", 30, 1), bufo("bufo-a", 20, 2), bufo("bufo-m", 10, 3) }; const reranked = [_]BotBufo{ bufo("bufo-a", 21, 1), bufo("bufo-m", 19, 2), bufo("bufo-z", 18, 3) }; const ym: YearMonth = .{ .year = 2026, .month = 7 }; const first = (try pickMonthlyBufo(arena.allocator(), &ranked, ym)).?; const second = (try pickMonthlyBufo(arena.allocator(), &reranked, ym)).?; try t.expectEqualStrings(first.name, second.name);}
test "january uses previous december as basis" { try t.expectEqual(YearMonth{ .year = 2026, .month = 12 }, previousMonth(.{ .year = 2027, .month = 1 })); try t.expectEqual(YearMonth{ .year = 2027, .month = 1 }, previousMonth(.{ .year = 2027, .month = 2 }));}
test "civil date from epoch days" { try t.expectEqual(YearMonth{ .year = 1970, .month = 1 }, civilFromDays(0)); try t.expectEqual(YearMonth{ .year = 2026, .month = 8 }, civilFromDays(20693)); // 2026-08-28 try t.expectEqual(YearMonth{ .year = 2000, .month = 3 }, civilFromDays(11017)); // 2000-03-01}
test "picks previous month winner from monthly stats" { var arena = std.heap.ArenaAllocator.init(t.allocator); defer arena.deinit(); const a = arena.allocator(); const parsed = try json.parseFromSliceLeaky(json.Value, a, \\{"2026-06\tbufo-second":{"count":4,"url":"u2"},"2026-06\tbufo-first":{"count":8,"url":"u1"},"2026-07\tbufo-too-new":{"count":99,"url":"u3"}} , .{}); const r = (try pickPreviousMonthWinner(a, parsed.object, .{ .year = 2026, .month = 7 })).?; try t.expectEqualStrings("bufo-first", r.bufo.name); try t.expectEqual(@as(u64, 8), r.bufo.matches); try t.expectEqual(@as(usize, 1), r.bufo.rank); try t.expectEqualStrings("June 2026", r.basis_month);}
test "parses bot stats cards" { var arena = std.heap.ArenaAllocator.init(t.allocator); defer arena.deinit(); const html = \\<div class="bufo-card" data-x="1" title="bufo-a (12 matches)"> \\ <img src="https://all-the.bufo.zone/bufo-a.png"> \\</div><div class="bufo-card" title="bufo-b (3 matches)"><img src="https://x/b.gif"></div> ; const bufos = try parseBotStats(arena.allocator(), html); try t.expectEqual(@as(usize, 2), bufos.len); try t.expectEqualStrings("bufo-a", bufos[0].name); try t.expectEqual(@as(u64, 12), bufos[0].matches); try t.expectEqualStrings("https://all-the.bufo.zone/bufo-a.png", bufos[0].url); try t.expectEqual(@as(usize, 2), bufos[1].rank);}
test "escapes html" { var arena = std.heap.ArenaAllocator.init(t.allocator); defer arena.deinit(); try t.expectEqualStrings("a&b<c>"", try escapeHtml(arena.allocator(), "a&b<c>\""));}