-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcralwer.ts
More file actions
186 lines (165 loc) · 5.44 KB
/
cralwer.ts
File metadata and controls
186 lines (165 loc) · 5.44 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import z from 'zod'
import _ from 'lodash'
import { dateLessThanOneMonthAgo } from './utils/date';
import { BasicCrawler, Dataset, RequestQueue } from "crawlee";
import fs from 'node:fs/promises'
import path from 'node:path';
const MIN_MAU = 20;
const CRAWLER_TIMEOUT = 30 * 60 * 1000;
const WRITE_DATA_INTERVAL = 10_000;
function isSupportedSoftware(software: string) {
switch (software.trim().toLowerCase()) {
case "lemmy":
case "piefed":
return true;
default:
return false;
}
}
function normalizeInstance(instance: string) {
instance = instance.trim();
if (!instance.startsWith("http")) {
instance = `https://${instance}`
}
return instance.replace(/\/+$/, "");
}
const lemmySiteV3 = z.object({
site_view: z.object({
site: z.object({
description: z.string().nullable().optional(),
icon: z.string().nullable().optional(),
}),
local_site: z.object({
registration_mode: z.string(),
private_instance: z.boolean(),
}),
counts: z.object({
users_active_month: z.number()
})
}),
});
const pieFedSiteV3 = lemmySiteV3;
const nodeInfoSchema = z.object({
software: z.object({
name: z.enum(["lemmy", "piefed"]),
version: z.string(),
}),
});
const federatedInstancesSchema = z.object({
federated_instances: z.object({
linked: z.array(z.object({
domain: z.string(),
software: z.string().optional(),
published: z.string().optional(),
updated: z.string().optional(),
}))
})
})
type Instance = {
url: string,
host: string,
description?: string,
icon?: string,
software: "lemmy" | "piefed"
registrationMode: string,
};
async function crawl() {
const dataset = await Dataset.open();
const requestQueue = await RequestQueue.open();
const crawler = new BasicCrawler({
requestQueue,
maxConcurrency: 50,
minConcurrency: 5,
requestHandlerTimeoutSecs: 10,
maxRequestRetries: 3,
requestHandler: async ({ request, sendRequest }) => {
async function get<S extends z.ZodObject>(url: string, schema: S) {
const nodeInfoReq = await sendRequest({
url,
method: "GET"
})
return schema.parse(JSON.parse(nodeInfoReq.body))
}
const explore = async (res: z.infer<typeof federatedInstancesSchema>) => {
await requestQueue.addRequests(
res.federated_instances.linked.filter(linked => {
const updated = linked.updated ?? linked.published;
return linked.software && isSupportedSoftware(linked.software) && updated && dateLessThanOneMonthAgo(updated)
}).map(linked => normalizeInstance(linked.domain))
)
}
const instance = normalizeInstance(request.url);
const host = new URL(instance).host;
const nodeInfo = await get(
`${instance}/nodeinfo/2.1`,
nodeInfoSchema
)
switch (nodeInfo.software.name) {
case "lemmy": {
if (nodeInfo.software.version.startsWith("1.")) {
// const federatedInstances = await get(`${instance}/api/v4/federated_instances`, federatedInstancesSchema)
// explore(federatedInstances)
} else {
const federatedInstances = await get(`${instance}/api/v3/federated_instances`, federatedInstancesSchema)
await explore(federatedInstances)
const site = await get(`${instance}/api/v3/site`, lemmySiteV3)
if (site.site_view.counts.users_active_month >= MIN_MAU && !site.site_view.local_site.private_instance) {
await Dataset.pushData<Instance>({
url: instance,
host,
description: site.site_view.site.description,
icon: site.site_view.site.icon,
software: "lemmy",
registrationMode: site.site_view.local_site.registration_mode,
})
}
}
break;
}
case "piefed": {
const federatedInstances = await get(`${instance}/api/v3/federated_instances`, federatedInstancesSchema)
await explore(federatedInstances)
const site = await get(`${instance}/api/v3/site`, pieFedSiteV3)
if (site.site_view.counts.users_active_month >= MIN_MAU && !site.site_view.local_site.private_instance) {
await Dataset.pushData<Instance>({
url: instance,
host,
description: site.site_view.site.description,
icon: site.site_view.site.icon,
software: "piefed",
registrationMode: site.site_view.local_site.registration_mode,
})
}
break;
}
}
},
});
const write = async () => {
const items = (await dataset.getData()).items as Instance[];
const sorted = _.sortBy(_.uniqBy(items, 'host'), 'host')
const outPath = path.join(process.cwd(), "public", "v1");
await fs.mkdir(outPath, { recursive: true });
await fs.writeFile(path.join(outPath, "instances.json"), JSON.stringify(sorted, null, 2));
}
const id2 = setInterval(() => {
write();
}, WRITE_DATA_INTERVAL)
const id1 = setTimeout(() => {
clearInterval(id2)
crawler.autoscaledPool.abort();
console.log("STOPPING CRAWLER DUE TO TIMEOUT")
}, CRAWLER_TIMEOUT)
await crawler.run([
"https://lemmy.world",
"https://lemmy.zip",
"https://lemmy.ml",
"https://piefed.world",
"https://piefed.zip",
"https://piefed.world",
]);
clearTimeout(id1)
clearInterval(id2)
write();
}
crawl();