feat(tv): tv seasons

tv seasons
This commit is contained in:
Nicolai Van der Storm
2022-06-01 14:48:05 +02:00
parent 3e7d64eb47
commit c117b37cd9
5 changed files with 109 additions and 1 deletions

View File

@@ -18,6 +18,7 @@ import {
TmdbSearchTvResponse,
TmdbSeasonWithEpisodes,
TmdbTvDetails,
TmdbTvSeasonResult,
TmdbUpcomingMoviesResponse,
} from './interfaces';
@@ -271,6 +272,32 @@ class TheMovieDb extends ExternalAPI {
}
};
public getTvSeasons = async ({
tvId,
page = 1,
language = 'en',
}: {
tvId: number;
page: number;
language?: string;
}): Promise<TmdbTvSeasonResult[]> => {
try {
const data = await this.get<TmdbTvDetails>(
`/tv/${tvId}`,
{
params: {
page,
language,
},
},
43200
);
return data.seasons;
} catch (e) {
throw new Error(`[TMDb] Failed to fetch TV show seasons: ${e.message}`);
}
};
public getTvSeason = async ({
tvId,
seasonNumber,

View File

@@ -48,6 +48,10 @@ interface Season {
seasonNumber: number;
}
export interface TvSeasons {
seasons: Season[];
}
export interface SeasonWithEpisodes extends Season {
episodes: Episode[];
externalIds: ExternalIds;
@@ -223,3 +227,7 @@ export const mapTvDetails = (
mediaInfo: media,
watchProviders: mapWatchProviders(show['watch/providers']?.results ?? {}),
});
export const mapTvSeasons = (seasons: TmdbTvSeasonResult[]): TvSeasons => ({
seasons: seasons.map(mapSeasonResult),
});

View File

@@ -5,7 +5,11 @@ import { MediaType } from '../constants/media';
import Media from '../entity/Media';
import logger from '../logger';
import { mapTvResult } from '../models/Search';
import { mapSeasonWithEpisodes, mapTvDetails } from '../models/Tv';
import {
mapSeasonWithEpisodes,
mapTvDetails,
mapTvSeasons,
} from '../models/Tv';
const tvRoutes = Router();
@@ -33,6 +37,29 @@ tvRoutes.get('/:id', async (req, res, next) => {
}
});
tvRoutes.get('/:id/seasons', async (req, res, next) => {
const tmdb = new TheMovieDb();
try {
const seasons = await tmdb.getTvSeasons({
tvId: Number(req.params.id),
page: Number(req.query.page),
language: req.locale ?? (req.query.language as string),
});
return res.status(200).json(mapTvSeasons(seasons));
} catch (e) {
logger.debug('Something went wrong retrieving seasons', {
label: 'API',
errorMessage: e.message,
tvId: req.params.id,
});
return next({
status: 500,
message: 'Unable to retrieve seasons.',
});
}
});
tvRoutes.get('/:id/season/:seasonNumber', async (req, res, next) => {
const tmdb = new TheMovieDb();