From 258bb93be2acc2ca32eaaefb617a5c326c5943ba Mon Sep 17 00:00:00 2001 From: sct Date: Fri, 11 Sep 2020 11:00:21 +0900 Subject: [PATCH] feat(api): discover endpoint for movie/tv (#73) --- server/overseerr-api.yml | 74 +++++++++++++++++++++++++++++++++++++++ server/routes/discover.ts | 33 +++++++++++++++++ server/routes/index.ts | 2 ++ server/routes/search.ts | 8 +++-- 4 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 server/routes/discover.ts diff --git a/server/overseerr-api.yml b/server/overseerr-api.yml index 3982ad21b..c5618dea6 100644 --- a/server/overseerr-api.yml +++ b/server/overseerr-api.yml @@ -732,6 +732,12 @@ paths: schema: type: string example: 'Mulan' + - in: query + name: page + schema: + type: number + example: 1 + default: 1 responses: '200': description: Results @@ -756,6 +762,74 @@ paths: - $ref: '#/components/schemas/MovieResult' - $ref: '#/components/schemas/TvResult' - $ref: '#/components/schemas/PersonResult' + /discover/movies: + get: + summary: Discover movies + description: Returns a list of movies in JSON format + tags: + - search + parameters: + - in: query + name: page + schema: + type: number + example: 1 + default: 1 + responses: + '200': + description: Results + content: + application/json: + schema: + type: object + properties: + page: + type: number + example: 1 + totalPages: + type: number + example: 20 + totalResults: + type: number + example: 200 + results: + type: array + items: + $ref: '#/components/schemas/MovieResult' + /discover/tv: + get: + summary: Discover TV shows + description: Returns a list of tv shows in JSON format + tags: + - search + parameters: + - in: query + name: page + schema: + type: number + example: 1 + default: 1 + responses: + '200': + description: Results + content: + application/json: + schema: + type: object + properties: + page: + type: number + example: 1 + totalPages: + type: number + example: 20 + totalResults: + type: number + example: 200 + results: + type: array + items: + $ref: '#/components/schemas/TvResult' security: - cookieAuth: [] diff --git a/server/routes/discover.ts b/server/routes/discover.ts new file mode 100644 index 000000000..ab69273bf --- /dev/null +++ b/server/routes/discover.ts @@ -0,0 +1,33 @@ +import { Router } from 'express'; +import TheMovieDb from '../api/themoviedb'; +import { mapMovieResult, mapTvResult } from '../models/Search'; + +const discoverRoutes = Router(); + +discoverRoutes.get('/movies', async (req, res) => { + const tmdb = new TheMovieDb(); + + const data = await tmdb.getDiscoverMovies({ page: Number(req.query.page) }); + + return res.status(200).json({ + page: data.page, + totalPages: data.total_pages, + totalResults: data.total_results, + results: data.results.map(mapMovieResult), + }); +}); + +discoverRoutes.get('/tv', async (req, res) => { + const tmdb = new TheMovieDb(); + + const data = await tmdb.getDiscoverTv({ page: Number(req.query.page) }); + + return res.status(200).json({ + page: data.page, + totalPages: data.total_pages, + totalResults: data.total_results, + results: data.results.map(mapTvResult), + }); +}); + +export default discoverRoutes; diff --git a/server/routes/index.ts b/server/routes/index.ts index 8c5f088ff..2e603ccb3 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -6,6 +6,7 @@ import settingsRoutes from './settings'; import { Permission } from '../lib/permissions'; import { getSettings } from '../lib/settings'; import searchRoutes from './search'; +import discoverRoutes from './discover'; const router = Router(); @@ -17,6 +18,7 @@ router.use( settingsRoutes ); router.use('/search', isAuthenticated(), searchRoutes); +router.use('/discover', isAuthenticated(), discoverRoutes); router.use('/auth', authRoutes); router.get('/settings/public', (_req, res) => { diff --git a/server/routes/search.ts b/server/routes/search.ts index 416a3a083..e00768210 100644 --- a/server/routes/search.ts +++ b/server/routes/search.ts @@ -7,9 +7,11 @@ const searchRoutes = Router(); searchRoutes.get('/', async (req, res) => { const tmdb = new TheMovieDb(); - const results = await tmdb.searchMulti({ query: req.query.query as string }); - const megaResults = mapSearchResults(results.results); - console.log(megaResults); + const results = await tmdb.searchMulti({ + query: req.query.query as string, + page: Number(req.query.page), + }); + return res.status(200).json({ page: results.page, totalPages: results.total_pages,