-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackfill.ts
57 lines (37 loc) · 1.45 KB
/
backfill.ts
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
const posts: { [k in string]: { date: Date; contents: string } } = {};
const outputFile = "./README.md";
const dataDir = "./data";
const archiveDir = "./archive";
const archiveImageFileLookup: { [k in string]: string } = {};
function getDatePrefix(date: string): string {
return date.substring(0, date.length - 6);
}
for await (const file of Deno.readDir(archiveDir)) {
if (!file.isFile) continue;
const date = file.name.replace("daily-dall-e-", "").replace(".png", "");
archiveImageFileLookup[getDatePrefix(date)] = `${archiveDir}/${file.name}`;
}
for await (const file of Deno.readDir(dataDir)) {
if (!file.isFile) continue;
const fileContents = await Deno.readTextFile(`${dataDir}/${file.name}`);
const data: {
topThree: string[];
imagePrompt: string;
imageURL: string;
} = JSON.parse(fileContents);
const date = file.name.replace("daily-dall-e-", "").replace(".json", "");
const datePrefix = getDatePrefix(date);
const archiveImageFile = archiveImageFileLookup[datePrefix];
if (!archiveImageFile) continue;
const day = date.substring(0, 10);
const contents = `
## ${day}
![Daily Dall-E](${archiveImageFile})
> ${data.imagePrompt}
${data.topThree.map((t) => `1. ${t}`).join("\n")}
`;
posts[day] = { date: new Date(date), contents };
}
const postsArray = Object.values(posts);
postsArray.sort((a, b) => b.date.getTime() - a.date.getTime());
Deno.writeTextFile(outputFile, postsArray.map((p) => p.contents).join("---\n"));