1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { blob } from "https://esm.town/v/std/blob";
export interface PuzzleContent {
grid: string[][];
info: {
type: string;
title: string;
author: string;
description: string;
};
clues: {
down: (string | null)[];
across: (string | null)[];
};
shades: any[];
circles: any[];
private: boolean;
}
export interface Puzzle {
pid: string;
content: PuzzleContent;
stats: {
numSolves: number;
};
}
interface PuzzlesData {
puzzles: Puzzle[];
}
const searchCx = async ({ query, page = 0, pageSize = 10, standard = true, mini = false }: {
query: string;
page?: number;
pageSize?: number;
standard?: boolean;
mini?: boolean;
}): Promise<PuzzlesData> => {
// https://api.foracross.com/api/puzzle_list?page=0&pageSize=50&filter%5BnameOrTitleFilter%5D=ny%20times%20june%2019%202024&filter%5BsizeFilter%5D%5BMini%5D=true&filter%5BsizeFilter%5D%5BStandard%5D=true
const url = new URL("https://api.foracross.com/api/puzzle_list");
const search = new URLSearchParams();
search.set("page", `${page}`);
search.set("pageSize", `${pageSize}`);
search.set("filter[nameOrTitleFilter]", query);
search.set("filter[sizeFilter][Mini]", `${mini}`);
search.set("filter[sizeFilter][Standard]", `${standard}`);
url.search = search.toString();
return fetch(url.toString()).then(r => r.json()) as Promise<PuzzlesData>;
};
export const fetchTodaysNytCxPuzzle = async (): Promise<Puzzle> => {
const dateStr = new Intl.DateTimeFormat("en-US", { year: "numeric", month: "long", day: "numeric" }).format(
new Date(),
).replace(",", "");
const cacheKey = `nyt-puzzle-${dateStr.replace(" ", "-")}`;
const cachedPuzzle = await blob.getJSON(cacheKey) as Puzzle | undefined;
if (cachedPuzzle) {
return cachedPuzzle;
}
const { puzzles } = await searchCx({
query: `ny times ${dateStr}`,
pageSize: 1,
});
const puzzle = puzzles[0];
if (!puzzle) {
throw new Error("No puzzle found");
}
await blob.setJSON(cacheKey, puzzle);
return puzzle;
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
June 19, 2024