@astrofoundry/trendarr 0.8.0 → 0.9.1
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 +8 -1
- package/dist/cli.js +171 -1
- package/dist/clients/audiobookshelf.js +42 -0
- package/dist/clients/chaptarr.js +183 -0
- package/dist/commands/add.js +1 -1
- package/dist/commands/books.js +197 -0
- package/dist/commands/init.js +30 -1
- package/dist/config.js +3 -0
- package/dist/format.js +45 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ TMDB weekly trending in the terminal — IMDb ratings via OMDb, Radarr/Sonarr li
|
|
|
8
8
|
pnpm add -g @astrofoundry/trendarr
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
Node 24+. Run `trendarr init` once — prompts for TMDB API Read Access Token (themoviedb.org/settings/api), OMDb, Radarr, Sonarr, and SABnzbd credentials, validates each live, and saves to `~/.trendarr/config.json` (mode 600). No environment variables.
|
|
11
|
+
Node 24+. Run `trendarr init` once — prompts for TMDB API Read Access Token (themoviedb.org/settings/api), OMDb, Radarr, Sonarr, and SABnzbd credentials, validates each live, and saves to `~/.trendarr/config.json` (mode 600). Books are optional: init then also asks for Chaptarr and Audiobookshelf. No environment variables.
|
|
12
12
|
|
|
13
13
|
## Commands
|
|
14
14
|
|
|
@@ -19,10 +19,17 @@ trendarr recommend [--max-entries 10] [--type movies|series] [--exclude tt1,tt2]
|
|
|
19
19
|
trendarr add-movies <imdbIds...> [--json]
|
|
20
20
|
trendarr add-series <imdbIds...> [--json]
|
|
21
21
|
trendarr sabnzbd status|pause|resume|unpause [--json]
|
|
22
|
+
trendarr books search <query> [--max-entries 10] [--json]
|
|
23
|
+
trendarr books add <bookId> --format audiobook|ebook [--json]
|
|
24
|
+
trendarr books releases <bookId> --format audiobook|ebook [--max-entries 10] [--json]
|
|
25
|
+
trendarr books grab <chaptarrId> <guid> <indexerId> [--json]
|
|
26
|
+
trendarr books send <chaptarrId> <device> [--json]
|
|
22
27
|
```
|
|
23
28
|
|
|
24
29
|
`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).
|
|
25
30
|
|
|
31
|
+
`books` works with Chaptarr, which keeps an audiobook and an ebook entry per work. `add` lets Chaptarr pick the release; `releases` then `grab` let you pick one yourself (narrator, size, format). `send` emails an ebook to a Kindle through Audiobookshelf's e-reader devices.
|
|
32
|
+
|
|
26
33
|
## Development
|
|
27
34
|
|
|
28
35
|
```bash
|
package/dist/cli.js
CHANGED
|
@@ -2,13 +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
6
|
import { runInit } from './commands/init.js';
|
|
6
7
|
import { runSabnzbd } from './commands/sabnzbd.js';
|
|
7
8
|
import { CANDIDATE_LIMIT, getRecommendations } from './commands/recommend.js';
|
|
8
9
|
import { getSearch } from './commands/search.js';
|
|
9
10
|
import { getTrending } from './commands/trending.js';
|
|
10
11
|
import { ConfigError, loadConfig } from './config.js';
|
|
11
|
-
import { renderAddTable, renderRecommendTable, renderSabStatus, renderSearchDiscord, renderSearchTable, renderTrendingDiscord, renderTrendingTable, toJson, } from './format.js';
|
|
12
|
+
import { renderAddTable, renderBookAdd, renderBookGrab, renderBookSearchTable, renderBookSend, renderReleasesTable, renderRecommendTable, renderSabStatus, renderSearchDiscord, renderSearchTable, renderTrendingDiscord, renderTrendingTable, toJson, } from './format.js';
|
|
12
13
|
import { describeError } from './http.js';
|
|
13
14
|
const pkg = createRequire(import.meta.url)('../package.json');
|
|
14
15
|
const program = new Command();
|
|
@@ -29,6 +30,16 @@ function parseRating(value) {
|
|
|
29
30
|
throw new InvalidArgumentError('must be a number between 0 and 10');
|
|
30
31
|
return n;
|
|
31
32
|
}
|
|
33
|
+
function parseFormat(value) {
|
|
34
|
+
if (value === 'audiobook' || value === 'ebook')
|
|
35
|
+
return value;
|
|
36
|
+
throw new InvalidArgumentError("must be 'audiobook' or 'ebook'");
|
|
37
|
+
}
|
|
38
|
+
function parseBookId(value) {
|
|
39
|
+
if (/^[a-z]{2,10}:[\w:-]{1,40}$/.test(value))
|
|
40
|
+
return value;
|
|
41
|
+
throw new InvalidArgumentError('not a Chaptarr book id (e.g. gr:3060926 or hc:194793)');
|
|
42
|
+
}
|
|
32
43
|
function parseImdbList(value) {
|
|
33
44
|
const ids = [...new Set(value.split(',').map((p) => p.trim()).filter((p) => p.length > 0))];
|
|
34
45
|
const bad = ids.find((id) => !/^tt\d+$/.test(id));
|
|
@@ -66,6 +77,7 @@ Commands:
|
|
|
66
77
|
add-movies <imdbIds...> Add movies to Radarr by IMDb id and start a search.
|
|
67
78
|
add-series <imdbIds...> Add series to Sonarr by IMDb id and start a search.
|
|
68
79
|
sabnzbd <action> Read or toggle the SABnzbd queue pause state.
|
|
80
|
+
books <action> Audiobooks and ebooks: Chaptarr search, adds, release picks, Send to Kindle.
|
|
69
81
|
|
|
70
82
|
Run "trendarr <command> --help" for that command's parameters and output shape.`);
|
|
71
83
|
program
|
|
@@ -304,6 +316,164 @@ Examples:
|
|
|
304
316
|
fail(err);
|
|
305
317
|
}
|
|
306
318
|
});
|
|
319
|
+
const books = program
|
|
320
|
+
.command('books')
|
|
321
|
+
.description('audiobooks and ebooks: Chaptarr search and adds, Audiobookshelf Send to Kindle')
|
|
322
|
+
.addHelpText('after', `
|
|
323
|
+
Needs the optional "chaptarr" section of the config (and "audiobookshelf" for send and the Kindle list).
|
|
324
|
+
Chaptarr keeps one book per format: an audiobook id and an ebook id for the same work. The book id
|
|
325
|
+
used by add and releases is Chaptarr's foreign book id (e.g. gr:3060926), from books search.
|
|
326
|
+
|
|
327
|
+
Actions:
|
|
328
|
+
search <query> Find works, with each format's state and the Kindle devices.
|
|
329
|
+
add <bookId> --format <f> Monitor one format and let Chaptarr pick and download a release.
|
|
330
|
+
releases <bookId> --format <f> List the releases you can pick from (narrator, size, quality).
|
|
331
|
+
grab <chaptarrId> <guid> <indexerId> Download one release from that list.
|
|
332
|
+
send <chaptarrId> <device> Email the ebook to a Kindle through Audiobookshelf.`);
|
|
333
|
+
books
|
|
334
|
+
.command('search')
|
|
335
|
+
.description('search Chaptarr for books by title or author')
|
|
336
|
+
.argument('<query>', 'title or author, e.g. "pride and prejudice"')
|
|
337
|
+
.option('--max-entries <n>', 'results', parsePositiveInt)
|
|
338
|
+
.option('--json', 'output JSON')
|
|
339
|
+
.addHelpText('after', `
|
|
340
|
+
Parameters:
|
|
341
|
+
<query> Title or author words. Quote multi-word queries.
|
|
342
|
+
--max-entries <n> Results (positive integer). Default: 10 (config defaults.maxEntries).
|
|
343
|
+
--json Output JSON instead of the table.
|
|
344
|
+
|
|
345
|
+
Output (--json): { "books": BookItem[], "kindles": string[] }
|
|
346
|
+
BookItem: { rank, title, author, foreign_book_id, votes, audiobook: Format, ebook: Format }
|
|
347
|
+
Format: { book_id, monitored, has_file, narrator } — book_id is Chaptarr's id for that format,
|
|
348
|
+
null when Chaptarr does not have it yet. kindles lists the Audiobookshelf e-reader device names;
|
|
349
|
+
empty when Audiobookshelf is not configured or unreachable (then a warning).
|
|
350
|
+
|
|
351
|
+
Examples:
|
|
352
|
+
trendarr books search "the two towers"
|
|
353
|
+
trendarr books search austen --json`)
|
|
354
|
+
.action(async (query, opts) => {
|
|
355
|
+
try {
|
|
356
|
+
const { output, warnings } = await searchBooks(loadConfig(), query, opts);
|
|
357
|
+
printWarnings(warnings);
|
|
358
|
+
process.stdout.write(`${opts.json === true ? toJson(output) : renderBookSearchTable(output)}\n`);
|
|
359
|
+
}
|
|
360
|
+
catch (err) {
|
|
361
|
+
fail(err);
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
books
|
|
365
|
+
.command('add')
|
|
366
|
+
.description('monitor one format of a book and start a search')
|
|
367
|
+
.argument('<bookId>', 'foreign book id from books search, e.g. gr:3060926', parseBookId)
|
|
368
|
+
.requiredOption('--format <format>', "'audiobook' or 'ebook'", parseFormat)
|
|
369
|
+
.option('--json', 'output JSON')
|
|
370
|
+
.addHelpText('after', `
|
|
371
|
+
Behavior: adds the book (and its author, with no other book monitored) when Chaptarr does not have it,
|
|
372
|
+
or monitors the existing entry, then starts a search; Chaptarr picks the release by the format's
|
|
373
|
+
quality profile. A format that already has a file reports already_exists.
|
|
374
|
+
|
|
375
|
+
Output (--json): { foreign_book_id, format, book_id, title, status, detail, sabnzbd_paused }
|
|
376
|
+
status is one of: added | searching | already_exists | not_found | error. Exit code 1 on not_found or error.
|
|
377
|
+
|
|
378
|
+
Examples:
|
|
379
|
+
trendarr books add gr:3060926 --format ebook`)
|
|
380
|
+
.action(async (bookId, opts) => {
|
|
381
|
+
try {
|
|
382
|
+
const config = loadConfig();
|
|
383
|
+
const { output, warnings } = await addBook(config, bookId, opts.format);
|
|
384
|
+
printWarnings(warnings);
|
|
385
|
+
process.stdout.write(`${opts.json === true ? toJson(output) : renderBookAdd(output)}\n`);
|
|
386
|
+
if (output.status === 'not_found' || output.status === 'error')
|
|
387
|
+
process.exitCode = 1;
|
|
388
|
+
}
|
|
389
|
+
catch (err) {
|
|
390
|
+
fail(err);
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
books
|
|
394
|
+
.command('releases')
|
|
395
|
+
.description('list the releases of one format to pick from')
|
|
396
|
+
.argument('<bookId>', 'foreign book id from books search, e.g. gr:3060926', parseBookId)
|
|
397
|
+
.requiredOption('--format <format>', "'audiobook' or 'ebook'", parseFormat)
|
|
398
|
+
.option('--max-entries <n>', 'releases', parsePositiveInt)
|
|
399
|
+
.option('--json', 'output JSON')
|
|
400
|
+
.addHelpText('after', `
|
|
401
|
+
Behavior: adds the book unmonitored when Chaptarr does not have it, then runs Chaptarr's interactive
|
|
402
|
+
search (can take a minute). Releases in a format the profile does not want, and torrents without
|
|
403
|
+
enough seeders, are left out. Order is Chaptarr's. Grab one within 30 minutes, while Chaptarr
|
|
404
|
+
still caches the search.
|
|
405
|
+
|
|
406
|
+
Output (--json): { foreign_book_id, format, book_id, title, releases: BookRelease[] }
|
|
407
|
+
BookRelease: { rank, title, size, quality, protocol, indexer, seeders, age_days, guid, indexer_id,
|
|
408
|
+
rejections } — size in bytes; rejections are Chaptarr's reasons it would not pick it by itself.
|
|
409
|
+
|
|
410
|
+
Examples:
|
|
411
|
+
trendarr books releases gr:3060926 --format audiobook --max-entries 5`)
|
|
412
|
+
.action(async (bookId, opts) => {
|
|
413
|
+
try {
|
|
414
|
+
const { output, warnings } = await bookReleases(loadConfig(), bookId, opts.format, opts);
|
|
415
|
+
printWarnings(warnings);
|
|
416
|
+
process.stdout.write(`${opts.json === true ? toJson(output) : renderReleasesTable(output)}\n`);
|
|
417
|
+
}
|
|
418
|
+
catch (err) {
|
|
419
|
+
fail(err);
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
books
|
|
423
|
+
.command('grab')
|
|
424
|
+
.description('download one release from books releases')
|
|
425
|
+
.argument('<chaptarrId>', 'book_id from books releases', parsePositiveInt)
|
|
426
|
+
.argument('<guid>', 'guid of the release')
|
|
427
|
+
.argument('<indexerId>', 'indexer_id of the release', parsePositiveInt)
|
|
428
|
+
.option('--json', 'output JSON')
|
|
429
|
+
.addHelpText('after', `
|
|
430
|
+
Behavior: monitors the book and sends that release to the download client.
|
|
431
|
+
|
|
432
|
+
Output (--json): { book_id, title, status, detail, sabnzbd_paused }
|
|
433
|
+
status is grabbed or error (an expired search reports Chaptarr's cache message). Exit code 1 on error.
|
|
434
|
+
|
|
435
|
+
Examples:
|
|
436
|
+
trendarr books grab 450 "https://example.org/details/abc" 1`)
|
|
437
|
+
.action(async (chaptarrId, guid, indexerId, opts) => {
|
|
438
|
+
try {
|
|
439
|
+
const { output, warnings } = await grabRelease(loadConfig(), chaptarrId, guid, indexerId);
|
|
440
|
+
printWarnings(warnings);
|
|
441
|
+
process.stdout.write(`${opts.json === true ? toJson(output) : renderBookGrab(output)}\n`);
|
|
442
|
+
if (output.status === 'error')
|
|
443
|
+
process.exitCode = 1;
|
|
444
|
+
}
|
|
445
|
+
catch (err) {
|
|
446
|
+
fail(err);
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
books
|
|
450
|
+
.command('send')
|
|
451
|
+
.description('email an ebook to a Kindle through Audiobookshelf')
|
|
452
|
+
.argument('<chaptarrId>', "the ebook's book_id from books search", parsePositiveInt)
|
|
453
|
+
.argument('<device>', 'Audiobookshelf e-reader device name, from books search "kindles"')
|
|
454
|
+
.option('--json', 'output JSON')
|
|
455
|
+
.addHelpText('after', `
|
|
456
|
+
Behavior: finds the ebook's folder from Chaptarr's file, the Audiobookshelf item in that folder,
|
|
457
|
+
and asks Audiobookshelf to email it to the device. The sender must be approved on the Amazon account.
|
|
458
|
+
|
|
459
|
+
Output (--json): { book_id, title, device, status, detail }
|
|
460
|
+
status is one of: sent | no_file | not_in_audiobookshelf | unknown_device | error.
|
|
461
|
+
Exit code 1 unless sent.
|
|
462
|
+
|
|
463
|
+
Examples:
|
|
464
|
+
trendarr books send 3274 "Kindle Astro"`)
|
|
465
|
+
.action(async (chaptarrId, device, opts) => {
|
|
466
|
+
try {
|
|
467
|
+
const { output, warnings } = await sendBook(loadConfig(), chaptarrId, device);
|
|
468
|
+
printWarnings(warnings);
|
|
469
|
+
process.stdout.write(`${opts.json === true ? toJson(output) : renderBookSend(output)}\n`);
|
|
470
|
+
if (output.status !== 'sent')
|
|
471
|
+
process.exitCode = 1;
|
|
472
|
+
}
|
|
473
|
+
catch (err) {
|
|
474
|
+
fail(err);
|
|
475
|
+
}
|
|
476
|
+
});
|
|
307
477
|
if (process.argv.slice(2).length === 0) {
|
|
308
478
|
program.outputHelp();
|
|
309
479
|
process.exit(0);
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { fetchJson } from '../http.js';
|
|
3
|
+
const authorizeSchema = z.object({ ereaderDevices: z.array(z.object({ name: z.string() })).nullish() });
|
|
4
|
+
const librariesSchema = z.object({
|
|
5
|
+
libraries: z.array(z.object({ id: z.string(), name: z.string(), mediaType: z.string() })),
|
|
6
|
+
});
|
|
7
|
+
const itemsSchema = z.object({
|
|
8
|
+
results: z.array(z.object({
|
|
9
|
+
id: z.string(),
|
|
10
|
+
path: z.string(),
|
|
11
|
+
media: z.object({ ebookFormat: z.string().nullish() }).nullish(),
|
|
12
|
+
})),
|
|
13
|
+
});
|
|
14
|
+
export class AudiobookshelfClient {
|
|
15
|
+
baseUrl;
|
|
16
|
+
apiKey;
|
|
17
|
+
constructor(baseUrl, apiKey) {
|
|
18
|
+
this.baseUrl = baseUrl;
|
|
19
|
+
this.apiKey = apiKey;
|
|
20
|
+
}
|
|
21
|
+
request(path, opts = {}) {
|
|
22
|
+
return fetchJson(`${this.baseUrl}${path}`, { ...opts, headers: { Authorization: `Bearer ${this.apiKey}` } });
|
|
23
|
+
}
|
|
24
|
+
async ereaderDevices() {
|
|
25
|
+
const parsed = authorizeSchema.parse(await this.request('/api/authorize', { method: 'POST' }));
|
|
26
|
+
return (parsed.ereaderDevices ?? []).map((d) => d.name);
|
|
27
|
+
}
|
|
28
|
+
async libraries() {
|
|
29
|
+
return librariesSchema.parse(await this.request('/api/libraries')).libraries;
|
|
30
|
+
}
|
|
31
|
+
async items(libraryId) {
|
|
32
|
+
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 }));
|
|
34
|
+
}
|
|
35
|
+
async sendEbook(libraryItemId, deviceName) {
|
|
36
|
+
await this.request('/api/emails/send-ebook-to-device', {
|
|
37
|
+
method: 'POST',
|
|
38
|
+
body: { libraryItemId, deviceName },
|
|
39
|
+
timeoutMs: 60_000,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { fetchJson } from '../http.js';
|
|
3
|
+
const LOOKUP_TIMEOUT_MS = 60_000;
|
|
4
|
+
const RELEASE_TIMEOUT_MS = 180_000;
|
|
5
|
+
const statusSchema = z.object({ appName: z.string(), version: z.string() });
|
|
6
|
+
const localBookSchema = z.object({
|
|
7
|
+
id: z.number(),
|
|
8
|
+
monitored: z.boolean(),
|
|
9
|
+
hasFiles: z.boolean(),
|
|
10
|
+
narrator: z.string().nullish(),
|
|
11
|
+
});
|
|
12
|
+
const lookupSchema = z.object({
|
|
13
|
+
title: z.string(),
|
|
14
|
+
mediaType: z.string(),
|
|
15
|
+
foreignBookId: z.string(),
|
|
16
|
+
author: z.object({ authorName: z.string() }).nullish(),
|
|
17
|
+
ratings: z.object({ votes: z.number() }).nullish(),
|
|
18
|
+
localAudiobookBooks: z.array(localBookSchema).nullish(),
|
|
19
|
+
localEbookBooks: z.array(localBookSchema).nullish(),
|
|
20
|
+
});
|
|
21
|
+
const rootFolderSchema = z.object({
|
|
22
|
+
path: z.string(),
|
|
23
|
+
isEffectiveDefaultAudiobook: z.boolean().nullish(),
|
|
24
|
+
isEffectiveDefaultEbook: z.boolean().nullish(),
|
|
25
|
+
audiobookQualityProfileId: z.number().nullish(),
|
|
26
|
+
audiobookMetadataProfileId: z.number().nullish(),
|
|
27
|
+
ebookQualityProfileId: z.number().nullish(),
|
|
28
|
+
ebookMetadataProfileId: z.number().nullish(),
|
|
29
|
+
});
|
|
30
|
+
const rejectionSchema = z.union([z.string(), z.object({ reason: z.string() }).transform((r) => r.reason)]);
|
|
31
|
+
const releaseSchema = z.object({
|
|
32
|
+
guid: z.string(),
|
|
33
|
+
indexerId: z.number(),
|
|
34
|
+
title: z.string(),
|
|
35
|
+
size: z.number().nullish(),
|
|
36
|
+
indexer: z.string().nullish(),
|
|
37
|
+
protocol: z.string().nullish(),
|
|
38
|
+
seeders: z.number().nullish(),
|
|
39
|
+
publishDate: z.string().nullish(),
|
|
40
|
+
quality: z.object({ quality: z.object({ name: z.string() }) }).nullish(),
|
|
41
|
+
rejections: z.array(rejectionSchema).nullish(),
|
|
42
|
+
});
|
|
43
|
+
const releasePageSchema = z.object({ releases: z.array(z.unknown()), hiddenReleases: z.array(z.unknown()).nullish() });
|
|
44
|
+
const bookSchema = z.object({ id: z.number(), title: z.string(), mediaType: z.string(), monitored: z.boolean() });
|
|
45
|
+
const bookFilesSchema = z.array(z.object({ path: z.string() }));
|
|
46
|
+
const addedSchema = z.object({ id: z.number() });
|
|
47
|
+
function localBooks(value) {
|
|
48
|
+
return (value ?? []).map((b) => ({ id: b.id, monitored: b.monitored, hasFiles: b.hasFiles, narrator: b.narrator ?? null }));
|
|
49
|
+
}
|
|
50
|
+
function toRelease(value) {
|
|
51
|
+
const parsed = releaseSchema.safeParse(value);
|
|
52
|
+
if (!parsed.success)
|
|
53
|
+
return null;
|
|
54
|
+
const r = parsed.data;
|
|
55
|
+
return {
|
|
56
|
+
guid: r.guid,
|
|
57
|
+
indexerId: r.indexerId,
|
|
58
|
+
title: r.title,
|
|
59
|
+
size: r.size ?? null,
|
|
60
|
+
indexer: r.indexer ?? null,
|
|
61
|
+
protocol: r.protocol ?? null,
|
|
62
|
+
seeders: r.seeders ?? null,
|
|
63
|
+
publishDate: r.publishDate ?? null,
|
|
64
|
+
quality: r.quality?.quality.name ?? null,
|
|
65
|
+
rejections: r.rejections ?? [],
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
export class ChaptarrClient {
|
|
69
|
+
baseUrl;
|
|
70
|
+
apiKey;
|
|
71
|
+
constructor(baseUrl, apiKey) {
|
|
72
|
+
this.baseUrl = baseUrl;
|
|
73
|
+
this.apiKey = apiKey;
|
|
74
|
+
}
|
|
75
|
+
request(path, opts = {}) {
|
|
76
|
+
return fetchJson(`${this.baseUrl}${path}`, { ...opts, headers: { 'X-Api-Key': this.apiKey } });
|
|
77
|
+
}
|
|
78
|
+
async systemStatus() {
|
|
79
|
+
return statusSchema.parse(await this.request('/api/v1/system/status'));
|
|
80
|
+
}
|
|
81
|
+
async lookup(term) {
|
|
82
|
+
const value = await this.request(`/api/v1/book/lookup?term=${encodeURIComponent(term)}`, {
|
|
83
|
+
timeoutMs: LOOKUP_TIMEOUT_MS,
|
|
84
|
+
});
|
|
85
|
+
if (!Array.isArray(value))
|
|
86
|
+
return [];
|
|
87
|
+
return value.flatMap((item) => {
|
|
88
|
+
const parsed = lookupSchema.safeParse(item);
|
|
89
|
+
if (!parsed.success)
|
|
90
|
+
return [];
|
|
91
|
+
const b = parsed.data;
|
|
92
|
+
return [
|
|
93
|
+
{
|
|
94
|
+
title: b.title,
|
|
95
|
+
mediaType: b.mediaType,
|
|
96
|
+
foreignBookId: b.foreignBookId,
|
|
97
|
+
author: b.author?.authorName ?? null,
|
|
98
|
+
votes: b.ratings?.votes ?? null,
|
|
99
|
+
audiobook: localBooks(b.localAudiobookBooks),
|
|
100
|
+
ebook: localBooks(b.localEbookBooks),
|
|
101
|
+
raw: item,
|
|
102
|
+
},
|
|
103
|
+
];
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
async formatDefaults() {
|
|
107
|
+
const folders = z.array(rootFolderSchema).parse(await this.request('/api/v1/rootfolder'));
|
|
108
|
+
const audiobook = folders.find((f) => f.isEffectiveDefaultAudiobook === true);
|
|
109
|
+
const ebook = folders.find((f) => f.isEffectiveDefaultEbook === true);
|
|
110
|
+
if (audiobook?.audiobookQualityProfileId == null ||
|
|
111
|
+
audiobook.audiobookMetadataProfileId == null ||
|
|
112
|
+
ebook?.ebookQualityProfileId == null ||
|
|
113
|
+
ebook.ebookMetadataProfileId == null) {
|
|
114
|
+
throw new Error('Chaptarr has no default audiobook and ebook root folders with profiles');
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
audiobook: {
|
|
118
|
+
rootFolderPath: audiobook.path,
|
|
119
|
+
qualityProfileId: audiobook.audiobookQualityProfileId,
|
|
120
|
+
metadataProfileId: audiobook.audiobookMetadataProfileId,
|
|
121
|
+
},
|
|
122
|
+
ebook: {
|
|
123
|
+
rootFolderPath: ebook.path,
|
|
124
|
+
qualityProfileId: ebook.ebookQualityProfileId,
|
|
125
|
+
metadataProfileId: ebook.ebookMetadataProfileId,
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
async addBook(lookup, format, defaults, opts) {
|
|
130
|
+
const rawAuthor = lookup.raw.author;
|
|
131
|
+
const author = typeof rawAuthor === 'object' && rawAuthor !== null ? rawAuthor : {};
|
|
132
|
+
const body = {
|
|
133
|
+
...lookup.raw,
|
|
134
|
+
mediaType: format,
|
|
135
|
+
monitored: opts.monitored,
|
|
136
|
+
addOptions: { searchForNewBook: opts.search },
|
|
137
|
+
author: {
|
|
138
|
+
...author,
|
|
139
|
+
audiobookQualityProfileId: defaults.audiobook.qualityProfileId,
|
|
140
|
+
audiobookMetadataProfileId: defaults.audiobook.metadataProfileId,
|
|
141
|
+
ebookQualityProfileId: defaults.ebook.qualityProfileId,
|
|
142
|
+
ebookMetadataProfileId: defaults.ebook.metadataProfileId,
|
|
143
|
+
qualityProfileId: defaults[format].qualityProfileId,
|
|
144
|
+
metadataProfileId: defaults[format].metadataProfileId,
|
|
145
|
+
audiobookRootFolderPath: defaults.audiobook.rootFolderPath,
|
|
146
|
+
ebookRootFolderPath: defaults.ebook.rootFolderPath,
|
|
147
|
+
rootFolderPath: defaults[format].rootFolderPath,
|
|
148
|
+
monitored: true,
|
|
149
|
+
audiobookMonitored: true,
|
|
150
|
+
ebookMonitored: true,
|
|
151
|
+
audiobookMonitorNewItems: 'none',
|
|
152
|
+
ebookMonitorNewItems: 'none',
|
|
153
|
+
addOptions: { monitor: 'none', searchForMissingBooks: false },
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
return addedSchema.parse(await this.request('/api/v1/book', { method: 'POST', body })).id;
|
|
157
|
+
}
|
|
158
|
+
async setMonitored(bookIds, monitored) {
|
|
159
|
+
await this.request('/api/v1/book/monitor', { method: 'PUT', body: { bookIds, monitored } });
|
|
160
|
+
}
|
|
161
|
+
async searchBook(bookId) {
|
|
162
|
+
await this.request('/api/v1/command', { method: 'POST', body: { name: 'BookSearch', bookIds: [bookId] } });
|
|
163
|
+
}
|
|
164
|
+
async releases(bookId) {
|
|
165
|
+
const value = await this.request(`/api/v1/release?bookId=${bookId}`, { timeoutMs: RELEASE_TIMEOUT_MS });
|
|
166
|
+
const page = releasePageSchema.safeParse(value);
|
|
167
|
+
const items = Array.isArray(value)
|
|
168
|
+
? value
|
|
169
|
+
: page.success
|
|
170
|
+
? [...page.data.releases, ...(page.data.hiddenReleases ?? [])]
|
|
171
|
+
: [];
|
|
172
|
+
return items.flatMap((item) => toRelease(item) ?? []);
|
|
173
|
+
}
|
|
174
|
+
async grab(guid, indexerId) {
|
|
175
|
+
await this.request('/api/v1/release', { method: 'POST', body: { guid, indexerId }, timeoutMs: LOOKUP_TIMEOUT_MS });
|
|
176
|
+
}
|
|
177
|
+
async book(bookId) {
|
|
178
|
+
return bookSchema.parse(await this.request(`/api/v1/book/${bookId}`));
|
|
179
|
+
}
|
|
180
|
+
async bookFilePaths(bookId) {
|
|
181
|
+
return bookFilesSchema.parse(await this.request(`/api/v1/bookfile?bookId=${bookId}`)).map((f) => f.path);
|
|
182
|
+
}
|
|
183
|
+
}
|
package/dist/commands/add.js
CHANGED
|
@@ -20,7 +20,7 @@ export function parseImdbIds(raw) {
|
|
|
20
20
|
export function hasFailure(output) {
|
|
21
21
|
return output.results.some((r) => r.status === 'not_found' || r.status === 'error');
|
|
22
22
|
}
|
|
23
|
-
async function sabPaused(config, warnings) {
|
|
23
|
+
export async function sabPaused(config, warnings) {
|
|
24
24
|
try {
|
|
25
25
|
return await new SabnzbdClient(config.sabnzbd.url, config.sabnzbd.apiKey).isPaused();
|
|
26
26
|
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { dirname } from 'node:path';
|
|
2
|
+
import { AudiobookshelfClient } from '../clients/audiobookshelf.js';
|
|
3
|
+
import { ChaptarrClient } from '../clients/chaptarr.js';
|
|
4
|
+
import { ConfigError } from '../config.js';
|
|
5
|
+
import { HttpError, describeError } from '../http.js';
|
|
6
|
+
import { sabPaused } from './add.js';
|
|
7
|
+
const DAY_MS = 86_400_000;
|
|
8
|
+
const UNWANTED_REJECTION = /not wanted in profile|not enough seeders/i;
|
|
9
|
+
function chaptarr(config) {
|
|
10
|
+
if (config.chaptarr === undefined)
|
|
11
|
+
throw new ConfigError("Chaptarr is not configured — run 'trendarr init'.");
|
|
12
|
+
return new ChaptarrClient(config.chaptarr.url, config.chaptarr.apiKey);
|
|
13
|
+
}
|
|
14
|
+
function audiobookshelf(config) {
|
|
15
|
+
if (config.audiobookshelf === undefined) {
|
|
16
|
+
throw new ConfigError("Audiobookshelf is not configured — run 'trendarr init'.");
|
|
17
|
+
}
|
|
18
|
+
return new AudiobookshelfClient(config.audiobookshelf.url, config.audiobookshelf.apiKey);
|
|
19
|
+
}
|
|
20
|
+
function preferred(books) {
|
|
21
|
+
return books.find((b) => b.hasFiles) ?? books.find((b) => b.monitored) ?? books[0] ?? null;
|
|
22
|
+
}
|
|
23
|
+
function formatState(books) {
|
|
24
|
+
const book = preferred(books);
|
|
25
|
+
return {
|
|
26
|
+
book_id: book?.id ?? null,
|
|
27
|
+
monitored: book?.monitored ?? false,
|
|
28
|
+
has_file: book?.hasFiles ?? false,
|
|
29
|
+
narrator: book?.narrator && book.narrator.length > 0 ? book.narrator : null,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function httpDetail(err) {
|
|
33
|
+
if (err instanceof HttpError) {
|
|
34
|
+
const body = err.body;
|
|
35
|
+
if (typeof body === 'string' && body.length > 0)
|
|
36
|
+
return body.slice(0, 300);
|
|
37
|
+
if (typeof body === 'object' && body !== null && 'message' in body && typeof body.message === 'string') {
|
|
38
|
+
return body.message.slice(0, 300);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return describeError(err);
|
|
42
|
+
}
|
|
43
|
+
export async function searchBooks(config, query, opts) {
|
|
44
|
+
const client = chaptarr(config);
|
|
45
|
+
const warnings = [];
|
|
46
|
+
const limit = opts.maxEntries ?? config.defaults.maxEntries;
|
|
47
|
+
const seen = new Set();
|
|
48
|
+
const books = [];
|
|
49
|
+
for (const found of await client.lookup(query)) {
|
|
50
|
+
if (seen.has(found.foreignBookId) || books.length >= limit)
|
|
51
|
+
continue;
|
|
52
|
+
seen.add(found.foreignBookId);
|
|
53
|
+
books.push({
|
|
54
|
+
rank: books.length + 1,
|
|
55
|
+
title: found.title,
|
|
56
|
+
author: found.author,
|
|
57
|
+
foreign_book_id: found.foreignBookId,
|
|
58
|
+
votes: found.votes,
|
|
59
|
+
audiobook: formatState(found.audiobook),
|
|
60
|
+
ebook: formatState(found.ebook),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
let kindles = [];
|
|
64
|
+
if (config.audiobookshelf !== undefined) {
|
|
65
|
+
try {
|
|
66
|
+
kindles = await audiobookshelf(config).ereaderDevices();
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
warnings.push(`Audiobookshelf unreachable — no Kindle list (${describeError(err)})`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return { output: { books, kindles }, warnings };
|
|
73
|
+
}
|
|
74
|
+
async function findFormat(client, foreignBookId, format) {
|
|
75
|
+
const found = await client.lookup(foreignBookId);
|
|
76
|
+
const exact = found.filter((b) => b.foreignBookId === foreignBookId);
|
|
77
|
+
const pool = exact.length > 0 ? exact : found;
|
|
78
|
+
return pool.find((b) => b.mediaType === format) ?? pool[0] ?? null;
|
|
79
|
+
}
|
|
80
|
+
async function ensureBook(client, lookup, format, opts) {
|
|
81
|
+
const local = preferred(lookup[format]);
|
|
82
|
+
if (local !== null)
|
|
83
|
+
return { id: local.id, added: false };
|
|
84
|
+
const id = await client.addBook(lookup, format, await client.formatDefaults(), opts);
|
|
85
|
+
return { id, added: true };
|
|
86
|
+
}
|
|
87
|
+
export async function addBook(config, foreignBookId, format) {
|
|
88
|
+
const client = chaptarr(config);
|
|
89
|
+
const warnings = [];
|
|
90
|
+
const base = { foreign_book_id: foreignBookId, format };
|
|
91
|
+
let result;
|
|
92
|
+
try {
|
|
93
|
+
const lookup = await findFormat(client, foreignBookId, format);
|
|
94
|
+
if (lookup === null) {
|
|
95
|
+
result = { book_id: null, title: null, status: 'not_found', detail: null };
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
const local = preferred(lookup[format]);
|
|
99
|
+
if (local?.hasFiles === true) {
|
|
100
|
+
result = { book_id: local.id, title: lookup.title, status: 'already_exists', detail: null };
|
|
101
|
+
}
|
|
102
|
+
else if (local !== null) {
|
|
103
|
+
await client.setMonitored([local.id], true);
|
|
104
|
+
await client.searchBook(local.id);
|
|
105
|
+
result = { book_id: local.id, title: lookup.title, status: 'searching', detail: null };
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
const { id } = await ensureBook(client, lookup, format, { monitored: true, search: true });
|
|
109
|
+
result = { book_id: id, title: lookup.title, status: 'added', detail: null };
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
catch (err) {
|
|
114
|
+
result = { book_id: null, title: null, status: 'error', detail: httpDetail(err) };
|
|
115
|
+
}
|
|
116
|
+
const sabnzbd_paused = await sabPaused(config, warnings);
|
|
117
|
+
return { output: { ...base, ...result, sabnzbd_paused }, warnings };
|
|
118
|
+
}
|
|
119
|
+
function ageDays(publishDate, now) {
|
|
120
|
+
if (publishDate === null)
|
|
121
|
+
return null;
|
|
122
|
+
const time = Date.parse(publishDate);
|
|
123
|
+
return Number.isNaN(time) ? null : Math.max(0, Math.floor((now - time) / DAY_MS));
|
|
124
|
+
}
|
|
125
|
+
export function selectReleases(releases, limit, now = Date.now()) {
|
|
126
|
+
return releases
|
|
127
|
+
.filter((r) => !r.rejections.some((reason) => UNWANTED_REJECTION.test(reason)))
|
|
128
|
+
.slice(0, limit)
|
|
129
|
+
.map((r, i) => ({
|
|
130
|
+
rank: i + 1,
|
|
131
|
+
title: r.title,
|
|
132
|
+
size: r.size,
|
|
133
|
+
quality: r.quality,
|
|
134
|
+
protocol: r.protocol,
|
|
135
|
+
indexer: r.indexer,
|
|
136
|
+
seeders: r.seeders,
|
|
137
|
+
age_days: ageDays(r.publishDate, now),
|
|
138
|
+
guid: r.guid,
|
|
139
|
+
indexer_id: r.indexerId,
|
|
140
|
+
rejections: r.rejections,
|
|
141
|
+
}));
|
|
142
|
+
}
|
|
143
|
+
export async function bookReleases(config, foreignBookId, format, opts) {
|
|
144
|
+
const client = chaptarr(config);
|
|
145
|
+
const lookup = await findFormat(client, foreignBookId, format);
|
|
146
|
+
if (lookup === null)
|
|
147
|
+
throw new Error(`no book ${foreignBookId} in Chaptarr's lookup`);
|
|
148
|
+
const { id } = await ensureBook(client, lookup, format, { monitored: false, search: false });
|
|
149
|
+
const releases = selectReleases(await client.releases(id), opts.maxEntries ?? config.defaults.maxEntries);
|
|
150
|
+
return {
|
|
151
|
+
output: { foreign_book_id: foreignBookId, format, book_id: id, title: lookup.title, releases },
|
|
152
|
+
warnings: [],
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
export async function grabRelease(config, bookId, guid, indexerId) {
|
|
156
|
+
const client = chaptarr(config);
|
|
157
|
+
const warnings = [];
|
|
158
|
+
let title = null;
|
|
159
|
+
let result;
|
|
160
|
+
try {
|
|
161
|
+
title = (await client.book(bookId)).title;
|
|
162
|
+
await client.setMonitored([bookId], true);
|
|
163
|
+
await client.grab(guid, indexerId);
|
|
164
|
+
result = { status: 'grabbed', detail: null };
|
|
165
|
+
}
|
|
166
|
+
catch (err) {
|
|
167
|
+
result = { status: 'error', detail: httpDetail(err) };
|
|
168
|
+
}
|
|
169
|
+
const sabnzbd_paused = await sabPaused(config, warnings);
|
|
170
|
+
return { output: { book_id: bookId, title, ...result, sabnzbd_paused }, warnings };
|
|
171
|
+
}
|
|
172
|
+
export async function sendBook(config, bookId, device) {
|
|
173
|
+
const client = chaptarr(config);
|
|
174
|
+
const abs = audiobookshelf(config);
|
|
175
|
+
const base = { book_id: bookId, device };
|
|
176
|
+
let title = null;
|
|
177
|
+
try {
|
|
178
|
+
title = (await client.book(bookId)).title;
|
|
179
|
+
if (!(await abs.ereaderDevices()).includes(device)) {
|
|
180
|
+
return { output: { ...base, title, status: 'unknown_device', detail: null }, warnings: [] };
|
|
181
|
+
}
|
|
182
|
+
const folders = new Set((await client.bookFilePaths(bookId)).map((p) => dirname(p)));
|
|
183
|
+
if (folders.size === 0)
|
|
184
|
+
return { output: { ...base, title, status: 'no_file', detail: null }, warnings: [] };
|
|
185
|
+
for (const library of (await abs.libraries()).filter((l) => l.mediaType === 'book')) {
|
|
186
|
+
const item = (await abs.items(library.id)).find((i) => folders.has(i.path) && i.ebookFormat !== null);
|
|
187
|
+
if (item === undefined)
|
|
188
|
+
continue;
|
|
189
|
+
await abs.sendEbook(item.id, device);
|
|
190
|
+
return { output: { ...base, title, status: 'sent', detail: null }, warnings: [] };
|
|
191
|
+
}
|
|
192
|
+
return { output: { ...base, title, status: 'not_in_audiobookshelf', detail: null }, warnings: [] };
|
|
193
|
+
}
|
|
194
|
+
catch (err) {
|
|
195
|
+
return { output: { ...base, title, status: 'error', detail: httpDetail(err) }, warnings: [] };
|
|
196
|
+
}
|
|
197
|
+
}
|
package/dist/commands/init.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { cancel, intro, isCancel, log, outro, select, spinner, text } from '@clack/prompts';
|
|
1
|
+
import { cancel, confirm, intro, isCancel, log, outro, select, spinner, text } from '@clack/prompts';
|
|
2
|
+
import { AudiobookshelfClient } from '../clients/audiobookshelf.js';
|
|
3
|
+
import { ChaptarrClient } from '../clients/chaptarr.js';
|
|
2
4
|
import { OmdbClient } from '../clients/omdb.js';
|
|
3
5
|
import { RadarrClient } from '../clients/radarr.js';
|
|
4
6
|
import { SonarrClient } from '../clients/sonarr.js';
|
|
@@ -141,6 +143,32 @@ export async function runInit() {
|
|
|
141
143
|
const ok = await validated('SABnzbd', () => new SabnzbdClient(url, apiKey).isPaused());
|
|
142
144
|
return ok === null ? null : { url, apiKey };
|
|
143
145
|
});
|
|
146
|
+
const wantBooks = await confirm({
|
|
147
|
+
message: 'Set up books (Chaptarr and Audiobookshelf)?',
|
|
148
|
+
initialValue: prior.chaptarr !== undefined,
|
|
149
|
+
});
|
|
150
|
+
if (isCancel(wantBooks))
|
|
151
|
+
bail();
|
|
152
|
+
let books = {};
|
|
153
|
+
if (wantBooks) {
|
|
154
|
+
const chaptarr = await askUntilValid(async () => {
|
|
155
|
+
const url = (await ask('Chaptarr URL (e.g. http://localhost:8789)', prior.chaptarr?.url)).replace(/\/+$/, '');
|
|
156
|
+
const apiKey = await ask('Chaptarr API key (Settings > General > Security)', prior.chaptarr?.apiKey);
|
|
157
|
+
const client = new ChaptarrClient(url, apiKey);
|
|
158
|
+
const ok = await validated('Chaptarr', async () => {
|
|
159
|
+
await client.systemStatus();
|
|
160
|
+
return client.formatDefaults();
|
|
161
|
+
});
|
|
162
|
+
return ok === null ? null : { url, apiKey };
|
|
163
|
+
});
|
|
164
|
+
const audiobookshelf = await askUntilValid(async () => {
|
|
165
|
+
const url = (await ask('Audiobookshelf URL (e.g. http://localhost:13378)', prior.audiobookshelf?.url)).replace(/\/+$/, '');
|
|
166
|
+
const apiKey = await ask('Audiobookshelf API key (Settings > API Keys)', prior.audiobookshelf?.apiKey);
|
|
167
|
+
const ok = await validated('Audiobookshelf', () => new AudiobookshelfClient(url, apiKey).ereaderDevices());
|
|
168
|
+
return ok === null ? null : { url, apiKey };
|
|
169
|
+
});
|
|
170
|
+
books = { chaptarr, audiobookshelf };
|
|
171
|
+
}
|
|
144
172
|
const config = {
|
|
145
173
|
tmdb,
|
|
146
174
|
omdb,
|
|
@@ -148,6 +176,7 @@ export async function runInit() {
|
|
|
148
176
|
sonarr,
|
|
149
177
|
sabnzbd,
|
|
150
178
|
defaults: { maxEntries: prior.defaults?.maxEntries ?? 10 },
|
|
179
|
+
...books,
|
|
151
180
|
};
|
|
152
181
|
saveConfig(config);
|
|
153
182
|
outro(`Saved ${configPath()} (chmod 600). Try: trendarr trending`);
|
package/dist/config.js
CHANGED
|
@@ -8,6 +8,7 @@ const arrSchema = z.object({
|
|
|
8
8
|
qualityProfileId: z.number().int().positive(),
|
|
9
9
|
rootFolderPath: z.string().min(1),
|
|
10
10
|
});
|
|
11
|
+
const serviceSchema = z.object({ url: z.string().min(1), apiKey: z.string().min(1) });
|
|
11
12
|
export const configSchema = z.object({
|
|
12
13
|
tmdb: z.object({ accessToken: z.string().min(1) }),
|
|
13
14
|
omdb: z.object({ apiKey: z.string().min(1) }),
|
|
@@ -15,6 +16,8 @@ export const configSchema = z.object({
|
|
|
15
16
|
sonarr: arrSchema.extend({ monitor: z.string().min(1) }),
|
|
16
17
|
sabnzbd: z.object({ url: z.string().min(1), apiKey: z.string().min(1) }),
|
|
17
18
|
defaults: z.object({ maxEntries: z.number().int().positive() }),
|
|
19
|
+
chaptarr: serviceSchema.optional(),
|
|
20
|
+
audiobookshelf: serviceSchema.optional(),
|
|
18
21
|
});
|
|
19
22
|
export class ConfigError extends Error {
|
|
20
23
|
constructor(message) {
|
package/dist/format.js
CHANGED
|
@@ -97,3 +97,48 @@ export function renderAddTable(output) {
|
|
|
97
97
|
const sab = output.sabnzbd_paused === null ? 'SABnzbd status: unknown' : renderSabStatus(output.sabnzbd_paused);
|
|
98
98
|
return [...lines, '', sab].join('\n');
|
|
99
99
|
}
|
|
100
|
+
function formatCell(state) {
|
|
101
|
+
if (state.has_file)
|
|
102
|
+
return '✓';
|
|
103
|
+
if (state.monitored)
|
|
104
|
+
return 'wanted';
|
|
105
|
+
return state.book_id === null ? '' : 'known';
|
|
106
|
+
}
|
|
107
|
+
export function renderBookSearchTable(output) {
|
|
108
|
+
if (output.books.length === 0)
|
|
109
|
+
return 'No results.';
|
|
110
|
+
const header = `${cell('#', 3)} ${cell('TITLE', 40)} ${cell('AUTHOR', 24)} ${cell('AUDIO', 7)} ${cell('EBOOK', 7)} ID`;
|
|
111
|
+
const rows = output.books.map((b) => `${cell(String(b.rank), 3)} ${cell(b.title, 40)} ${cell(dash(b.author), 24)} ${cell(formatCell(b.audiobook), 7)} ${cell(formatCell(b.ebook), 7)} ${b.foreign_book_id}`);
|
|
112
|
+
const kindles = output.kindles.length === 0 ? 'Kindles: none' : `Kindles: ${output.kindles.join(', ')}`;
|
|
113
|
+
return [pc.bold(header), ...rows, '', kindles].join('\n');
|
|
114
|
+
}
|
|
115
|
+
function statusColor(status, good) {
|
|
116
|
+
return good.includes(status) ? pc.green(status) : status === 'already_exists' ? pc.yellow(status) : pc.red(status);
|
|
117
|
+
}
|
|
118
|
+
function sabLine(paused) {
|
|
119
|
+
return paused === null ? 'SABnzbd status: unknown' : renderSabStatus(paused);
|
|
120
|
+
}
|
|
121
|
+
export function renderBookAdd(output) {
|
|
122
|
+
const detail = output.detail === null ? '' : ` (${output.detail})`;
|
|
123
|
+
const line = `${output.title ?? output.foreign_book_id} [${output.format}] ${statusColor(output.status, ['added', 'searching'])}${detail}`;
|
|
124
|
+
return [line, '', sabLine(output.sabnzbd_paused)].join('\n');
|
|
125
|
+
}
|
|
126
|
+
function megabytes(size) {
|
|
127
|
+
return size === null ? '-' : `${Math.round(size / 1_048_576)} MB`;
|
|
128
|
+
}
|
|
129
|
+
export function renderReleasesTable(output) {
|
|
130
|
+
const title = `${output.title} [${output.format}] book ${output.book_id}`;
|
|
131
|
+
if (output.releases.length === 0)
|
|
132
|
+
return `${title}\nNo releases.`;
|
|
133
|
+
const header = `${cell('#', 3)} ${cell('RELEASE', 60)} ${cell('SIZE', 9)} ${cell('QUALITY', 13)} ${cell('SEEDS', 5)} AGE`;
|
|
134
|
+
const rows = output.releases.map((r) => `${cell(String(r.rank), 3)} ${cell(r.title, 60)} ${cell(megabytes(r.size), 9)} ${cell(dash(r.quality), 13)} ${cell(r.protocol === 'usenet' ? 'nzb' : dash(r.seeders), 5)} ${r.age_days === null ? '-' : `${r.age_days}d`}`);
|
|
135
|
+
return [title, pc.bold(header), ...rows].join('\n');
|
|
136
|
+
}
|
|
137
|
+
export function renderBookGrab(output) {
|
|
138
|
+
const detail = output.detail === null ? '' : ` (${output.detail})`;
|
|
139
|
+
return [`${output.title ?? `book ${output.book_id}`} ${statusColor(output.status, ['grabbed'])}${detail}`, '', sabLine(output.sabnzbd_paused)].join('\n');
|
|
140
|
+
}
|
|
141
|
+
export function renderBookSend(output) {
|
|
142
|
+
const detail = output.detail === null ? '' : ` (${output.detail})`;
|
|
143
|
+
return `${output.title ?? `book ${output.book_id}`} → ${output.device}: ${statusColor(output.status, ['sent'])}${detail}`;
|
|
144
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrofoundry/trendarr",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.1",
|
|
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",
|