複数の画像ファイルをGyazoにuploadするscript
2022-09-16
08:57:49 pool()
で複数の画像を一度にuploadする処理を無くした
upload順序を保証するようにしたので、複数の画像を一度にuploadしようとしても、結局は1枚ずつuploadすることになる
2022-09-11
11:45:17 upload順序を保証する
2022-07-26
19:27:14 末尾に連番をつけないようにするoptionを追加した
18:59:34 created
を指定しない
2022-04-21
一部optionを削った
また必要になったら入れる
使用例
localから複数の画像ファイルを指定してuploadし、uploadしたGyazo URLのlistをJSON形式でdownloadする
buildしたコードの
タイトル
を書き換えて実行する
お好みで refererURL
や app
も指定できる
$ deno check --remote -r=https://scrapbox.io https://scrapbox.io/api/code/takker/複数の画像ファイルをGyazoにuploadするscript/script.ts
script.tsimport { uploadBulk } from "./upload.ts";
import { useStatusBar } from "../scrapbox-userscript-std/dom.ts";
import { upload } from "../scrapbox-file-uploader/mod.ts";
await (async () => {
const URLs = [] as string[];
const { render, dispose } = useStatusBar();
const errorBar = useStatusBar();
const infoBar = useStatusBar();
try {
const fileList = await upload({ accept: "image/jpeg, image/png", 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;
const errors = [] as number[];
for await (const result of uploadBulk(
files,
{
title: "タイトル",
noIndex: true,
},
)) {
counter++;
if (!result.success) {
if (!(result.reason instanceof Error)) throw result.reason;
console.error(result.reason);
errors.push(counter);
errorBar.render(
{ type: "exclamation-triangle" },
{
type: "text",
text: `${result.reason.name} ${result.reason.message}`,
},
);
continue;
} else {
const { permalink_url, name, index } = result.value;
URLs[index] = permalink_url;
infoBar.render(
{ type: "text", text: `${name} ${permalink_url}` },
);
}
render(
{ type: "spinner" },
{
type: "text",
text: `${files.length} images, ${
counter - errors.length
} uploaded, ${errors.length} failed`,
},
);
}
render(
{ type: "check-circle" },
{ type: "text", text: `Finish uploading.` },
);
console.log(URLs);
console.log(errors);
const blob = new Blob([JSON.stringify(URLs)], { type: "application/json" });
window.open(URL.createObjectURL(blob));
} finally {
setTimeout(() => {
errorBar.dispose();
infoBar.dispose();
dispose();
}, 1000);
}
})();
code
$ deno check --remote -r=https://scrapbox.io https://scrapbox.io/api/code/takker/複数の画像ファイルをGyazoにuploadするscript/upload.ts
upload.tsimport { upload, UploadResult } from "../deno-gyazo/mod.ts";
import { getGyazoToken } from "../scrapbox-userscript-std/rest.ts";
export type Result<T> = { success: false; reason: unknown; } | { success: true; value: T; };
export interface UploadOptions {
title?: string;
refererURL?: string;
app?: string;
noIndex?: boolean;
accessToken?: string;
}
export async function* uploadBulk(
images: File[],
uploadOptions?: UploadOptions,
): AsyncGenerator<Result<UploadResult & { index: number; name: string; }>, void, unknown> {
const { noIndex, accessToken: token, ...options } = uploadOptions ?? {};
// access token取得
const accessToken = token ?? await (async () => {
const result = await getGyazoToken();
if (!result.ok) throw new Error(JSON.stringify(result.value));
const accessToken = result.value;
if (!accessToken) throw new Error("Could not get the access token");
return accessToken;
})();
const zero = (n: number) => `${n}`.padStart(`${images.length}`.length, "0");
// 一枚ずつuploadして返す
for (let index = 0; index < images.length; index++) {
const result = await upload(images[index], {
accessToken,
description: `${images[index].name}${noIndex === true ? "" :` ${zero(index + 1)}`}`,
...options,
});
if (!result.ok) {
yield { success: false, reason: result.value };
continue;
}
yield { success: true, value: { index, name: images[index].name, ...result.value } };
}
}