fix(rt-api): use rotten-tomatoes 2.0 search api for movies

pull/2105/head
sct 3 years ago
parent bb09f8eaf7
commit a11bb49663
No known key found for this signature in database
GPG Key ID: 845B4E54293A6F5E

@ -1,39 +1,28 @@
import cacheManager from '../lib/cache'; import cacheManager from '../lib/cache';
import ExternalAPI from './externalapi'; import ExternalAPI from './externalapi';
interface RTMovieOldSearchResult { interface RTSearchResult {
id: number; meterClass: 'certified_fresh' | 'fresh' | 'rotten';
title: string; meterScore: number;
year: number; url: string;
ratings: {
critics_rating: 'Certified Fresh' | 'Fresh' | 'Rotten';
critics_score: number;
audience_rating: 'Upright' | 'Spilled';
audience_score: number;
};
links: {
self: string;
alternate: string;
};
} }
interface RTTvSearchResult { interface RTTvSearchResult extends RTSearchResult {
title: string; title: string;
meterClass: 'fresh' | 'rotten';
meterScore: number;
url: string;
startYear: number; startYear: number;
endYear: number; endYear: number;
} }
interface RTMovieSearchResult extends RTSearchResult {
interface RTMovieSearchResponse { name: string;
total: number; url: string;
movies: RTMovieOldSearchResult[]; year: number;
} }
interface RTMultiSearchResponse { interface RTMultiSearchResponse {
tvCount: number; tvCount: number;
tvSeries: RTTvSearchResult[]; tvSeries: RTTvSearchResult[];
movieCount: number;
movies: RTMovieSearchResult[];
} }
export interface RTRating { export interface RTRating {
@ -88,19 +77,19 @@ class RottenTomatoes extends ExternalAPI {
year: number year: number
): Promise<RTRating | null> { ): Promise<RTRating | null> {
try { try {
const data = await this.get<RTMovieSearchResponse>('/v1.0/movies', { const data = await this.get<RTMultiSearchResponse>('/v2.0/search/', {
params: { q: name }, params: { q: name, limit: 10 },
}); });
// First, attempt to match exact name and year // First, attempt to match exact name and year
let movie = data.movies.find( let movie = data.movies.find(
(movie) => movie.year === year && movie.title === name (movie) => movie.year === year && movie.name === name
); );
// If we don't find a movie, try to match partial name and year // If we don't find a movie, try to match partial name and year
if (!movie) { if (!movie) {
movie = data.movies.find( movie = data.movies.find(
(movie) => movie.year === year && movie.title.includes(name) (movie) => movie.year === year && movie.name.includes(name)
); );
} }
@ -111,7 +100,7 @@ class RottenTomatoes extends ExternalAPI {
// One last try, try exact name match only // One last try, try exact name match only
if (!movie) { if (!movie) {
movie = data.movies.find((movie) => movie.title === name); movie = data.movies.find((movie) => movie.name === name);
} }
if (!movie) { if (!movie) {
@ -119,12 +108,15 @@ class RottenTomatoes extends ExternalAPI {
} }
return { return {
title: movie.title, title: movie.name,
url: movie.links.alternate, url: movie.url,
criticsRating: movie.ratings.critics_rating, criticsRating:
criticsScore: movie.ratings.critics_score, movie.meterClass === 'certified_fresh'
audienceRating: movie.ratings.audience_rating, ? 'Certified Fresh'
audienceScore: movie.ratings.audience_score, : movie.meterClass === 'fresh'
? 'Fresh'
: 'Rotten',
criticsScore: movie.meterScore,
year: movie.year, year: movie.year,
}; };
} catch (e) { } catch (e) {

Loading…
Cancel
Save