Scrapbox書籍を作るUserScript@0.2.0
必要なもの
gyazoの画像リスト
JSON形式
コードの一例
for Deno
なぜかspinnerの大部分が消えずに残ってしまう
しかも3回重複する
バグッてる?
03:05:18 改行が含まれていたのが原因だった
\nを取り除いたら直った
必須オプションとして-t <gyazoのaccess token>を渡す
引数に①gyazoの画像リストのファイルパス②scrapbox書籍用目次のcsvファイルへのパスを順に渡す
code:main.ts
/// <reference lib="deno.ns" />
import { getOCRs } from "../GyazoのURLリストからOCRテキストを一括取得するscript/mod.ts";
import { parse, sort, stringify, Page } from "../Scrapbox書籍のformat@0.2.0/mod.ts";
import { getGyazoToken } from "../scrapbox-userscript-std/rest.ts";
import Parser from "../papaparse/mod.ts";
import type { ImportedLightPage } from "../scrapbox-jp%2Ftypes/rest.ts";
.name("Scrapbox書籍を作るUserScript")
.description("GyazoのURLリストとscrapbox書籍用目次CSVからScrapbox書籍のJSONファイルを作る")
.version("v0.2.0")
.arguments("<gyazoListPath:string> <csvPath:string>")
.option("-t, --token <token:string>", "Gyazo Access Token")
.option("--offset <offset:number>", "scrapbox書籍用目次CSVに書かれたページ番号をoffsetだけずらす", { default: 0 })
.parse(Deno.args);
const listURL = toFileUrl(
isAbsolute(gyazoListPath) ? gyazoListPath: resolve(Deno.cwd(), gyazoListPath)
);
const gyazoList = (await (await fetch(listURL)).json()) as string[];
// OCRを取り込む
const images: string[] = [];
const ocrs: string[] = [];
let counter = 0;
const errors = [] as number[];
const spinner = new Spinner();
spinner.message = "Download OCRs...";
spinner.start();
const header = () => ${counter - errors.length}/${gyazoList.length} got, ${errors.length} failed: ;
for await (const result of getOCRs(gyazoList, token!)) {
counter++;
if (!result.success) {
if (!(result.reason instanceof Error)) throw result.reason;
console.error(result.reason);
errors.push(counter);
spinner.message = ${header()}${result.reason}
} else {
images.push(result.value.url);
ocrs.push(result.value.text);
spinner.message = ${header()}${[...result.value.text.replaceAll("\n","")].slice(0, 10).join("")};
}
}
spinner.message = "creating the json file...";
// 目次を取り込む
const csvURL = toFileUrl(
isAbsolute(csvPath) ? csvPath: resolve(Deno.cwd(), csvPath)
);
const csv = await (await fetch(csvURL)).text();
const { data } = Parser.parse<string[]>(csv);
// JSONを作る
const pages: ImportedLightPage[] = [];
for (const page of sort(parse(data))) {
const lines = stringify(page, images, ocrs, offset).split("\n");
pages.push({ title: lines0, lines }); }
await Deno.writeTextFile("scrapbox.json", JSON.stringify({ pages }));
spinner.stop();
console.log("created the json file.");
テキストのみ
code:onlyText.ts
import { parse, sort, stringify, Page } from "../Scrapbox書籍のformat@0.2.0/mod.ts";
import Parser from "../papaparse/mod.ts";
import type { ImportedLightPage } from "../scrapbox-jp%2Ftypes/rest.ts";
const ocrs = JSON.parse(await Deno.readTextFile(Deno.args0)) as string[]; console.log(creating the json file...);
// 目次を取り込む
const csv = await Deno.readTextFile(Deno.args1); const { data } = Parser.parse<string[]>(csv);
// JSONを作る
const pages: ImportedLightPage[] = [];
for (const page of sort(parse(data))) {
const lines = stringify(page, new Array<string>(ocrs.length).fill(""), ocrs).split("\n");
pages.push({ title: lines0, lines }); }
await Deno.writeTextFile("scrapbox.json", JSON.stringify({ pages }));
console.log("created the json file.");
text fileをjsonにする (web browser用)
code:makeJson.ts
import { useStatusBar } from "../scrapbox-userscript-std/dom.ts";
import { upload } from "../scrapbox-file-uploader/mod.ts";
const main = async () => {
const textList = [] as string[];
const { render, dispose } = useStatusBar();
try {
const fileList = await upload({ accept: "text/plain", multiple: true });
if (!fileList) return;
const compare = new Intl.Collator().compare;
const files = Array.from(fileList)
.sort((a, b) => compare(a.name, b.name));
let counter = 0;
for (const file of files) {
textList.push(await file.text());
render(
{ type: "spinner" },
{
type: "text",
text: ${files.length} files, ${counter} proceed,
},
);
}
render(
{ type: "check-circle" },
{ type: "text", text: Finish process. },
);
console.log(textList);
window.open(URL.createObjectURL(blob));
} finally {
setTimeout(() => {
dispose();
}, 1000);
}
};
await main();