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
37
38
39
40
import { blob } from "https://esm.town/v/std/blob?v=11";
import { email } from "https://esm.town/v/std/email?v=11";
import { fetchText } from "https://esm.town/v/stevekrouse/fetchText?v=6";
import { JSDOM } from "npm:jsdom";
const username = "ccorcos";
export default async function(interval: Interval) {
const url = "https://news.ycombinator.com/threads?id=" + username;
const html = await fetchText(url);
console.log("html", html.slice(0, 100));
const { document } = (new JSDOM(html)).window;
const comments = Array.from(document.querySelectorAll(".athing.comtr")) as any[];
const currentIds = new Set(comments.map(x => x.id));
const newIds = new Set(comments.map(x => x.id));
const key = "hn2:" + username;
const prevIds = (await blob.getJSON(key)) || [] as string[];
for (const id of prevIds) newIds.delete(id);
if (newIds.size === 0) {
console.log("No new comments.");
return;
}
for (const id of Array.from(newIds)) prevIds.push(id)
console.log(newIds.size + " new comments.");
await blob.setJSON(key, prevIds);
const newComments = comments.filter(comment => newIds.has(comment.id))
.map(comment => comment.textContent);
await email({
subject: "New HN comments",
text: url + "\n\n" + newComments.join("\n---\n"),
});
}
👆 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.