@astrofoundry/trendarr 0.9.0 → 0.10.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 +1 -1
- package/dist/cli.js +10 -5
- package/dist/clients/chaptarr.js +56 -1
- package/dist/clients/sabnzbd.js +7 -0
- package/dist/commands/books.js +79 -26
- package/dist/format.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ trendarr books send <chaptarrId> <device> [--json]
|
|
|
28
28
|
|
|
29
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).
|
|
30
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.
|
|
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.
|
|
32
32
|
|
|
33
33
|
## Development
|
|
34
34
|
|
package/dist/cli.js
CHANGED
|
@@ -363,17 +363,21 @@ Examples:
|
|
|
363
363
|
});
|
|
364
364
|
books
|
|
365
365
|
.command('add')
|
|
366
|
-
.description('monitor one format of a book and
|
|
366
|
+
.description('monitor one format of a book, search, and report whether Chaptarr grabbed a release')
|
|
367
367
|
.argument('<bookId>', 'foreign book id from books search, e.g. gr:3060926', parseBookId)
|
|
368
368
|
.requiredOption('--format <format>', "'audiobook' or 'ebook'", parseFormat)
|
|
369
369
|
.option('--json', 'output JSON')
|
|
370
370
|
.addHelpText('after', `
|
|
371
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
|
|
373
|
-
|
|
372
|
+
or monitors the existing entry. When the monitored edition's title carries a parenthesised suffix,
|
|
373
|
+
such as a series name, it monitors the plain-title edition in the same language and narration, so
|
|
374
|
+
release names match. Then it searches, waits up to 2 minutes, and reports whether Chaptarr grabbed a
|
|
375
|
+
release by the format's quality profile. A format that already has a file reports already_exists.
|
|
376
|
+
sabnzbd_paused is false when the format's SABnzbd category has Force priority.
|
|
374
377
|
|
|
375
378
|
Output (--json): { foreign_book_id, format, book_id, title, status, detail, sabnzbd_paused }
|
|
376
|
-
status is one of:
|
|
379
|
+
status is one of: downloading | nothing_found | searching (still running after 2 minutes) |
|
|
380
|
+
already_exists | not_found | error. Exit code 1 on not_found or error.
|
|
377
381
|
|
|
378
382
|
Examples:
|
|
379
383
|
trendarr books add gr:3060926 --format ebook`)
|
|
@@ -398,7 +402,8 @@ books
|
|
|
398
402
|
.option('--max-entries <n>', 'releases', parsePositiveInt)
|
|
399
403
|
.option('--json', 'output JSON')
|
|
400
404
|
.addHelpText('after', `
|
|
401
|
-
Behavior: adds the book unmonitored when Chaptarr does not have it,
|
|
405
|
+
Behavior: adds the book unmonitored when Chaptarr does not have it, switches a suffixed edition title
|
|
406
|
+
to the plain one as books add does, then runs Chaptarr's interactive
|
|
402
407
|
search (can take a minute). Releases in a format the profile does not want, and torrents without
|
|
403
408
|
enough seeders, are left out. Order is Chaptarr's. Grab one within 30 minutes, while Chaptarr
|
|
404
409
|
still caches the search.
|
package/dist/clients/chaptarr.js
CHANGED
|
@@ -44,6 +44,24 @@ const releasePageSchema = z.object({ releases: z.array(z.unknown()), hiddenRelea
|
|
|
44
44
|
const bookSchema = z.object({ id: z.number(), title: z.string(), mediaType: z.string(), monitored: z.boolean() });
|
|
45
45
|
const bookFilesSchema = z.array(z.object({ path: z.string() }));
|
|
46
46
|
const addedSchema = z.object({ id: z.number() });
|
|
47
|
+
const editionSchema = z.object({
|
|
48
|
+
id: z.number(),
|
|
49
|
+
title: z.string(),
|
|
50
|
+
language: z.string().nullish(),
|
|
51
|
+
monitored: z.boolean(),
|
|
52
|
+
narrators: z.array(z.object({ name: z.string() })).nullish(),
|
|
53
|
+
});
|
|
54
|
+
const rawSchema = z.record(z.string(), z.unknown());
|
|
55
|
+
const commandSchema = z.object({ id: z.number(), status: z.string(), queued: z.string() });
|
|
56
|
+
const historySchema = z.object({
|
|
57
|
+
records: z.array(z.object({ bookId: z.number().nullish(), eventType: z.string(), date: z.string() })),
|
|
58
|
+
});
|
|
59
|
+
const queueSchema = z.object({ records: z.array(z.object({ bookId: z.number().nullish() })) });
|
|
60
|
+
const downloadClientsSchema = z.array(z.object({
|
|
61
|
+
implementation: z.string(),
|
|
62
|
+
enable: z.boolean(),
|
|
63
|
+
fields: z.array(z.object({ name: z.string(), value: z.unknown() })),
|
|
64
|
+
}));
|
|
47
65
|
function localBooks(value) {
|
|
48
66
|
return (value ?? []).map((b) => ({ id: b.id, monitored: b.monitored, hasFiles: b.hasFiles, narrator: b.narrator ?? null }));
|
|
49
67
|
}
|
|
@@ -159,7 +177,44 @@ export class ChaptarrClient {
|
|
|
159
177
|
await this.request('/api/v1/book/monitor', { method: 'PUT', body: { bookIds, monitored } });
|
|
160
178
|
}
|
|
161
179
|
async searchBook(bookId) {
|
|
162
|
-
|
|
180
|
+
const body = { name: 'BookSearch', bookIds: [bookId] };
|
|
181
|
+
return commandSchema.parse(await this.request('/api/v1/command', { method: 'POST', body }));
|
|
182
|
+
}
|
|
183
|
+
async command(id) {
|
|
184
|
+
return commandSchema.parse(await this.request(`/api/v1/command/${id}`));
|
|
185
|
+
}
|
|
186
|
+
async grabbedSince(bookId, since) {
|
|
187
|
+
const page = historySchema.parse(await this.request(`/api/v1/history?bookId=${bookId}&pageSize=20&sortKey=date&sortDirection=descending`));
|
|
188
|
+
const from = Date.parse(since);
|
|
189
|
+
return page.records.some((r) => r.bookId === bookId && r.eventType === 'grabbed' && Date.parse(r.date) >= from);
|
|
190
|
+
}
|
|
191
|
+
async queuedBookIds() {
|
|
192
|
+
const page = queueSchema.parse(await this.request('/api/v1/queue?pageSize=200'));
|
|
193
|
+
return new Set(page.records.flatMap((r) => (r.bookId == null ? [] : [r.bookId])));
|
|
194
|
+
}
|
|
195
|
+
async sabnzbdCategory(format) {
|
|
196
|
+
const clients = downloadClientsSchema.parse(await this.request('/api/v1/downloadclient'));
|
|
197
|
+
const sab = clients.find((c) => c.enable && c.implementation === 'Sabnzbd');
|
|
198
|
+
const value = sab?.fields.find((f) => f.name === `${format}Category`)?.value;
|
|
199
|
+
return typeof value === 'string' && value.length > 0 ? value : null;
|
|
200
|
+
}
|
|
201
|
+
async editions(bookId) {
|
|
202
|
+
return z
|
|
203
|
+
.array(editionSchema)
|
|
204
|
+
.parse(await this.request(`/api/v1/edition?bookId=${bookId}`))
|
|
205
|
+
.map((e) => ({
|
|
206
|
+
id: e.id,
|
|
207
|
+
title: e.title,
|
|
208
|
+
language: e.language ?? null,
|
|
209
|
+
monitored: e.monitored,
|
|
210
|
+
narrators: (e.narrators ?? []).map((n) => n.name),
|
|
211
|
+
}));
|
|
212
|
+
}
|
|
213
|
+
async monitorEdition(bookId, editionId) {
|
|
214
|
+
const book = rawSchema.parse(await this.request(`/api/v1/book/${bookId}`));
|
|
215
|
+
const editions = z.array(rawSchema).parse(await this.request(`/api/v1/edition?bookId=${bookId}`));
|
|
216
|
+
const body = { ...book, editions: editions.map((e) => ({ ...e, monitored: e.id === editionId })) };
|
|
217
|
+
await this.request(`/api/v1/book/${bookId}`, { method: 'PUT', body });
|
|
163
218
|
}
|
|
164
219
|
async releases(bookId) {
|
|
165
220
|
const value = await this.request(`/api/v1/release?bookId=${bookId}`, { timeoutMs: RELEASE_TIMEOUT_MS });
|
package/dist/clients/sabnzbd.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { fetchJson } from '../http.js';
|
|
3
3
|
const queueSchema = z.object({ queue: z.object({ paused: z.boolean() }) });
|
|
4
|
+
const categoriesSchema = z.object({
|
|
5
|
+
config: z.object({ categories: z.array(z.object({ name: z.string(), priority: z.coerce.number() })) }),
|
|
6
|
+
});
|
|
4
7
|
export class SabnzbdClient {
|
|
5
8
|
url;
|
|
6
9
|
apiKey;
|
|
@@ -14,6 +17,10 @@ export class SabnzbdClient {
|
|
|
14
17
|
async isPaused() {
|
|
15
18
|
return queueSchema.parse(await this.call('queue')).queue.paused;
|
|
16
19
|
}
|
|
20
|
+
async categoryPriority(name) {
|
|
21
|
+
const parsed = categoriesSchema.parse(await this.call('get_config§ion=categories'));
|
|
22
|
+
return parsed.config.categories.find((c) => c.name === name)?.priority ?? null;
|
|
23
|
+
}
|
|
17
24
|
async pause() {
|
|
18
25
|
await this.call('pause');
|
|
19
26
|
}
|
package/dist/commands/books.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { dirname } from 'node:path';
|
|
2
2
|
import { AudiobookshelfClient } from '../clients/audiobookshelf.js';
|
|
3
|
-
import { ChaptarrClient } from '../clients/chaptarr.js';
|
|
3
|
+
import { ChaptarrClient, } from '../clients/chaptarr.js';
|
|
4
|
+
import { SabnzbdClient } from '../clients/sabnzbd.js';
|
|
4
5
|
import { ConfigError } from '../config.js';
|
|
5
6
|
import { HttpError, describeError } from '../http.js';
|
|
6
7
|
import { sabPaused } from './add.js';
|
|
7
8
|
const DAY_MS = 86_400_000;
|
|
8
9
|
const UNWANTED_REJECTION = /not wanted in profile|not enough seeders/i;
|
|
10
|
+
const SAB_FORCE_PRIORITY = 2;
|
|
11
|
+
const RUNNING_COMMAND = new Set(['queued', 'started']);
|
|
12
|
+
const SEARCH_TIMING = { waitMs: 120_000, pollMs: 3_000 };
|
|
9
13
|
function chaptarr(config) {
|
|
10
14
|
if (config.chaptarr === undefined)
|
|
11
15
|
throw new ConfigError("Chaptarr is not configured — run 'trendarr init'.");
|
|
@@ -72,46 +76,91 @@ export async function searchBooks(config, query, opts) {
|
|
|
72
76
|
return { output: { books, kindles }, warnings };
|
|
73
77
|
}
|
|
74
78
|
async function findFormat(client, foreignBookId, format) {
|
|
75
|
-
const found =
|
|
76
|
-
|
|
79
|
+
const found = await client.lookup(foreignBookId);
|
|
80
|
+
const exact = found.filter((b) => b.foreignBookId === foreignBookId);
|
|
81
|
+
const pool = exact.length > 0 ? exact : found;
|
|
82
|
+
return pool.find((b) => b.mediaType === format) ?? pool[0] ?? null;
|
|
83
|
+
}
|
|
84
|
+
export function plainEdition(editions) {
|
|
85
|
+
const current = editions.find((e) => e.monitored);
|
|
86
|
+
if (current === undefined)
|
|
87
|
+
return null;
|
|
88
|
+
const narrators = current.narrators.join('|');
|
|
89
|
+
const plain = editions.filter((e) => e.id !== current.id &&
|
|
90
|
+
e.language === current.language &&
|
|
91
|
+
e.narrators.join('|') === narrators &&
|
|
92
|
+
current.title.startsWith(`${e.title} (`));
|
|
93
|
+
return plain.sort((a, b) => a.title.length - b.title.length)[0] ?? null;
|
|
77
94
|
}
|
|
78
95
|
async function ensureBook(client, lookup, format, opts) {
|
|
79
96
|
const local = preferred(lookup[format]);
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
97
|
+
const id = local?.id ?? (await client.addBook(lookup, format, await client.formatDefaults(), opts));
|
|
98
|
+
const plain = plainEdition(await client.editions(id));
|
|
99
|
+
if (plain !== null)
|
|
100
|
+
await client.monitorEdition(id, plain.id);
|
|
101
|
+
return id;
|
|
102
|
+
}
|
|
103
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
104
|
+
async function searchOutcome(client, bookId, timing) {
|
|
105
|
+
const started = await client.searchBook(bookId);
|
|
106
|
+
const deadline = Date.now() + timing.waitMs;
|
|
107
|
+
let command = started;
|
|
108
|
+
while (RUNNING_COMMAND.has(command.status)) {
|
|
109
|
+
if (Date.now() >= deadline)
|
|
110
|
+
return { status: 'searching', detail: null };
|
|
111
|
+
await sleep(timing.pollMs);
|
|
112
|
+
command = await client.command(started.id);
|
|
113
|
+
}
|
|
114
|
+
if (command.status !== 'completed')
|
|
115
|
+
return { status: 'error', detail: `Chaptarr's search ended ${command.status}` };
|
|
116
|
+
const grabbed = await client.grabbedSince(bookId, started.queued);
|
|
117
|
+
return { status: grabbed ? 'downloading' : 'nothing_found', detail: null };
|
|
118
|
+
}
|
|
119
|
+
async function sabPausedFor(config, client, format, warnings) {
|
|
120
|
+
const paused = await sabPaused(config, warnings);
|
|
121
|
+
if (paused !== true || format === null)
|
|
122
|
+
return paused;
|
|
123
|
+
try {
|
|
124
|
+
const category = await client.sabnzbdCategory(format);
|
|
125
|
+
if (category === null)
|
|
126
|
+
return true;
|
|
127
|
+
const priority = await new SabnzbdClient(config.sabnzbd.url, config.sabnzbd.apiKey).categoryPriority(category);
|
|
128
|
+
return priority !== SAB_FORCE_PRIORITY;
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
warnings.push(`SABnzbd category priority unknown (${describeError(err)})`);
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
84
134
|
}
|
|
85
|
-
export async function addBook(config, foreignBookId, format) {
|
|
135
|
+
export async function addBook(config, foreignBookId, format, timing = SEARCH_TIMING) {
|
|
86
136
|
const client = chaptarr(config);
|
|
87
137
|
const warnings = [];
|
|
88
138
|
const base = { foreign_book_id: foreignBookId, format };
|
|
89
139
|
let result;
|
|
90
140
|
try {
|
|
91
141
|
const lookup = await findFormat(client, foreignBookId, format);
|
|
142
|
+
const local = lookup === null ? null : preferred(lookup[format]);
|
|
92
143
|
if (lookup === null) {
|
|
93
144
|
result = { book_id: null, title: null, status: 'not_found', detail: null };
|
|
94
145
|
}
|
|
146
|
+
else if (local?.hasFiles === true) {
|
|
147
|
+
result = { book_id: local.id, title: lookup.title, status: 'already_exists', detail: null };
|
|
148
|
+
}
|
|
149
|
+
else if (local !== null && (await client.queuedBookIds()).has(local.id)) {
|
|
150
|
+
result = { book_id: local.id, title: lookup.title, status: 'downloading', detail: null };
|
|
151
|
+
}
|
|
95
152
|
else {
|
|
96
|
-
const
|
|
97
|
-
if (local
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
await client.setMonitored([local.id], true);
|
|
102
|
-
await client.searchBook(local.id);
|
|
103
|
-
result = { book_id: local.id, title: lookup.title, status: 'searching', detail: null };
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
const { id } = await ensureBook(client, lookup, format, { monitored: true, search: true });
|
|
107
|
-
result = { book_id: id, title: lookup.title, status: 'added', detail: null };
|
|
108
|
-
}
|
|
153
|
+
const id = await ensureBook(client, lookup, format, { monitored: true, search: false });
|
|
154
|
+
if (local !== null)
|
|
155
|
+
await client.setMonitored([id], true);
|
|
156
|
+
const title = (await client.book(id)).title;
|
|
157
|
+
result = { book_id: id, title, ...(await searchOutcome(client, id, timing)) };
|
|
109
158
|
}
|
|
110
159
|
}
|
|
111
160
|
catch (err) {
|
|
112
161
|
result = { book_id: null, title: null, status: 'error', detail: httpDetail(err) };
|
|
113
162
|
}
|
|
114
|
-
const sabnzbd_paused = await
|
|
163
|
+
const sabnzbd_paused = await sabPausedFor(config, client, format, warnings);
|
|
115
164
|
return { output: { ...base, ...result, sabnzbd_paused }, warnings };
|
|
116
165
|
}
|
|
117
166
|
function ageDays(publishDate, now) {
|
|
@@ -143,10 +192,11 @@ export async function bookReleases(config, foreignBookId, format, opts) {
|
|
|
143
192
|
const lookup = await findFormat(client, foreignBookId, format);
|
|
144
193
|
if (lookup === null)
|
|
145
194
|
throw new Error(`no book ${foreignBookId} in Chaptarr's lookup`);
|
|
146
|
-
const
|
|
195
|
+
const id = await ensureBook(client, lookup, format, { monitored: false, search: false });
|
|
196
|
+
const title = (await client.book(id)).title;
|
|
147
197
|
const releases = selectReleases(await client.releases(id), opts.maxEntries ?? config.defaults.maxEntries);
|
|
148
198
|
return {
|
|
149
|
-
output: { foreign_book_id: foreignBookId, format, book_id: id, title
|
|
199
|
+
output: { foreign_book_id: foreignBookId, format, book_id: id, title, releases },
|
|
150
200
|
warnings: [],
|
|
151
201
|
};
|
|
152
202
|
}
|
|
@@ -154,9 +204,12 @@ export async function grabRelease(config, bookId, guid, indexerId) {
|
|
|
154
204
|
const client = chaptarr(config);
|
|
155
205
|
const warnings = [];
|
|
156
206
|
let title = null;
|
|
207
|
+
let format = null;
|
|
157
208
|
let result;
|
|
158
209
|
try {
|
|
159
|
-
|
|
210
|
+
const book = await client.book(bookId);
|
|
211
|
+
title = book.title;
|
|
212
|
+
format = book.mediaType === 'ebook' ? 'ebook' : 'audiobook';
|
|
160
213
|
await client.setMonitored([bookId], true);
|
|
161
214
|
await client.grab(guid, indexerId);
|
|
162
215
|
result = { status: 'grabbed', detail: null };
|
|
@@ -164,7 +217,7 @@ export async function grabRelease(config, bookId, guid, indexerId) {
|
|
|
164
217
|
catch (err) {
|
|
165
218
|
result = { status: 'error', detail: httpDetail(err) };
|
|
166
219
|
}
|
|
167
|
-
const sabnzbd_paused = await
|
|
220
|
+
const sabnzbd_paused = await sabPausedFor(config, client, format, warnings);
|
|
168
221
|
return { output: { book_id: bookId, title, ...result, sabnzbd_paused }, warnings };
|
|
169
222
|
}
|
|
170
223
|
export async function sendBook(config, bookId, device) {
|
package/dist/format.js
CHANGED
|
@@ -120,7 +120,7 @@ function sabLine(paused) {
|
|
|
120
120
|
}
|
|
121
121
|
export function renderBookAdd(output) {
|
|
122
122
|
const detail = output.detail === null ? '' : ` (${output.detail})`;
|
|
123
|
-
const line = `${output.title ?? output.foreign_book_id} [${output.format}] ${statusColor(output.status, ['
|
|
123
|
+
const line = `${output.title ?? output.foreign_book_id} [${output.format}] ${statusColor(output.status, ['downloading', 'searching'])}${detail}`;
|
|
124
124
|
return [line, '', sabLine(output.sabnzbd_paused)].join('\n');
|
|
125
125
|
}
|
|
126
126
|
function megabytes(size) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrofoundry/trendarr",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.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",
|