From 456fbfbeafe7f9c1d2b03e40e5ebdbeea9e20ab1 Mon Sep 17 00:00:00 2001 From: mike Date: Wed, 24 Dec 2025 06:53:20 +0100 Subject: [PATCH] update them --- src/puzzle/Main.java | 56 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/src/puzzle/Main.java b/src/puzzle/Main.java index f32f118..5dbdae2 100644 --- a/src/puzzle/Main.java +++ b/src/puzzle/Main.java @@ -2,9 +2,11 @@ package puzzle; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; -import java.time.LocalDate; -import java.util.Date; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; import java.util.Locale; public class Main { @@ -82,10 +84,13 @@ public class Main { } // Export to JSON file - var dateStr = LocalDate.now().toString(); - var theme = "algemeen-" + new Date().getTime(); - var filename = String.format("crossword_%s_%02d_%s.json", dateStr, 1, safeSlug(theme)); - var outDir = "data"; + var now = OffsetDateTime.now(ZoneOffset.UTC); + var createdAt = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")); + var dateStr = now.toLocalDate().toString(); + var id = createdAt.replace(":", "-") + "_" + (System.currentTimeMillis() / 1000); + var theme = "algemeen"; + var filename = id + ".json"; + var outDir = "/data/puzzle/puzzles"; var outputPath = Paths.get(outDir, filename); try { @@ -93,6 +98,11 @@ public class Main { var json = toJson(out, dateStr, theme); Files.writeString(outputPath, json, StandardCharsets.UTF_8); System.out.println("\nSaved to: " + outputPath); + + // Update index.json + var pathInIndex = "/puzzles/" + filename; + var indexRecord = toIndexRecordJson(id, pathInIndex, dateStr, theme, out.difficulty(), createdAt); + updateIndex(outDir, indexRecord); } catch (IOException e) { System.err.println("Failed to write " + filename + ": " + e.getMessage()); } @@ -149,4 +159,38 @@ public class Main { private static String safeSlug(String s) { return s.toLowerCase().replaceAll("[^a-z0-9]+", "-").replaceAll("^-|-$", ""); } + + private static String toIndexRecordJson(String id, String path, String date, String theme, int difficulty, String createdAt) { + return String.format( + "{\"id\":\"%s\",\"path\":\"%s\",\"date\":\"%s\",\"theme\":\"%s\",\"difficulty\":%d,\"createdAt\":\"%s\"}", + escapeJson(id), escapeJson(path), escapeJson(date), escapeJson(theme), difficulty, escapeJson(createdAt) + ); + } + + private static void updateIndex(String outDir, String newRecordJson) { + Path indexPath = Paths.get(outDir, "index.json"); + try { + String content; + if (Files.exists(indexPath)) { + content = Files.readString(indexPath, StandardCharsets.UTF_8).trim(); + } else { + content = ""; + } + + if (content.isEmpty() || content.equals("[]")) { + content = "[\n " + newRecordJson + "\n]"; + } else { + int firstBracket = content.indexOf('['); + if (firstBracket != -1) { + content = content.substring(0, firstBracket + 1) + "\n " + newRecordJson + "," + content.substring(firstBracket + 1); + } else { + content = "[\n " + newRecordJson + "\n]"; + } + } + Files.writeString(indexPath, content, StandardCharsets.UTF_8); + System.out.println("Updated index.json at: " + indexPath); + } catch (IOException e) { + System.err.println("Failed to update index.json: " + e.getMessage()); + } + } } \ No newline at end of file