Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 33 additions & 24 deletions app/server/src/sources/tmdb/tmdb.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,6 @@ export class TmdbService {
if (category === 'movie' && !this.hasFilters(filters)) {
return section === 'recent' ? this.recentMovies(page) : this.popularMovies(page);
}
if (
category === 'tv' &&
section === 'popular' &&
!this.hasFilters(filters)
) {
return this.normalizeTvList(
await this.request<TmdbTvSearchResponse>(
'/tv/popular',
{ page: String(page) },
),
);
}
if (category === 'movie') {
return this.normalizeMovieList(
await this.request<TmdbSearchResponse>(
Expand All @@ -183,19 +171,25 @@ export class TmdbService {
);
}
if (category === 'tv') {
return this.normalizeTvList(
await this.request<TmdbTvSearchResponse>(
'/discover/tv',
this.discoverFilters(
category,
section,
page,
filters,
'first_air_date',
this.requiredGenreId(tvGenres, filters.genre),
),
const response = await this.request<TmdbTvSearchResponse>(
'/discover/tv',
this.discoverFilters(
category,
section,
page,
filters,
'first_air_date',
this.requiredGenreId(tvGenres, filters.genre),
),
);
return this.normalizeTvList({
...response,
results: response.results.filter(
(show) =>
!['zh', 'cn'].includes(show.original_language) &&
!show.origin_country?.includes('CN'),
),
});
}
if (category === 'anime') {
const movieGenre = this.genreId(movieGenres, filters.genre);
Expand Down Expand Up @@ -647,7 +641,9 @@ export class TmdbService {
filters.sort === 'first_air_date.desc'
? `${dateField}.desc`
: filters.sort ??
(section === 'recent' ? `${dateField}.desc` : 'popularity.desc'),
(section === 'recent' && category !== 'tv'
? `${dateField}.desc`
: 'popularity.desc'),
};
if (genreId) {
parameters.with_genres = genreId;
Expand All @@ -674,6 +670,19 @@ export class TmdbService {
pilot: '5',
}[filters.status.toLowerCase()] ?? filters.status;
}
if (category === 'tv') {
if (!filters.genre) {
parameters.with_type = '2|4';
}
if (!['planned', 'production', 'pilot'].includes(filters.status?.toLowerCase() ?? '')) {
parameters['vote_count.gte'] = section === 'recent' ? '10' : '100';
}
if (section === 'recent' && !filters.year) {
parameters['first_air_date.gte'] = new Date(Date.now() - 90 * 86_400_000)
.toISOString()
.slice(0, 10);
}
}
return parameters;
}

Expand Down
78 changes: 76 additions & 2 deletions app/server/test/tmdb.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('TmdbService', () => {
expect((fetchMock.mock.calls[0] as [URL])[0].pathname).toBe(`/3${path}`);
});

it('orders recent TV and anime by their release fields', async () => {
it('orders recent anime by release date and recent TV by popularity', async () => {
const fetchMock = vi.fn().mockImplementation((input: URL) => {
const isMovie = input.pathname.endsWith('/discover/movie');
return Promise.resolve(
Expand Down Expand Up @@ -158,14 +158,88 @@ describe('TmdbService', () => {
const movieUrl = urls[1]!;
const animeTvUrl = urls[2]!;
expect(tvUrl.pathname).toBe('/3/discover/tv');
expect(tvUrl.searchParams.get('sort_by')).toBe('first_air_date.desc');
expect(tvUrl.searchParams.get('sort_by')).toBe('popularity.desc');
expect(tvUrl.searchParams.get('first_air_date.gte')).toBeTruthy();
expect(tvUrl.searchParams.get('first_air_date.lte')).toBeTruthy();
expect(tvUrl.searchParams.get('vote_count.gte')).toBe('10');
expect(movieUrl.searchParams.get('sort_by')).toBe('primary_release_date.desc');
expect(movieUrl.searchParams.get('primary_release_date.lte')).toBeTruthy();
expect(animeTvUrl.searchParams.get('sort_by')).toBe('first_air_date.desc');
expect(animeTvUrl.searchParams.get('first_air_date.lte')).toBeTruthy();
});

it('browses scripted TV above a vote floor without Chinese-origin titles', async () => {
const show = (name: string, original_language: string, origin_country: string[]) => ({
id: name.length,
name,
original_name: name,
overview: '',
original_language,
origin_country,
first_air_date: '2026-01-01',
});
const response = {
page: 1,
total_pages: 1,
total_results: 4,
results: [
show('Kept', 'en', ['US']),
show('Mandarin', 'zh', ['TW']),
show('Cantonese', 'cn', ['HK']),
show('Co-production', 'ko', ['KR', 'CN']),
],
};
const fetchMock = vi.fn().mockImplementation(() =>
Promise.resolve(
new Response(JSON.stringify(response), {
status: 200,
headers: { 'Content-Type': 'application/json' },
}),
),
);
vi.stubGlobal('fetch', fetchMock);
const service = new TmdbService(
new ConfigService({ TMDB_READ_ACCESS_TOKEN: 'server-token' }),
new ConnectorHttpService(),
);

const popular = await service.browse('tv', 'popular', 1, {});
const searched = await service.search('tv', 'drama', 1, {});

expect(popular.results.map(({ title }) => title)).toEqual(['Kept']);
expect(searched.results).toHaveLength(4);
const [url] = fetchMock.mock.calls[0] as [URL];
expect(url.pathname).toBe('/3/discover/tv');
expect(url.searchParams.get('sort_by')).toBe('popularity.desc');
expect(url.searchParams.get('with_type')).toBe('2|4');
expect(url.searchParams.get('vote_count.gte')).toBe('100');
expect(url.searchParams.get('first_air_date.gte')).toBeNull();
});

it('keeps chosen TV genres and unaired statuses reachable', async () => {
const fetchMock = vi.fn().mockImplementation(() =>
Promise.resolve(
new Response(
JSON.stringify({ page: 1, total_pages: 1, total_results: 0, results: [] }),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
),
),
);
vi.stubGlobal('fetch', fetchMock);
const service = new TmdbService(
new ConfigService({ TMDB_READ_ACCESS_TOKEN: 'server-token' }),
new ConnectorHttpService(),
);

await service.browse('tv', 'popular', 1, { genre: 'Reality', status: 'planned' });

const [url] = fetchMock.mock.calls[0] as [URL];
expect(url.searchParams.get('with_genres')).toBe('10764');
expect(url.searchParams.get('with_status')).toBe('1');
expect(url.searchParams.get('with_type')).toBeNull();
expect(url.searchParams.get('vote_count.gte')).toBeNull();
});

it('loads a normalized movie detail by external ID', async () => {
const fetchMock = vi.fn().mockResolvedValue(
new Response(
Expand Down
Loading