longPageRankingInDeno.ts
code:rank.ts
interface ScrapboxPagesJson {
count: number;
pages: { title: string; charsCount: number }[];
}
const getList = async (projectName: string, offset: number) => {
const pageRes = await fetch(
projectName +
"/?limit=100&skip=" +
encodeURIComponent(offset)
);
if (pageRes.ok) {
const pageJson: Readonly<ScrapboxPagesJson> = await pageRes.json();
return pageJson.pages;
} else {
return [];
}
};
const getPage = async (projectName: string, pageName: string) => {
try {
const pageRes = await fetch(
projectName +
"/" +
encodeURIComponent(pageName)
);
if (pageRes.ok) {
const json: { charsCount: number; title: string } = await pageRes.json();
return json;
} else {
throw Error;
}
} catch (e) {
throw Error;
}
};
const allpagelist = async (pjname: string) => {
const spinner = Spinner.getInstance();
spinner.start("Fetching All Page List...");
const projectName: string = pjname;
let marked: { title: string; charsCount: number }[] = [];
if (!pageRes.ok) {
throw Error;
}
const pageJson: Readonly<ScrapboxPagesJson> = await pageRes.json();
const arrayRange = (start: number, stop: number, step: number) =>
Array.from(
{ length: (stop - start) / step + 1 },
(_, index) => start + index * step
);
for (const element of arrayRange(0, pageJson.count, 100)) {
spinner.setText(
"Fetching All Page List...[" +
element.toString().padStart(pageJson.count.toString().length) +
"/" +
pageJson.count +
"]"
);
const promiseList: Promise<void>[] = [];
for (const element2 of await getList(projectName, element)) {
promiseList.push(
getPage(pjname, element2.title)
.then((ele3) => {
marked.push({ charsCount: ele3.charsCount, title: ele3.title });
})
.catch(() => {})
);
}
await Promise.all(promiseList);
}
spinner.succeed("Fetched All Page List");
spinner.stop();
marked.sort((a, b) => b.charsCount - a.charsCount);
let scrapchar = "";
for (const element2 of marked.slice(0, 500)) {
scrapchar +=
" + element2.title + " (" + element2.charsCount + "文字)\n";
}
return scrapchar;
};
await Deno.writeTextFile(
"villagepumpsoko.txt",
await allpagelist("villagepump")
);