-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbook.js
109 lines (93 loc) · 2.85 KB
/
book.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import config from "../config.js";
import { escapeCharacters } from "./lib/character-reference.js";
import fs from "node:fs/promises";
import { getDateDetails } from "./lib/get-date-details.js";
import path from "node:path";
import { runCommand } from "./lib/run-command.js";
import sharp from "sharp";
import util from "node:util";
const isReadBook = (title, book) => title === book.title;
const cancelFetch = (abortController) => {
abortController.abort();
};
const {
values: { asin, author, title },
} = util.parseArgs({
options: {
asin: {
type: "string",
},
author: {
type: "string",
},
title: {
type: "string",
},
},
});
if (!asin) {
throw new Error("--asin is required.");
}
if (!author) {
throw new Error("--author is required.");
}
if (!title) {
throw new Error("--title is required.");
}
if (!/^[A-Z0-9]{10}$/iu.test(asin)) {
throw new Error(`${asin} is not consisted of 10 alphanumeric characters.`);
}
const file = path.join(config.dir.data, "books.json");
const books = await fs.readFile(file, "utf8").then(JSON.parse);
if (books.find(isReadBook.bind(null, title))) {
throw new Error(`${title} has already been added.`);
}
const abortController = new AbortController();
const abortID = setTimeout(cancelFetch.bind(null, abortController), 5000);
try {
const cover = `https://m.media-amazon.com/images/P/${asin}.jpg`;
const res = await fetch(cover, { signal: abortController.signal });
if (!res.ok) {
throw new Error(`${res.status} ${res.statusText}`);
}
const img = await res.arrayBuffer();
const metadata = await sharp(Buffer.from(img)).metadata();
if (metadata.height === 1 && metadata.width === 1) {
throw new Error(`${title} does not have a cover image.`);
}
const link = `https://www.amazon.co.jp/exec/obidos/ASIN/${asin}/hail2unet-22`;
const titleEscaped = escapeCharacters(title);
const body = `<a href="${link}"><img alt="${titleEscaped}" height="${metadata.height}" loading="lazy" src="${cover}" width="${metadata.width}"></a>`;
const published = Date.now();
const dt = getDateDetails(published);
const formatted = JSON.stringify(
[
{
body,
description: author,
link,
published,
...dt,
title,
type: "book",
},
...books,
],
null,
2,
);
await fs.mkdir(path.dirname(file), { recursive: true });
await fs.writeFile(file, `${formatted}\n`);
await runCommand("git", ["add", "--", file]);
await runCommand("git", ["commit", `--message=Read ${asin}`]);
const twitter = new URL("https://x.com/intent/tweet");
twitter.searchParams.append("text", `${title} / ${author} ${link}`);
await runCommand("open", [twitter.href]);
} catch (e) {
if (e.name === "AbortError") {
throw new Error("Amazon image server does not respond in 5s.");
}
throw e;
} finally {
clearTimeout(abortID);
}