Runs every 1 hrs
Fork
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
import { fetch } from "https://esm.town/v/std/fetch";
import { big_stories_ranks } from "https://esm.town/v/tmcw/big_stories_ranks";
import process from "node:process";
export let big_story = (async () => {
const nytimes =
await (await fetch(
`https://api.nytimes.com/svc/topstories/v2/home.json?api-key=${process.env.nytimes_api_key}`,
)).json();
const now = Date.now();
const newDataPoints = nytimes.results.slice(0, 20).forEach(
({ title, url, created_date, section, uri }, rank) => {
if (big_stories_ranks[url]) {
big_stories_ranks[url].ranks.push([now, rank]);
}
else {
big_stories_ranks[url] = {
title,
url,
section,
ranks: [[now, rank]],
};
}
},
);
for (let url of Object.keys(big_stories_ranks)) {
// Prune stories over 3 days ago
if (
big_stories_ranks[url].ranks.every((rank) => {
return rank[0] < Date.now() - (1000 * 60 * 60 * 24 * 3);
})
) {
delete big_stories_ranks[url];
}
}
});
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.