Readme

Prioritizes posts by authors with the most followers

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
import { fetchTwitterUser } from "https://esm.town/v/andreterron/fetchTwitterUser";
export async function sortedPosts({ accessToken, posts }: {
accessToken: string;
posts: {
id: string;
text: string;
author_id: string;
}[];
}) {
const q = new URLSearchParams({
"user.fields": "public_metrics",
}).toString();
// Enrich posts
const detailed = (await Promise.all(
posts.slice(0, 4).map(async (post) => {
try {
const author = await fetchTwitterUser({
id: post.author_id,
accessToken,
query: q,
});
return {
...post,
author,
};
}
catch (e) {
console.log("Failed to fetch user", post.author_id, q);
console.error(e);
return undefined;
}
}),
)).filter((a) => !!a);
//
// Sort by follower count
detailed.sort((a, b) =>
b.author.public_metrics.followers_count -
a.author.public_metrics.followers_count
);
return detailed;
}
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!
October 23, 2023