GyazoのURLリストからOCRテキストを一括取得するscript
code:sh
code:mod.ts
import { getImage } from "../deno-gyazo/mod.ts";
import { getGyazoToken } from "../scrapbox-userscript-std/rest.ts";
import { pool, Result } from "../async-lib/mod.ts";
export type { Result };
export interface ImageResult {
url: string;
text: string;
}
export async function* getOCRs(
images: Iterable<string | URL>,
accessToken: string,
): AsyncGenerator<Result<ImageResult>, void, unknown> {
const reader = pool(
10,
images,
async (image): Promise<ImageResult> => {
const id = image.toString().match(/\/(^\/+)$/)?.1; if (!id) throw new Error(Could not find Gyazo ID from "${image}");
const result = await getImage(id, { accessToken });
if (!result.ok) throw new Error(JSON.stringify(result.ok));
const { permalink_url, ocr } = result.value;
return { url: permalink_url, text: ocr?.description || "" };
},
);
// 順番を壊さずに返す
for (const result of reader) {
yield await result;
}
}