ある日時以降に更新されたすべてのscrapboxのページを取得するscript
getAllPages()を軽くしたもの
前回取得した日時を使えば、更新分だけをfetchできるので、サーバー負荷を低減し処理の高速化に貢献できる
$ deno check --remote -r=https://scrapbox.io https://scrapbox.io/api/code/takker/ある日時以降に更新されたすべてのscrapboxのページを取得するscript/mod.ts
https://scrapbox-bundler.vercel.app?url=https://scrapbox.io/api/code/takker/ある日時以降に更新されたすべてのscrapboxのページを取得するscript/mod.ts&bundle&minify&run&reload
code:mod.ts
import {
BaseOptions,
Fetch,
listPages,
getPage,
InvalidFollowingIdError,
Result,
TooLongURIError,
} from "../scrapbox-userscript-std/rest.ts";
import {
Page,
ExportedPage,
NotFoundError,
NotLoggedInError,
NotMemberError,
} from "../scrapbox-jp%2Ftypes/rest.ts";
import { makeThrottle } from "https://raw.githubusercontent.com/takker99/Scrapbubble/0.7.4/throttle.ts";
export async function* getAllUpdatedPages(project: string, lastChecked: number, options?: BaseOptions): AsyncGenerator<
Result<Page, NotFoundError | NotLoggedInError | NotMemberError | TooLongURIError>,
void,
unknown
{
const throttle = makeThrottle<Response>(5);
const { fetch: fetch_ = globalThis.fetch, ...rest } = options ?? {};
const fetch: Fetch = (args) => throttle(() => fetch_(args));
let skip = 0;
while (true) {
const result = await listPages(project, { ...options, limit: 1000, skip });
if (!result.ok) {
yield result;
return;
}
const promises = result.value.pages.flatMap(
(page) => page.updated > lastChecked ? getPage(project, page.title, { ...rest, fetch }) : []
);
for await (const promise of promises) {
yield await promise;
}
if ((result.value.pages.pop()?.updated ?? 0) <= lastChecked) break;
skip += 1000;
}
}
#2023-03-17 09:36:17