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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export let load_new_subreddit_posts = async ({
subreddit,
filters,
after_utc,
fetch_limit,
}) => {
let patterns = [];
for (const pattern_def of filters) {
let pattern_str, mode;
if (typeof pattern_def === "string") {
pattern_str = pattern_def;
mode = "gi";
} else {
pattern_str = pattern_def.pattern;
if (pattern_def.strategy == "wordmatch") {
pattern_str = `\\b${pattern_str}\\b`;
}
mode = pattern_def.mode || "gi";
}
patterns.push(RegExp(pattern_str, mode));
}
let next_fetch_from = null;
let matches = [];
let new_posts = 0;
let fetches = 1;
let data = await fetchJSON(
`https://www.reddit.com/r/${subreddit}/new/.json`
);
console.log(`got https://www.reddit.com/r/${subreddit}/new/.json`);
while (true) {
let done = false;
for (const post of data.data.children) {
let post_data = post.data;
let created_utc = post_data.created_utc;
if (created_utc <= after_utc) {
done = true;
break;
}
new_posts += 1;
let pattern_idx = 0;
for (const pattern of patterns) {
if (
post_data.title.match(pattern) ||
post_data.selftext.match(pattern)
) {
matches.push({
url: post_data.url,
title: post_data.title,
pattern_idx: pattern_idx,
});
console.log(
`found match created_utc=${created_utc} after_utc=${after_utc} url=${post_data.url}`
);
break;
}
pattern_idx += 1;
}
}
if (done) {
break;
}
let last_post_name = data.data.children.at(-1).data.name;
// mark this subreddit for additional fetches if it reached its budget
if (fetches == fetch_limit) {
next_fetch_from = last_post_name;
break;
}
fetches += 1;
data = await fetchJSON(
`https://www.reddit.com/r/${subreddit}/new/.json?after=${last_post_name}`
);
console.log(
`got https://www.reddit.com/r/${subreddit}/new/.json?after=${last_post_name}`
);
}
return {
new_posts: new_posts,
matches: matches,
fetches: fetches,
next_fetch_from: next_fetch_from,
};
};
👆 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.