-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathregion.ts
111 lines (95 loc) · 3.03 KB
/
region.ts
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
import { readFileStr, exists } from "std/fs/mod.ts";
import * as wiki from "seran/wiki.ts";
export let handler = new wiki.Handler()
let rootSite = "fed.wiki.org";
handler.items("Welcome Visitors", [
"Welcome to this [[Seran Wiki]] Federation outpost.\
From this page you can find who we are and what we do.",
"Pages about us.",
"[[Joshua Benuck]]",
"[[Ward Cunningham]]",
"Pages where we do and share.",
"[[Region Rosters]]"
])
handler.items("Region Rosters", [
"Region crawling [[Configuration]]",
"[[One Degree]]",
"[[Two Degrees]]",
"[[Three Degrees]]"
])
handler.items("One Degree", async () => [
"Generated roster.",
wiki.item("roster", {text: [...(await oneDegreeAway(rootSite)).values()].join("\n")})
])
handler.items("Two Degrees", async () => [
"Generated roster.",
wiki.item("roster", {text: [...(await twoDegreesAway(rootSite)).values()].join("\n")})
])
handler.items("Three Degrees", async () => [
"Generated roster.",
wiki.item("roster", {text: [...(await threeDegreesAway(rootSite)).values()].join("\n")})
])
handler.items("Configuration", async (req,sys) => [
"We currently only support a fixed configuration compiled into the server.",
`Root site for rosters: ${rootSite}`,
`Number of scraped sites: ${(await readDirz('data')).length}`,
"See [[Sites]] for the full list."
])
handler.items("Sites", async () => [
"This is the full list of sites for which we have references recorded.\
See [[Federation Scraper]] to enlarge this list.",
...(await readDirz('data')).map(i=>i.name)
])
async function readDirz(path) {
let result = []
let fileInfo = await Deno.stat(path);
if (!fileInfo.isDirectory) {
console.log(`path ${path} is not a directory.`);
return [];
}
for await (let f of Deno.readDir(path)) {
result.push(f.name)
}
return result
}
async function referencedSites(siteName) {
let sites = new Set();
let siteDir = `data/${siteName}`;
if (!await exists(siteDir)) {
console.log(`WARN: Site ${siteDir} doesn't exist`);
return sites;
}
let files = await readDirz(siteDir);
for (let file of files) {
let filename = `${siteDir}/${file.name}`;
if (!await exists(filename)) {
console.log(`WARN: ${filename} doesn't exist`);
continue;
}
let contents = await readFileStr(filename);
let localSites = JSON.parse(contents);
localSites.forEach((s) => sites.add(s));
}
return sites;
}
async function oneDegreeAway(siteName) {
let sites = new Set();
(await referencedSites(siteName)).forEach((s) => sites.add(s));
return sites;
}
async function anotherDegreeAway(someDegree) {
let anotherDegree = new Set();
for (let site of someDegree) {
anotherDegree.add(site);
(await oneDegreeAway(site)).forEach((s) => anotherDegree.add(s));
}
return anotherDegree;
}
async function twoDegreesAway(siteName) {
let oneDegree = await oneDegreeAway(siteName);
return anotherDegreeAway(oneDegree);
}
async function threeDegreesAway(siteName) {
let twoDegrees = await twoDegreesAway(siteName);
return anotherDegreeAway(twoDegrees);
}