///
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";
import { isAbsolute, toFileUrl, resolve } from "https://deno.land/std@0.213.0/path/mod.ts";
import { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.3/command/mod.ts";
import { Spinner } from "https://deno.land/std@0.212.0/cli/mod.ts";
const { args: [gyazoListPath, csvPath], options: { token, offset } } = await new Command()
.name("Scrapbox書籍を作るUserScript")
.description("GyazoのURLリストとscrapbox書籍用目次CSVからScrapbox書籍のJSONファイルを作る")
.version("v0.2.0")
.arguments(" ")
.option("-t, --token ", "Gyazo Access Token")
.option("--offset ", "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(csv);
// JSONを作る
const pages: ImportedLightPage[] = [];
for (const page of sort(parse(data))) {
const lines = stringify(page, images, ocrs, offset).split("\n");
pages.push({ title: lines[0], lines });
}
await Deno.writeTextFile("scrapbox.json", JSON.stringify({ pages }));
spinner.stop();
console.log("created the json file.");