@astrofoundry/trendarr 0.10.2 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -23,12 +23,15 @@ trendarr books search <query> [--max-entries 10] [--json]
23
23
  trendarr books add <bookId> --format audiobook|ebook [--json]
24
24
  trendarr books releases <bookId> --format audiobook|ebook [--max-entries 10] [--json]
25
25
  trendarr books grab <chaptarrId> <guid> <indexerId> [--json]
26
+ trendarr books ready <chaptarrId> [--json]
26
27
  trendarr books send <chaptarrId> <device> [--json]
28
+ trendarr books ebooks [--json]
29
+ trendarr books send-item <itemId> <device> [--json]
27
30
  ```
28
31
 
29
32
  `trendarr <command> --help` shows parameters and output shapes. Movies below the IMDb min-rating (default 6.5) are dropped from trending; unrated movies are kept; `--min-rating 0` disables the filter. `recommend` ranks the titles TMDB recommends for your Radarr and Sonarr library by how many library titles lead to them, and keeps released titles not in the library with IMDb 7.0+ and 5,000 votes (movies) or 2,000 (series).
30
33
 
31
- `books` works with Chaptarr, which keeps an audiobook and an ebook entry per work. `add` lets Chaptarr pick the release and waits up to 2 minutes to report whether it grabbed one; `releases` then `grab` let you pick one yourself (narrator, size, format). `send` emails an ebook to a Kindle through Audiobookshelf's e-reader devices.
34
+ `books` works with Chaptarr, which keeps an audiobook and an ebook entry per work. `add` lets Chaptarr pick the release and waits up to 2 minutes to report whether it grabbed one; `releases` then `grab` let you pick one yourself (narrator, size, format). `ready` tells whether a book has its file and, for an ebook, whether Audiobookshelf has it. `send` emails an ebook to a Kindle through Audiobookshelf's e-reader devices. `ebooks` lists every ebook in Audiobookshelf, newest first, and `send-item` emails one of them by its item id.
32
35
 
33
36
  ## Development
34
37
 
package/dist/cli.js CHANGED
@@ -2,14 +2,14 @@
2
2
  import { createRequire } from 'node:module';
3
3
  import { Command, InvalidArgumentError } from 'commander';
4
4
  import { addMovies, addSeries, hasFailure } from './commands/add.js';
5
- import { addBook, bookReleases, grabRelease, searchBooks, sendBook } from './commands/books.js';
5
+ import { addBook, bookReady, bookReleases, grabRelease, listEbooks, searchBooks, sendBook, sendItem } from './commands/books.js';
6
6
  import { runInit } from './commands/init.js';
7
7
  import { runSabnzbd } from './commands/sabnzbd.js';
8
8
  import { CANDIDATE_LIMIT, getRecommendations } from './commands/recommend.js';
9
9
  import { getSearch } from './commands/search.js';
10
10
  import { getTrending } from './commands/trending.js';
11
11
  import { ConfigError, loadConfig } from './config.js';
12
- import { renderAddTable, renderBookAdd, renderBookGrab, renderBookSearchTable, renderBookSend, renderReleasesTable, renderRecommendTable, renderSabStatus, renderSearchDiscord, renderSearchTable, renderTrendingDiscord, renderTrendingTable, toJson, } from './format.js';
12
+ import { renderAddTable, renderBookAdd, renderBookGrab, renderBookReady, renderBookSearchTable, renderBookSend, renderEbookList, renderItemSend, renderReleasesTable, renderRecommendTable, renderSabStatus, renderSearchDiscord, renderSearchTable, renderTrendingDiscord, renderTrendingTable, toJson, } from './format.js';
13
13
  import { describeError } from './http.js';
14
14
  const pkg = createRequire(import.meta.url)('../package.json');
15
15
  const program = new Command();
@@ -329,7 +329,10 @@ Actions:
329
329
  add <bookId> --format <f> Monitor one format and let Chaptarr pick and download a release.
330
330
  releases <bookId> --format <f> List the releases you can pick from (narrator, size, quality).
331
331
  grab <chaptarrId> <guid> <indexerId> Download one release from that list.
332
- send <chaptarrId> <device> Email the ebook to a Kindle through Audiobookshelf.`);
332
+ ready <chaptarrId> Whether the book has a file and, for an ebook, is in Audiobookshelf.
333
+ send <chaptarrId> <device> Email the ebook to a Kindle through Audiobookshelf.
334
+ ebooks List every ebook in Audiobookshelf, newest first, with the Kindles.
335
+ send-item <itemId> <device> Email one Audiobookshelf ebook item to a Kindle.`);
333
336
  books
334
337
  .command('search')
335
338
  .description('search Chaptarr for books by title or author')
@@ -451,6 +454,31 @@ Examples:
451
454
  fail(err);
452
455
  }
453
456
  });
457
+ books
458
+ .command('ready')
459
+ .description('report whether a book has its file and, for an ebook, whether Audiobookshelf has it')
460
+ .argument('<chaptarrId>', 'book_id from books add, releases, or search', parsePositiveInt)
461
+ .option('--json', 'output JSON')
462
+ .addHelpText('after', `
463
+ Behavior: reads the book and its files from Chaptarr. For an ebook with a file, it looks for the
464
+ Audiobookshelf item in that folder, as send does, and lists the Kindles. Changes nothing.
465
+
466
+ Output (--json): { book_id, title, format, has_file, in_audiobookshelf, kindles }
467
+ in_audiobookshelf is false and kindles empty for an audiobook or a book without a file.
468
+ An ebook is ready to send when has_file and in_audiobookshelf are both true.
469
+
470
+ Examples:
471
+ trendarr books ready 6237 --json`)
472
+ .action(async (chaptarrId, opts) => {
473
+ try {
474
+ const { output, warnings } = await bookReady(loadConfig(), chaptarrId);
475
+ printWarnings(warnings);
476
+ process.stdout.write(`${opts.json === true ? toJson(output) : renderBookReady(output)}\n`);
477
+ }
478
+ catch (err) {
479
+ fail(err);
480
+ }
481
+ });
454
482
  books
455
483
  .command('send')
456
484
  .description('email an ebook to a Kindle through Audiobookshelf')
@@ -479,6 +507,57 @@ Examples:
479
507
  fail(err);
480
508
  }
481
509
  });
510
+ books
511
+ .command('ebooks')
512
+ .description('list every ebook in Audiobookshelf, newest first, with the Kindle devices')
513
+ .option('--json', 'output JSON')
514
+ .addHelpText('after', `
515
+ Behavior: reads every book library of Audiobookshelf and keeps the items with an ebook file,
516
+ including ebooks Chaptarr does not manage. Changes nothing.
517
+
518
+ Output (--json): { "ebooks": EbookItem[], "kindles": string[] }
519
+ EbookItem: { item_id, title, author, added_at } — item_id is Audiobookshelf's library item id,
520
+ for send-item; added_at is ISO 8601 or null.
521
+
522
+ Examples:
523
+ trendarr books ebooks --json`)
524
+ .action(async (opts) => {
525
+ try {
526
+ const { output, warnings } = await listEbooks(loadConfig());
527
+ printWarnings(warnings);
528
+ process.stdout.write(`${opts.json === true ? toJson(output) : renderEbookList(output)}\n`);
529
+ }
530
+ catch (err) {
531
+ fail(err);
532
+ }
533
+ });
534
+ books
535
+ .command('send-item')
536
+ .description('email one Audiobookshelf ebook item to a Kindle')
537
+ .argument('<itemId>', 'item_id from books ebooks')
538
+ .argument('<device>', 'Audiobookshelf e-reader device name, from books ebooks "kindles"')
539
+ .option('--json', 'output JSON')
540
+ .addHelpText('after', `
541
+ Behavior: asks Audiobookshelf to email that item's ebook to the device. The sender must be approved
542
+ on the Amazon account.
543
+
544
+ Output (--json): { item_id, device, status, detail }
545
+ status is one of: sent | unknown_device | error. Exit code 1 unless sent.
546
+
547
+ Examples:
548
+ trendarr books send-item ed3cbf09-3303-4714-ae11-f190d8279af5 "Kindle Astro"`)
549
+ .action(async (itemId, device, opts) => {
550
+ try {
551
+ const { output, warnings } = await sendItem(loadConfig(), itemId, device);
552
+ printWarnings(warnings);
553
+ process.stdout.write(`${opts.json === true ? toJson(output) : renderItemSend(output)}\n`);
554
+ if (output.status !== 'sent')
555
+ process.exitCode = 1;
556
+ }
557
+ catch (err) {
558
+ fail(err);
559
+ }
560
+ });
482
561
  if (process.argv.slice(2).length === 0) {
483
562
  program.outputHelp();
484
563
  process.exit(0);
@@ -8,7 +8,13 @@ const itemsSchema = z.object({
8
8
  results: z.array(z.object({
9
9
  id: z.string(),
10
10
  path: z.string(),
11
- media: z.object({ ebookFormat: z.string().nullish() }).nullish(),
11
+ addedAt: z.number().nullish(),
12
+ media: z
13
+ .object({
14
+ ebookFormat: z.string().nullish(),
15
+ metadata: z.object({ title: z.string().nullish(), authorName: z.string().nullish() }).nullish(),
16
+ })
17
+ .nullish(),
12
18
  })),
13
19
  });
14
20
  export class AudiobookshelfClient {
@@ -30,7 +36,14 @@ export class AudiobookshelfClient {
30
36
  }
31
37
  async items(libraryId) {
32
38
  const parsed = itemsSchema.parse(await this.request(`/api/libraries/${encodeURIComponent(libraryId)}/items?limit=0`));
33
- return parsed.results.map((i) => ({ id: i.id, path: i.path, ebookFormat: i.media?.ebookFormat ?? null }));
39
+ return parsed.results.map((i) => ({
40
+ id: i.id,
41
+ path: i.path,
42
+ ebookFormat: i.media?.ebookFormat ?? null,
43
+ title: i.media?.metadata?.title ?? null,
44
+ author: i.media?.metadata?.authorName ?? null,
45
+ addedAt: i.addedAt ?? null,
46
+ }));
34
47
  }
35
48
  async sendEbook(libraryItemId, deviceName) {
36
49
  await this.request('/api/emails/send-ebook-to-device', {
@@ -21,6 +21,9 @@ function audiobookshelf(config) {
21
21
  }
22
22
  return new AudiobookshelfClient(config.audiobookshelf.url, config.audiobookshelf.apiKey);
23
23
  }
24
+ function formatOf(mediaType) {
25
+ return mediaType === 'ebook' ? 'ebook' : 'audiobook';
26
+ }
24
27
  function preferred(books) {
25
28
  return books.find((b) => b.hasFiles) ?? books.find((b) => b.monitored) ?? books[0] ?? null;
26
29
  }
@@ -208,7 +211,7 @@ export async function grabRelease(config, bookId, guid, indexerId) {
208
211
  try {
209
212
  const book = await client.book(bookId);
210
213
  title = book.title;
211
- format = book.mediaType === 'ebook' ? 'ebook' : 'audiobook';
214
+ format = formatOf(book.mediaType);
212
215
  await client.setMonitored([bookId], true);
213
216
  await client.grab(guid, indexerId);
214
217
  result = { status: 'grabbed', detail: null };
@@ -219,6 +222,27 @@ export async function grabRelease(config, bookId, guid, indexerId) {
219
222
  const sabnzbd_paused = await sabPausedFor(config, client, format, warnings);
220
223
  return { output: { book_id: bookId, title, ...result, sabnzbd_paused }, warnings };
221
224
  }
225
+ async function audiobookshelfItem(abs, paths) {
226
+ const folders = new Set(paths.map((p) => dirname(p)));
227
+ for (const library of (await abs.libraries()).filter((l) => l.mediaType === 'book')) {
228
+ const item = (await abs.items(library.id)).find((i) => folders.has(i.path) && i.ebookFormat !== null);
229
+ if (item !== undefined)
230
+ return item.id;
231
+ }
232
+ return null;
233
+ }
234
+ export async function bookReady(config, bookId) {
235
+ const client = chaptarr(config);
236
+ const book = await client.book(bookId);
237
+ const paths = await client.bookFilePaths(bookId);
238
+ const base = { book_id: bookId, title: book.title, format: formatOf(book.mediaType), has_file: paths.length > 0 };
239
+ if (base.format !== 'ebook' || paths.length === 0) {
240
+ return { output: { ...base, in_audiobookshelf: false, kindles: [] }, warnings: [] };
241
+ }
242
+ const abs = audiobookshelf(config);
243
+ const item = await audiobookshelfItem(abs, paths);
244
+ return { output: { ...base, in_audiobookshelf: item !== null, kindles: await abs.ereaderDevices() }, warnings: [] };
245
+ }
222
246
  export async function sendBook(config, bookId, device) {
223
247
  const client = chaptarr(config);
224
248
  const abs = audiobookshelf(config);
@@ -229,19 +253,44 @@ export async function sendBook(config, bookId, device) {
229
253
  if (!(await abs.ereaderDevices()).includes(device)) {
230
254
  return { output: { ...base, title, status: 'unknown_device', detail: null }, warnings: [] };
231
255
  }
232
- const folders = new Set((await client.bookFilePaths(bookId)).map((p) => dirname(p)));
233
- if (folders.size === 0)
256
+ const paths = await client.bookFilePaths(bookId);
257
+ if (paths.length === 0)
234
258
  return { output: { ...base, title, status: 'no_file', detail: null }, warnings: [] };
235
- for (const library of (await abs.libraries()).filter((l) => l.mediaType === 'book')) {
236
- const item = (await abs.items(library.id)).find((i) => folders.has(i.path) && i.ebookFormat !== null);
237
- if (item === undefined)
259
+ const item = await audiobookshelfItem(abs, paths);
260
+ if (item === null)
261
+ return { output: { ...base, title, status: 'not_in_audiobookshelf', detail: null }, warnings: [] };
262
+ await abs.sendEbook(item, device);
263
+ return { output: { ...base, title, status: 'sent', detail: null }, warnings: [] };
264
+ }
265
+ catch (err) {
266
+ return { output: { ...base, title, status: 'error', detail: httpDetail(err) }, warnings: [] };
267
+ }
268
+ }
269
+ export async function listEbooks(config) {
270
+ const abs = audiobookshelf(config);
271
+ const found = [];
272
+ for (const library of (await abs.libraries()).filter((l) => l.mediaType === 'book')) {
273
+ for (const i of await abs.items(library.id)) {
274
+ if (i.ebookFormat === null)
238
275
  continue;
239
- await abs.sendEbook(item.id, device);
240
- return { output: { ...base, title, status: 'sent', detail: null }, warnings: [] };
276
+ const added_at = i.addedAt === null ? null : new Date(i.addedAt).toISOString();
277
+ found.push({ item: { item_id: i.id, title: i.title ?? i.path, author: i.author, added_at }, addedAt: i.addedAt ?? 0 });
278
+ }
279
+ }
280
+ const ebooks = found.sort((a, b) => b.addedAt - a.addedAt).map((f) => f.item);
281
+ return { output: { ebooks, kindles: await abs.ereaderDevices() }, warnings: [] };
282
+ }
283
+ export async function sendItem(config, itemId, device) {
284
+ const abs = audiobookshelf(config);
285
+ const base = { item_id: itemId, device };
286
+ try {
287
+ if (!(await abs.ereaderDevices()).includes(device)) {
288
+ return { output: { ...base, status: 'unknown_device', detail: null }, warnings: [] };
241
289
  }
242
- return { output: { ...base, title, status: 'not_in_audiobookshelf', detail: null }, warnings: [] };
290
+ await abs.sendEbook(itemId, device);
291
+ return { output: { ...base, status: 'sent', detail: null }, warnings: [] };
243
292
  }
244
293
  catch (err) {
245
- return { output: { ...base, title, status: 'error', detail: httpDetail(err) }, warnings: [] };
294
+ return { output: { ...base, status: 'error', detail: httpDetail(err) }, warnings: [] };
246
295
  }
247
296
  }
package/dist/format.js CHANGED
@@ -142,3 +142,21 @@ export function renderBookSend(output) {
142
142
  const detail = output.detail === null ? '' : ` (${output.detail})`;
143
143
  return `${output.title ?? `book ${output.book_id}`} → ${output.device}: ${statusColor(output.status, ['sent'])}${detail}`;
144
144
  }
145
+ export function renderBookReady(output) {
146
+ const file = output.has_file ? pc.green('file') : pc.yellow('no file');
147
+ const abs = output.format !== 'ebook' ? '' : output.in_audiobookshelf ? `, ${pc.green('in Audiobookshelf')}` : `, ${pc.yellow('not in Audiobookshelf')}`;
148
+ const kindles = output.kindles.length === 0 ? '' : `\nKindles: ${output.kindles.join(', ')}`;
149
+ return `${output.title} [${output.format}] ${file}${abs}${kindles}`;
150
+ }
151
+ export function renderEbookList(output) {
152
+ if (output.ebooks.length === 0)
153
+ return 'No ebooks.';
154
+ const header = `${cell('TITLE', 44)} ${cell('AUTHOR', 24)} ${cell('ADDED', 10)} ITEM`;
155
+ const rows = output.ebooks.map((e) => `${cell(e.title, 44)} ${cell(dash(e.author), 24)} ${cell(e.added_at?.slice(0, 10) ?? '-', 10)} ${e.item_id}`);
156
+ const kindles = output.kindles.length === 0 ? 'Kindles: none' : `Kindles: ${output.kindles.join(', ')}`;
157
+ return [pc.bold(header), ...rows, '', kindles].join('\n');
158
+ }
159
+ export function renderItemSend(output) {
160
+ const detail = output.detail === null ? '' : ` (${output.detail})`;
161
+ return `${output.item_id} → ${output.device}: ${statusColor(output.status, ['sent'])}${detail}`;
162
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrofoundry/trendarr",
3
- "version": "0.10.2",
3
+ "version": "0.12.0",
4
4
  "description": "TMDB trending → OMDb ratings → Radarr/Sonarr adds → SABnzbd control, from the terminal",
5
5
  "scripts": {
6
6
  "build": "tsc -p tsconfig.build.json && chmod +x dist/cli.js",