This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchCommunities.js
63 lines (54 loc) · 1.56 KB
/
fetchCommunities.js
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
const instance = "https://ANYLEMMYINSTANCE.ml"; // the instance you want to get the communities from
const delay = 2000; // Keep this at 2500 or higher to avoid rate limiting
// Don't touch anything below this line
// ------------------------------------
const plainInstance = instance.replace("https://", "").replace("http://", "")
const apiUrl = instance + "/api/v3/";
const getContent = {
method: "GET",
headers: {
"Content-Type": "application/json",
},
};
function encodeGetParams(p) {
return Object.entries(p)
.filter((kv) => !!kv[1])
.map((kv) => kv.map(encodeURIComponent).join("="))
.join("&");
}
async function fetchCommunities(page) {
const request = {
type_: "Local",
sort: "New",
show_nsfw: true,
page: page,
limit: 50,
};
const response = await fetch(
apiUrl + "community/list" + "?" + encodeGetParams(request),
getContent
);
return (await response.json()).communities.map(c => {
return c.community.name + "@" + plainInstance;
});
}
async function startFetching() {
let page = 1;
const fetchedCommunities = [];
let communities = await fetchCommunities(page);
let intervalId;
intervalId = setInterval(async () => {
console.log(communities);
page++;
communities = await fetchCommunities(page);
console.log(fetchedCommunities);
if (communities.length === 0) {
clearInterval(intervalId);
console.log("Done fetching communities!");
return;
}
fetchedCommunities.push(...communities);
}, delay);
console.log(fetchedCommunities);
}
startFetching();