@astrofoundry/trendarr 0.6.0 → 0.8.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 +2 -1
- package/dist/cli.js +52 -1
- package/dist/clients/radarr.js +5 -0
- package/dist/clients/sonarr.js +9 -0
- package/dist/clients/tmdb.js +18 -1
- package/dist/commands/recommend.js +186 -0
- package/dist/format.js +12 -0
- package/package.json +2 -2
- package/dist/clients/trakt.js +0 -91
package/README.md
CHANGED
|
@@ -15,12 +15,13 @@ Node 24+. Run `trendarr init` once — prompts for TMDB API Read Access Token (t
|
|
|
15
15
|
```bash
|
|
16
16
|
trendarr trending [--max-entries 10] [--type movies|series] [--min-rating 6.5] [--json|--discord]
|
|
17
17
|
trendarr search <query> [--max-entries 10] [--type movies|series] [--json|--discord]
|
|
18
|
+
trendarr recommend [--max-entries 10] [--type movies|series] [--exclude tt1,tt2] [--json]
|
|
18
19
|
trendarr add-movies <imdbIds...> [--json]
|
|
19
20
|
trendarr add-series <imdbIds...> [--json]
|
|
20
21
|
trendarr sabnzbd status|pause|resume|unpause [--json]
|
|
21
22
|
```
|
|
22
23
|
|
|
23
|
-
`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.
|
|
24
|
+
`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).
|
|
24
25
|
|
|
25
26
|
## Development
|
|
26
27
|
|
package/dist/cli.js
CHANGED
|
@@ -4,10 +4,11 @@ import { Command, InvalidArgumentError } from 'commander';
|
|
|
4
4
|
import { addMovies, addSeries, hasFailure } from './commands/add.js';
|
|
5
5
|
import { runInit } from './commands/init.js';
|
|
6
6
|
import { runSabnzbd } from './commands/sabnzbd.js';
|
|
7
|
+
import { CANDIDATE_LIMIT, getRecommendations } from './commands/recommend.js';
|
|
7
8
|
import { getSearch } from './commands/search.js';
|
|
8
9
|
import { getTrending } from './commands/trending.js';
|
|
9
10
|
import { ConfigError, loadConfig } from './config.js';
|
|
10
|
-
import { renderAddTable, renderSabStatus, renderSearchDiscord, renderSearchTable, renderTrendingDiscord, renderTrendingTable, toJson, } from './format.js';
|
|
11
|
+
import { renderAddTable, renderRecommendTable, renderSabStatus, renderSearchDiscord, renderSearchTable, renderTrendingDiscord, renderTrendingTable, toJson, } from './format.js';
|
|
11
12
|
import { describeError } from './http.js';
|
|
12
13
|
const pkg = createRequire(import.meta.url)('../package.json');
|
|
13
14
|
const program = new Command();
|
|
@@ -28,6 +29,13 @@ function parseRating(value) {
|
|
|
28
29
|
throw new InvalidArgumentError('must be a number between 0 and 10');
|
|
29
30
|
return n;
|
|
30
31
|
}
|
|
32
|
+
function parseImdbList(value) {
|
|
33
|
+
const ids = [...new Set(value.split(',').map((p) => p.trim()).filter((p) => p.length > 0))];
|
|
34
|
+
const bad = ids.find((id) => !/^tt\d+$/.test(id));
|
|
35
|
+
if (bad !== undefined)
|
|
36
|
+
throw new InvalidArgumentError(`not an IMDb id: ${bad}`);
|
|
37
|
+
return ids;
|
|
38
|
+
}
|
|
31
39
|
function fail(err) {
|
|
32
40
|
process.stderr.write(`${err instanceof ConfigError ? err.message : describeError(err)}\n`);
|
|
33
41
|
process.exit(1);
|
|
@@ -54,6 +62,7 @@ Commands:
|
|
|
54
62
|
init Interactive setup with live credential validation.
|
|
55
63
|
trending Trending movies and series (TMDB, weekly) with IMDb ratings and a library flag.
|
|
56
64
|
search <query> Search movies and series by name (full or partial).
|
|
65
|
+
recommend Titles TMDB recommends for your Radarr/Sonarr library, not yet in it.
|
|
57
66
|
add-movies <imdbIds...> Add movies to Radarr by IMDb id and start a search.
|
|
58
67
|
add-series <imdbIds...> Add series to Sonarr by IMDb id and start a search.
|
|
59
68
|
sabnzbd <action> Read or toggle the SABnzbd queue pause state.
|
|
@@ -163,6 +172,48 @@ Examples:
|
|
|
163
172
|
fail(err);
|
|
164
173
|
}
|
|
165
174
|
});
|
|
175
|
+
program
|
|
176
|
+
.command('recommend')
|
|
177
|
+
.description('titles TMDB recommends for the most library titles, released, well rated, not in the library')
|
|
178
|
+
.option('--max-entries <n>', 'results per media type', parsePositiveInt)
|
|
179
|
+
.option('--type <type>', "'movies' or 'series'", parseType)
|
|
180
|
+
.option('--exclude <ids>', 'comma-separated IMDb ids to leave out', parseImdbList)
|
|
181
|
+
.option('--json', 'output JSON')
|
|
182
|
+
.addHelpText('after', `
|
|
183
|
+
Parameters:
|
|
184
|
+
--max-entries <n> Results per media type (positive integer). Default: 10 (config defaults.maxEntries).
|
|
185
|
+
--type <type> Restrict to "movies" or "series". Default: both.
|
|
186
|
+
--exclude <ids> Comma-separated IMDb ids that never appear (e.g. titles already suggested).
|
|
187
|
+
--json Output JSON instead of the table.
|
|
188
|
+
|
|
189
|
+
Behavior: reads every Radarr movie and Sonarr series, asks TMDB for the recommendations of each, and
|
|
190
|
+
ranks the titles not in the library by how many library titles lead to them, then by TMDB popularity.
|
|
191
|
+
A title is kept only when it is released (movies: a digital or physical release date that has passed;
|
|
192
|
+
series: a first air date that has passed) and its IMDb rating is 7.0 or higher with at least 5,000
|
|
193
|
+
votes (movies) or 2,000 votes (series). At most ${CANDIDATE_LIMIT} candidates per type are checked. With a large
|
|
194
|
+
library this takes minutes.
|
|
195
|
+
|
|
196
|
+
Output (table): a MOVIES block then a SERIES block, best first.
|
|
197
|
+
Columns: # rank, TITLE, YEAR, IMDB (rating), VOTES, BECAUSE (library titles, +N more), LINK.
|
|
198
|
+
Output (--json): { "movies": RecommendItem[], "series": RecommendItem[] }
|
|
199
|
+
RecommendItem: the search MediaItem plus "matches" (how many library titles lead to it) and
|
|
200
|
+
"because" (up to three of those titles). in_library is always false.
|
|
201
|
+
|
|
202
|
+
Examples:
|
|
203
|
+
trendarr recommend
|
|
204
|
+
trendarr recommend --type movies --max-entries 3
|
|
205
|
+
trendarr recommend --exclude tt1160419,tt0903747 --json`)
|
|
206
|
+
.action(async (opts) => {
|
|
207
|
+
try {
|
|
208
|
+
const config = loadConfig();
|
|
209
|
+
const { output, warnings } = await getRecommendations(config, opts);
|
|
210
|
+
printWarnings(warnings);
|
|
211
|
+
process.stdout.write(`${opts.json === true ? toJson(output) : renderRecommendTable(output)}\n`);
|
|
212
|
+
}
|
|
213
|
+
catch (err) {
|
|
214
|
+
fail(err);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
166
217
|
function addAction(kind) {
|
|
167
218
|
return async (ids, opts) => {
|
|
168
219
|
try {
|
package/dist/clients/radarr.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { ArrClient } from './arr.js';
|
|
3
3
|
const librarySchema = z.array(z.object({ tmdbId: z.number().nullish() }));
|
|
4
|
+
const libraryTitlesSchema = z.array(z.object({ tmdbId: z.number().nullish(), title: z.string().nullish() }));
|
|
4
5
|
export class RadarrClient extends ArrClient {
|
|
5
6
|
async lookupByImdb(imdbId) {
|
|
6
7
|
const raw = await this.getJson(`/api/v3/movie/lookup/imdb?imdbId=${encodeURIComponent(imdbId)}`);
|
|
@@ -22,6 +23,10 @@ export class RadarrClient extends ArrClient {
|
|
|
22
23
|
return this.mapAddError(err);
|
|
23
24
|
}
|
|
24
25
|
}
|
|
26
|
+
async libraryMovies() {
|
|
27
|
+
const items = libraryTitlesSchema.parse(await this.getJson('/api/v3/movie'));
|
|
28
|
+
return items.flatMap((m) => typeof m.tmdbId === 'number' && m.tmdbId > 0 ? [{ tmdbId: m.tmdbId, title: m.title ?? 'Unknown movie' }] : []);
|
|
29
|
+
}
|
|
25
30
|
async libraryTmdbIds() {
|
|
26
31
|
const items = librarySchema.parse(await this.getJson('/api/v3/movie'));
|
|
27
32
|
return new Set(items.flatMap((m) => (typeof m.tmdbId === 'number' ? [m.tmdbId] : [])));
|
package/dist/clients/sonarr.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { ArrClient } from './arr.js';
|
|
3
3
|
const librarySchema = z.array(z.object({ tvdbId: z.number().nullish() }));
|
|
4
|
+
const libraryTitlesSchema = z.array(z.object({ tmdbId: z.number().nullish(), tvdbId: z.number().nullish(), title: z.string().nullish() }));
|
|
4
5
|
export class SonarrClient extends ArrClient {
|
|
5
6
|
async lookupByTvdb(tvdbId) {
|
|
6
7
|
const raw = await this.getJson(`/api/v3/series/lookup?term=${encodeURIComponent(`tvdb:${tvdbId}`)}`);
|
|
@@ -24,6 +25,14 @@ export class SonarrClient extends ArrClient {
|
|
|
24
25
|
return this.mapAddError(err);
|
|
25
26
|
}
|
|
26
27
|
}
|
|
28
|
+
async librarySeries() {
|
|
29
|
+
const items = libraryTitlesSchema.parse(await this.getJson('/api/v3/series'));
|
|
30
|
+
return items.map((s) => ({
|
|
31
|
+
tmdbId: typeof s.tmdbId === 'number' && s.tmdbId > 0 ? s.tmdbId : null,
|
|
32
|
+
tvdbId: typeof s.tvdbId === 'number' && s.tvdbId > 0 ? s.tvdbId : null,
|
|
33
|
+
title: s.title ?? 'Unknown series',
|
|
34
|
+
}));
|
|
35
|
+
}
|
|
27
36
|
async libraryTvdbIds() {
|
|
28
37
|
const items = librarySchema.parse(await this.getJson('/api/v3/series'));
|
|
29
38
|
return new Set(items.flatMap((s) => (typeof s.tvdbId === 'number' ? [s.tvdbId] : [])));
|
package/dist/clients/tmdb.js
CHANGED
|
@@ -23,7 +23,11 @@ const genreListSchema = z.object({ genres: z.array(z.object({ id: z.number(), na
|
|
|
23
23
|
const movieExternalIdsSchema = z.object({ imdb_id: z.string().nullish() });
|
|
24
24
|
const showExternalIdsSchema = z.object({ imdb_id: z.string().nullish(), tvdb_id: z.number().nullish() });
|
|
25
25
|
const findSchema = z.object({ tv_results: z.array(z.object({ id: z.number() })) });
|
|
26
|
-
|
|
26
|
+
const releaseDatesSchema = z.object({
|
|
27
|
+
results: z.array(z.object({ release_dates: z.array(z.object({ release_date: z.string(), type: z.number() })) })),
|
|
28
|
+
});
|
|
29
|
+
export const HOME_RELEASE_TYPES = [4, 5];
|
|
30
|
+
export function yearOf(date) {
|
|
27
31
|
if (date === undefined || date === null || date.length < 4)
|
|
28
32
|
return null;
|
|
29
33
|
const year = Number(date.slice(0, 4));
|
|
@@ -80,6 +84,19 @@ export class TmdbClient {
|
|
|
80
84
|
this.genreMaps[kind] = map;
|
|
81
85
|
return map;
|
|
82
86
|
}
|
|
87
|
+
async genres(kind, ids) {
|
|
88
|
+
return genreNames(ids, await this.genreMap(kind));
|
|
89
|
+
}
|
|
90
|
+
async movieRecommendations(tmdbId) {
|
|
91
|
+
return moviePageSchema.parse(await this.get(`/movie/${tmdbId}/recommendations?page=1`)).results;
|
|
92
|
+
}
|
|
93
|
+
async showRecommendations(tmdbId) {
|
|
94
|
+
return showPageSchema.parse(await this.get(`/tv/${tmdbId}/recommendations?page=1`)).results;
|
|
95
|
+
}
|
|
96
|
+
async movieHomeRelease(tmdbId, now) {
|
|
97
|
+
const parsed = releaseDatesSchema.parse(await this.get(`/movie/${tmdbId}/release_dates`));
|
|
98
|
+
return parsed.results.some((country) => country.release_dates.some((r) => HOME_RELEASE_TYPES.includes(r.type) && Number.isFinite(Date.parse(r.release_date)) && Date.parse(r.release_date) <= now));
|
|
99
|
+
}
|
|
83
100
|
async movieImdbId(tmdbId) {
|
|
84
101
|
const raw = await this.get(`/movie/${tmdbId}/external_ids`);
|
|
85
102
|
const imdbId = movieExternalIdsSchema.parse(raw).imdb_id;
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { OmdbClient } from '../clients/omdb.js';
|
|
2
|
+
import { RadarrClient } from '../clients/radarr.js';
|
|
3
|
+
import { SonarrClient } from '../clients/sonarr.js';
|
|
4
|
+
import { TmdbClient, yearOf } from '../clients/tmdb.js';
|
|
5
|
+
import { mapConcurrent } from '../concurrency.js';
|
|
6
|
+
import { describeError } from '../http.js';
|
|
7
|
+
export const MIN_RATING = 7;
|
|
8
|
+
export const MIN_VOTES = { movies: 5000, series: 2000 };
|
|
9
|
+
export const BECAUSE_NAMES = 3;
|
|
10
|
+
export const CANDIDATE_LIMIT = 200;
|
|
11
|
+
export const RECOMMEND_CONCURRENCY = 5;
|
|
12
|
+
function isPast(date, now) {
|
|
13
|
+
if (date === undefined || date === null || date.length === 0)
|
|
14
|
+
return false;
|
|
15
|
+
const time = Date.parse(date);
|
|
16
|
+
return Number.isFinite(time) && time <= now;
|
|
17
|
+
}
|
|
18
|
+
export function votesOf(value) {
|
|
19
|
+
if (value === null)
|
|
20
|
+
return null;
|
|
21
|
+
const digits = value.replace(/[^0-9]/g, '');
|
|
22
|
+
return digits.length === 0 ? null : Number(digits);
|
|
23
|
+
}
|
|
24
|
+
export function passesThresholds(enrich, minVotes) {
|
|
25
|
+
if (enrich === null || enrich.imdbRating === null)
|
|
26
|
+
return false;
|
|
27
|
+
const rating = Number.parseFloat(enrich.imdbRating);
|
|
28
|
+
const votes = votesOf(enrich.imdbVotes);
|
|
29
|
+
return Number.isFinite(rating) && rating >= MIN_RATING && votes !== null && votes >= minVotes;
|
|
30
|
+
}
|
|
31
|
+
export function rankCandidates(lists, library) {
|
|
32
|
+
const byId = new Map();
|
|
33
|
+
for (const { source, results } of lists) {
|
|
34
|
+
for (const result of results) {
|
|
35
|
+
if (library.has(result.id))
|
|
36
|
+
continue;
|
|
37
|
+
const known = byId.get(result.id);
|
|
38
|
+
if (known === undefined)
|
|
39
|
+
byId.set(result.id, { result, because: [source] });
|
|
40
|
+
else if (!known.because.includes(source))
|
|
41
|
+
known.because.push(source);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return [...byId.values()].sort((a, b) => b.because.length - a.because.length || b.result.popularity - a.result.popularity);
|
|
45
|
+
}
|
|
46
|
+
async function collect(sources, kind, warnings) {
|
|
47
|
+
const errors = [];
|
|
48
|
+
const lists = await mapConcurrent(sources, RECOMMEND_CONCURRENCY, async (s) => {
|
|
49
|
+
try {
|
|
50
|
+
return { source: s.title, results: await kind.fetch(s.tmdbId) };
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
errors.push(err);
|
|
54
|
+
return { source: s.title, results: [] };
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
if (errors.length > 0 && errors.length === sources.length)
|
|
58
|
+
throw errors[0];
|
|
59
|
+
if (errors.length > 0) {
|
|
60
|
+
warnings.push(`TMDB recommendations failed for ${errors.length} of ${sources.length} library ${kind.label} (${describeError(errors[0])})`);
|
|
61
|
+
}
|
|
62
|
+
return lists;
|
|
63
|
+
}
|
|
64
|
+
async function select(ranked, kind, limit, warnings) {
|
|
65
|
+
const picked = [];
|
|
66
|
+
const queue = ranked.slice(0, CANDIDATE_LIMIT);
|
|
67
|
+
let failures = 0;
|
|
68
|
+
let firstError;
|
|
69
|
+
for (let i = 0; i < queue.length && picked.length < limit; i += RECOMMEND_CONCURRENCY) {
|
|
70
|
+
const batch = queue.slice(i, i + RECOMMEND_CONCURRENCY);
|
|
71
|
+
const checked = await mapConcurrent(batch, RECOMMEND_CONCURRENCY, async (c) => {
|
|
72
|
+
try {
|
|
73
|
+
return await kind.check(c);
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
failures += 1;
|
|
77
|
+
firstError ??= err;
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
for (const [j, result] of checked.entries()) {
|
|
82
|
+
const candidate = batch[j];
|
|
83
|
+
if (result === null || candidate === undefined || picked.length >= limit)
|
|
84
|
+
continue;
|
|
85
|
+
picked.push(await kind.toItem(candidate, result, picked.length + 1));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (failures > 0)
|
|
89
|
+
warnings.push(`${failures} ${kind.label} candidates could not be checked (${describeError(firstError)})`);
|
|
90
|
+
return picked;
|
|
91
|
+
}
|
|
92
|
+
function baseItem(candidate, checked, rank, title, year, genres) {
|
|
93
|
+
return {
|
|
94
|
+
rank,
|
|
95
|
+
title,
|
|
96
|
+
year,
|
|
97
|
+
imdb_rating: checked.enrich.imdbRating,
|
|
98
|
+
imdb_votes: checked.enrich.imdbVotes,
|
|
99
|
+
rotten_tomatoes: checked.enrich.rottenTomatoes,
|
|
100
|
+
metascore: checked.enrich.metascore,
|
|
101
|
+
genres,
|
|
102
|
+
imdb_id: checked.imdbId,
|
|
103
|
+
tmdb_id: candidate.result.id,
|
|
104
|
+
tvdb_id: checked.tvdbId,
|
|
105
|
+
in_library: false,
|
|
106
|
+
matches: candidate.because.length,
|
|
107
|
+
because: candidate.because.slice(0, BECAUSE_NAMES),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
export async function getRecommendations(config, opts) {
|
|
111
|
+
const tmdb = new TmdbClient(config.tmdb.accessToken);
|
|
112
|
+
const omdb = new OmdbClient(config.omdb.apiKey);
|
|
113
|
+
const radarr = new RadarrClient(config.radarr.url, config.radarr.apiKey);
|
|
114
|
+
const sonarr = new SonarrClient(config.sonarr.url, config.sonarr.apiKey);
|
|
115
|
+
const limit = opts.maxEntries ?? config.defaults.maxEntries;
|
|
116
|
+
const exclude = new Set(opts.exclude ?? []);
|
|
117
|
+
const now = Date.now();
|
|
118
|
+
const warnings = [];
|
|
119
|
+
const movieKind = {
|
|
120
|
+
label: 'movies',
|
|
121
|
+
fetch: (id) => tmdb.movieRecommendations(id),
|
|
122
|
+
check: async (c) => {
|
|
123
|
+
if (!isPast(c.result.release_date, now))
|
|
124
|
+
return null;
|
|
125
|
+
const imdbId = await tmdb.movieImdbId(c.result.id);
|
|
126
|
+
if (imdbId === null || exclude.has(imdbId))
|
|
127
|
+
return null;
|
|
128
|
+
if (!(await tmdb.movieHomeRelease(c.result.id, now)))
|
|
129
|
+
return null;
|
|
130
|
+
const enrich = await omdb.byImdbId(imdbId);
|
|
131
|
+
return passesThresholds(enrich, MIN_VOTES.movies) ? { imdbId, tvdbId: null, enrich } : null;
|
|
132
|
+
},
|
|
133
|
+
toItem: async (c, checked, rank) => baseItem(c, checked, rank, c.result.title, yearOf(c.result.release_date), await tmdb.genres('movie', c.result.genre_ids)),
|
|
134
|
+
};
|
|
135
|
+
const recommendMovies = async () => {
|
|
136
|
+
let library;
|
|
137
|
+
try {
|
|
138
|
+
library = await radarr.libraryMovies();
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
warnings.push(`Radarr unreachable — no movie recommendations (${describeError(err)})`);
|
|
142
|
+
return [];
|
|
143
|
+
}
|
|
144
|
+
if (library.length === 0)
|
|
145
|
+
return [];
|
|
146
|
+
const lists = await collect(library, movieKind, warnings);
|
|
147
|
+
const ranked = rankCandidates(lists, new Set(library.map((m) => m.tmdbId)));
|
|
148
|
+
return select(ranked, movieKind, limit, warnings);
|
|
149
|
+
};
|
|
150
|
+
const recommendSeries = async () => {
|
|
151
|
+
let library;
|
|
152
|
+
try {
|
|
153
|
+
library = await sonarr.librarySeries();
|
|
154
|
+
}
|
|
155
|
+
catch (err) {
|
|
156
|
+
warnings.push(`Sonarr unreachable — no series recommendations (${describeError(err)})`);
|
|
157
|
+
return [];
|
|
158
|
+
}
|
|
159
|
+
const libraryTvdb = new Set(library.flatMap((s) => (s.tvdbId === null ? [] : [s.tvdbId])));
|
|
160
|
+
const sources = library.flatMap((s) => (s.tmdbId === null ? [] : [{ tmdbId: s.tmdbId, title: s.title }]));
|
|
161
|
+
if (sources.length === 0)
|
|
162
|
+
return [];
|
|
163
|
+
const seriesKind = {
|
|
164
|
+
label: 'series',
|
|
165
|
+
fetch: (id) => tmdb.showRecommendations(id),
|
|
166
|
+
check: async (c) => {
|
|
167
|
+
if (!isPast(c.result.first_air_date, now))
|
|
168
|
+
return null;
|
|
169
|
+
const { imdbId, tvdbId } = await tmdb.showExternalIds(c.result.id);
|
|
170
|
+
if (imdbId === null || exclude.has(imdbId) || (tvdbId !== null && libraryTvdb.has(tvdbId)))
|
|
171
|
+
return null;
|
|
172
|
+
const enrich = await omdb.byImdbId(imdbId);
|
|
173
|
+
return passesThresholds(enrich, MIN_VOTES.series) ? { imdbId, tvdbId, enrich } : null;
|
|
174
|
+
},
|
|
175
|
+
toItem: async (c, checked, rank) => baseItem(c, checked, rank, c.result.name, yearOf(c.result.first_air_date), await tmdb.genres('tv', c.result.genre_ids)),
|
|
176
|
+
};
|
|
177
|
+
const lists = await collect(sources, seriesKind, warnings);
|
|
178
|
+
const ranked = rankCandidates(lists, new Set(sources.map((s) => s.tmdbId)));
|
|
179
|
+
return select(ranked, seriesKind, limit, warnings);
|
|
180
|
+
};
|
|
181
|
+
const [movies, series] = await Promise.all([
|
|
182
|
+
opts.type === 'series' ? Promise.resolve([]) : recommendMovies(),
|
|
183
|
+
opts.type === 'movies' ? Promise.resolve([]) : recommendSeries(),
|
|
184
|
+
]);
|
|
185
|
+
return { output: { movies, series }, warnings };
|
|
186
|
+
}
|
package/dist/format.js
CHANGED
|
@@ -19,6 +19,15 @@ function searchRows(items) {
|
|
|
19
19
|
const rows = items.map((i) => `${cell(String(i.rank), 3)} ${cell(i.title, 34)} ${cell(dash(i.year), 5)} ${cell(dash(i.imdb_rating), 5)} ${cell(tableLib(i), 3)} ${tableLink(i)}`);
|
|
20
20
|
return [pc.bold(header), ...rows];
|
|
21
21
|
}
|
|
22
|
+
function recommendRows(items) {
|
|
23
|
+
const header = `${cell('#', 3)} ${cell('TITLE', 34)} ${cell('YEAR', 5)} ${cell('IMDB', 5)} ${cell('VOTES', 10)} ${cell('BECAUSE', 40)} LINK`;
|
|
24
|
+
const rows = items.map((i) => {
|
|
25
|
+
const more = i.matches - i.because.length;
|
|
26
|
+
const because = `${i.because.join(', ')}${more > 0 ? ` +${more}` : ''}`;
|
|
27
|
+
return `${cell(String(i.rank), 3)} ${cell(i.title, 34)} ${cell(dash(i.year), 5)} ${cell(dash(i.imdb_rating), 5)} ${cell(dash(i.imdb_votes), 10)} ${cell(because, 40)} ${tableLink(i)}`;
|
|
28
|
+
});
|
|
29
|
+
return [pc.bold(header), ...rows];
|
|
30
|
+
}
|
|
22
31
|
function renderBlocks(movies, series, moviesEmpty, seriesEmpty) {
|
|
23
32
|
const sections = [];
|
|
24
33
|
if (!moviesEmpty)
|
|
@@ -35,6 +44,9 @@ function renderBlocks(movies, series, moviesEmpty, seriesEmpty) {
|
|
|
35
44
|
export function renderTrendingTable(output) {
|
|
36
45
|
return renderBlocks(trendingRows(output.movies), trendingRows(output.series), output.movies.length === 0, output.series.length === 0);
|
|
37
46
|
}
|
|
47
|
+
export function renderRecommendTable(output) {
|
|
48
|
+
return renderBlocks(recommendRows(output.movies), recommendRows(output.series), output.movies.length === 0, output.series.length === 0);
|
|
49
|
+
}
|
|
38
50
|
export function renderSearchTable(output) {
|
|
39
51
|
return renderBlocks(searchRows(output.movies), searchRows(output.series), output.movies.length === 0, output.series.length === 0);
|
|
40
52
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrofoundry/trendarr",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.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",
|
|
@@ -42,6 +42,6 @@
|
|
|
42
42
|
},
|
|
43
43
|
"repository": {
|
|
44
44
|
"type": "git",
|
|
45
|
-
"url": "git+https://github.com/astrofoundry/
|
|
45
|
+
"url": "git+https://github.com/astrofoundry/trendarr.git"
|
|
46
46
|
}
|
|
47
47
|
}
|
package/dist/clients/trakt.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { HttpError, fetchJson } from '../http.js';
|
|
3
|
-
const BASE = 'https://api.trakt.tv';
|
|
4
|
-
const idsSchema = z.object({
|
|
5
|
-
imdb: z.string().nullish(),
|
|
6
|
-
tmdb: z.number().nullish(),
|
|
7
|
-
tvdb: z.number().nullish(),
|
|
8
|
-
});
|
|
9
|
-
const mediaSchema = z.object({
|
|
10
|
-
title: z.string(),
|
|
11
|
-
year: z.number().nullish(),
|
|
12
|
-
ids: idsSchema,
|
|
13
|
-
rating: z.number().nullish(),
|
|
14
|
-
votes: z.number().nullish(),
|
|
15
|
-
genres: z.array(z.string()).nullish(),
|
|
16
|
-
});
|
|
17
|
-
const trendingMoviesSchema = z.array(z.object({ watchers: z.number(), movie: mediaSchema }));
|
|
18
|
-
const trendingShowsSchema = z.array(z.object({ watchers: z.number(), show: mediaSchema }));
|
|
19
|
-
const showSearchSchema = z.array(z.object({ type: z.string(), show: mediaSchema.optional() }));
|
|
20
|
-
const movieResultsSchema = z.array(z.object({ type: z.string(), movie: mediaSchema.optional() }));
|
|
21
|
-
const showResultsSchema = z.array(z.object({ type: z.string(), show: mediaSchema.optional() }));
|
|
22
|
-
function toMedia(media) {
|
|
23
|
-
return {
|
|
24
|
-
title: media.title,
|
|
25
|
-
year: media.year ?? null,
|
|
26
|
-
imdbId: media.ids.imdb ?? null,
|
|
27
|
-
tmdbId: media.ids.tmdb ?? null,
|
|
28
|
-
tvdbId: media.ids.tvdb ?? null,
|
|
29
|
-
rating: media.rating ?? null,
|
|
30
|
-
votes: media.votes ?? null,
|
|
31
|
-
genres: media.genres ?? [],
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
function toTrending(watchers, media) {
|
|
35
|
-
return { ...toMedia(media), watchers };
|
|
36
|
-
}
|
|
37
|
-
export class TraktClient {
|
|
38
|
-
clientId;
|
|
39
|
-
constructor(clientId) {
|
|
40
|
-
this.clientId = clientId;
|
|
41
|
-
}
|
|
42
|
-
headers() {
|
|
43
|
-
return {
|
|
44
|
-
'trakt-api-version': '2',
|
|
45
|
-
'trakt-api-key': this.clientId,
|
|
46
|
-
'User-Agent': 'traktarr',
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
async get(path) {
|
|
50
|
-
const url = `${BASE}${path}`;
|
|
51
|
-
try {
|
|
52
|
-
return await fetchJson(url, { headers: this.headers() });
|
|
53
|
-
}
|
|
54
|
-
catch (err) {
|
|
55
|
-
if (err instanceof HttpError && err.status === 429 && err.retryAfterSeconds !== undefined) {
|
|
56
|
-
const retryAfterSeconds = err.retryAfterSeconds;
|
|
57
|
-
await new Promise((r) => setTimeout(r, retryAfterSeconds * 1000));
|
|
58
|
-
return fetchJson(url, { headers: this.headers() });
|
|
59
|
-
}
|
|
60
|
-
throw err;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
async trendingMovies(limit) {
|
|
64
|
-
const raw = await this.get(`/movies/trending?extended=full&limit=${limit}`);
|
|
65
|
-
return trendingMoviesSchema.parse(raw).map((e) => toTrending(e.watchers, e.movie));
|
|
66
|
-
}
|
|
67
|
-
async trendingShows(limit) {
|
|
68
|
-
const raw = await this.get(`/shows/trending?extended=full&limit=${limit}`);
|
|
69
|
-
return trendingShowsSchema.parse(raw).map((e) => toTrending(e.watchers, e.show));
|
|
70
|
-
}
|
|
71
|
-
async searchMovies(query, limit) {
|
|
72
|
-
const raw = await this.get(`/search/movie?query=${encodeURIComponent(query)}&extended=full&limit=${limit}`);
|
|
73
|
-
return movieResultsSchema
|
|
74
|
-
.parse(raw)
|
|
75
|
-
.flatMap((r) => (r.movie === undefined ? [] : [toMedia(r.movie)]));
|
|
76
|
-
}
|
|
77
|
-
async searchShows(query, limit) {
|
|
78
|
-
const raw = await this.get(`/search/show?query=${encodeURIComponent(query)}&extended=full&limit=${limit}`);
|
|
79
|
-
return showResultsSchema.parse(raw).flatMap((r) => (r.show === undefined ? [] : [toMedia(r.show)]));
|
|
80
|
-
}
|
|
81
|
-
async findShowTvdbIdByImdb(imdbId) {
|
|
82
|
-
const raw = await this.get(`/search/imdb/${imdbId}?type=show`);
|
|
83
|
-
const results = showSearchSchema.parse(raw);
|
|
84
|
-
for (const result of results) {
|
|
85
|
-
const tvdb = result.show?.ids.tvdb;
|
|
86
|
-
if (typeof tvdb === 'number')
|
|
87
|
-
return tvdb;
|
|
88
|
-
}
|
|
89
|
-
return null;
|
|
90
|
-
}
|
|
91
|
-
}
|