-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.ts
executable file
·161 lines (151 loc) · 3.98 KB
/
cli.ts
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#! /usr/bin/env bun
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import LibroFmClient from "@/LibroFmClient";
import InputHandler from "@/lib/InputHandler";
import logger from "@/lib/Logger";
const SCRIPT_NAME = "libro";
const client = new LibroFmClient();
await client.init();
const Command = yargs(hideBin(process.argv))
.scriptName(SCRIPT_NAME)
.command({
command: "list",
aliases: ["ls"],
describe: "Fetch list of books in library",
handler: async () => {
const audiobooks = await client.getLibrary();
logger.info(
`Found ${
Object.values(audiobooks).length
} audiobooks in library\n`
);
const booklist = Object.values(audiobooks);
booklist.forEach((ab, i) => {
logger.info(`Title: ${ab.title}`);
logger.info(`ISBN: ${ab.isbn}`);
logger.info(`Duration: ${ab.audiobook_info?.duration}`);
logger.info(
`Narrators: ${ab.audiobook_info?.narrators?.join(", ")}`
);
logger.info("");
});
},
})
.command(
"get [isbns...]",
"Download a book. Optionally provide ISBNs to download specific books.",
(yargs) =>
yargs
.positional("isbns", {
describe: "ISBN of the book to download",
type: "string",
})
.array("isbns")
.options({
overwrite: {
describe: "Should overwrite existing files",
type: "boolean",
default: false,
},
keepZip: {
describe: "Keep the downloaded zip file",
type: "boolean",
default: false,
},
ignoreLocal: {
describe: "Ignore local state and download all books",
type: "boolean",
default: false,
},
}),
async (argv) => {
const audiobookLibrary = await client.getLibrary();
let count = 0;
const downloadPromises: Promise<void>[] = [];
const downloadHelper = async (book: Audiobook) => {
logger.info(`Downloading: ${book.title}`);
const filepath = await client.downloadBook(
book,
argv.overwrite,
argv.keepZip
);
if (filepath) {
logger.info(`Downloaded ${book.title}`);
count++;
} else {
logger.info("No new books downloaded");
}
};
// If ISBNs are provided, download those books
// Otherwise, prompt the user to select a book
if (argv.isbns && argv.isbns.length > 0) {
for (const isbn of argv.isbns) {
logger.info(`Searching library for ISBN: ${isbn}`);
const book = audiobookLibrary[isbn];
if (book) {
downloadPromises.push(downloadHelper(book));
} else {
logger.error(`No book found with ISBN: ${isbn}`, {
fn: "Command.get",
});
}
}
} else {
const downloadChoice = await InputHandler.requestDownloadChoice(
Object.values(audiobookLibrary)
);
if (downloadChoice) {
const book = audiobookLibrary[downloadChoice];
if (book) {
downloadPromises.push(downloadHelper(book));
} else {
logger.error("Invalid selection", {
fn: "Command.get",
});
}
} else {
logger.error("Invalid selection", {
fn: "Command.get",
});
}
}
await Promise.all(downloadPromises);
if (count === 0) throw new Error("No books selected");
logger.info(`Downloaded ${count} new books.`);
}
)
.command({
command: "check",
describe: "Check for new books in library",
builder: (yargs) =>
yargs.options({
json: {
describe: "Should output as JSON",
type: "boolean",
default: false,
},
}),
handler: async (argv) => {
const audiobooks = await client.getNewBooks();
if (!argv.json) {
logger.info(
`Found ${audiobooks.length} new audiobooks in library\n`
);
audiobooks.forEach((ab, i) => {
logger.info(`Title: ${ab.title}`);
logger.info(`ISBN: ${ab.isbn}`);
logger.info(`Duration: ${ab.audiobook_info?.duration}`);
logger.info(
`Narrators: ${ab.audiobook_info?.narrators?.join(", ")}`
);
logger.info("");
});
} else {
logger.info(JSON.stringify(audiobooks, null, 2));
}
},
})
.demandCommand()
.parse();
export default Command;