2023-06-08 20:09:25 +00:00
|
|
|
import fs from "fs";
|
|
|
|
import { spawn } from "child_process";
|
2022-05-03 12:23:37 +00:00
|
|
|
|
|
|
|
const outDir = "src/shared/translations/";
|
|
|
|
const recommendationsFile = "recommended-instances.json";
|
|
|
|
const instanceStatsFile = "src/shared/instance_stats.ts";
|
2023-06-29 19:53:14 +00:00
|
|
|
const min_monthly_users = 5;
|
2022-05-03 12:23:37 +00:00
|
|
|
|
|
|
|
fs.mkdirSync(outDir, { recursive: true });
|
|
|
|
|
|
|
|
// crawl instance stats
|
|
|
|
try {
|
2023-06-08 20:09:25 +00:00
|
|
|
const recommended_instances = JSON.parse(
|
|
|
|
fs.readFileSync(recommendationsFile, "utf8")
|
|
|
|
);
|
2022-05-03 12:23:37 +00:00
|
|
|
var all_recommended = [];
|
|
|
|
for (var k in recommended_instances) {
|
|
|
|
if (k != "exclude") {
|
2022-05-17 13:53:57 +00:00
|
|
|
all_recommended.push(...recommended_instances[k]);
|
2022-05-03 12:23:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-08 20:09:25 +00:00
|
|
|
const run = spawn(
|
|
|
|
"cargo",
|
|
|
|
[
|
|
|
|
"run",
|
|
|
|
"--",
|
|
|
|
"--json",
|
|
|
|
"--start-instances",
|
|
|
|
all_recommended,
|
|
|
|
"--exclude-instances",
|
|
|
|
recommended_instances.exclude,
|
|
|
|
],
|
|
|
|
{
|
|
|
|
cwd: "lemmy-stats-crawler",
|
|
|
|
encoding: "utf8",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
let savedOutput = "";
|
|
|
|
|
|
|
|
run.stdout.on("data", data => {
|
|
|
|
const strData = data.toString();
|
|
|
|
process.stdout.write(strData);
|
|
|
|
savedOutput += strData;
|
2022-05-03 12:23:37 +00:00
|
|
|
});
|
|
|
|
|
2023-06-08 20:09:25 +00:00
|
|
|
run.stderr.on("data", data => {
|
|
|
|
const strData = data.toString();
|
|
|
|
process.stdout.write(strData);
|
2022-05-17 13:53:57 +00:00
|
|
|
});
|
|
|
|
|
2023-06-08 20:09:25 +00:00
|
|
|
run.on("close", exitCode => {
|
2023-06-29 19:53:14 +00:00
|
|
|
var stats = JSON.parse(savedOutput);
|
2023-06-29 20:07:46 +00:00
|
|
|
// Crawl results from all instances include tons of data which needs to be compiled.
|
|
|
|
// If it is too much data it breaks the build, so we need to exclude as much as possible.
|
2023-06-29 19:56:33 +00:00
|
|
|
stats.instance_details = stats.instance_details
|
2023-06-29 20:07:46 +00:00
|
|
|
// Exclude instances with closed registration
|
2023-06-29 19:53:14 +00:00
|
|
|
.filter(
|
|
|
|
i =>
|
|
|
|
i.site_info.site_view.local_site.registration_mode != "closed"
|
|
|
|
)
|
2023-06-29 20:07:46 +00:00
|
|
|
// Exclude instances with few active users
|
2023-06-29 19:53:14 +00:00
|
|
|
.filter(
|
|
|
|
i =>
|
|
|
|
i.site_info.site_view.counts.users_active_month >
|
|
|
|
min_monthly_users
|
|
|
|
);
|
2023-06-29 20:07:46 +00:00
|
|
|
// Exclude unnecessary data
|
|
|
|
stats.instance_details.forEach(i => {
|
|
|
|
delete i.site_info.admins;
|
|
|
|
delete i.site_info.all_languages;
|
|
|
|
delete i.site_info.discussion_languages;
|
|
|
|
delete i.site_info.taglines;
|
|
|
|
delete i.site_info.custom_emojis;
|
|
|
|
delete i.federated_instances;
|
|
|
|
});
|
2022-05-03 12:23:37 +00:00
|
|
|
|
2022-05-17 13:53:57 +00:00
|
|
|
let stats2 = {
|
|
|
|
stats: stats,
|
2023-06-08 20:09:25 +00:00
|
|
|
recommended: recommended_instances,
|
|
|
|
};
|
2022-05-17 13:53:57 +00:00
|
|
|
|
|
|
|
let data = `export const instance_stats = \n `;
|
|
|
|
data += JSON.stringify(stats2, null, 2) + ";";
|
|
|
|
fs.writeFileSync(instanceStatsFile, data);
|
|
|
|
});
|
2023-06-08 20:09:25 +00:00
|
|
|
run.await;
|
2022-05-03 12:23:37 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|