fix(requests): handle when tvdbid is null (#657)

Co-authored-by: sct <sctsnipe@gmail.com>
This commit is contained in:
Jakob Ankarhem
2021-01-22 02:49:17 +01:00
committed by GitHub
parent a3fe4e6321
commit 2da0da826a
14 changed files with 508 additions and 94 deletions

View File

@@ -94,6 +94,28 @@ class SonarrAPI {
});
}
public async getSeriesByTitle(title: string): Promise<SonarrSeries[]> {
try {
const response = await this.axios.get<SonarrSeries[]>('/series/lookup', {
params: {
term: title,
},
});
if (!response.data[0]) {
throw new Error('No series found');
}
return response.data;
} catch (e) {
logger.error('Error retrieving series by series title', {
label: 'Sonarr API',
message: e.message,
});
throw new Error('No series found');
}
}
public async getSeriesByTvdbId(id: number): Promise<SonarrSeries> {
try {
const response = await this.axios.get<SonarrSeries[]>('/series/lookup', {

View File

@@ -427,9 +427,12 @@ export class MediaRequest {
});
logger.info('Sent request to Radarr', { label: 'Media Request' });
} catch (e) {
throw new Error(
`[MediaRequest] Request failed to send to radarr: ${e.message}`
);
const errorMessage = `Request failed to send to radarr: ${e.message}`;
logger.error('Request failed to send to Radarr', {
label: 'Media Request',
errorMessage,
});
throw new Error(errorMessage);
}
}
}
@@ -501,8 +504,10 @@ export class MediaRequest {
}:${sonarrSettings.port}${sonarrSettings.baseUrl ?? ''}/api`,
});
const series = await tmdb.getTvShow({ tvId: media.tmdbId });
const tvdbId = series.external_ids.tvdb_id ?? media.tvdbId;
if (!series.external_ids.tvdb_id) {
if (!tvdbId) {
this.handleRemoveParentUpdate();
throw new Error('Series was missing tvdb id');
}
@@ -550,7 +555,7 @@ export class MediaRequest {
profileId: qualityProfile,
rootFolderPath: rootFolder,
title: series.name,
tvdbid: series.external_ids.tvdb_id,
tvdbid: tvdbId,
seasons: this.seasons.map((season) => season.seasonNumber),
seasonFolder: sonarrSettings.enableSeasonFolders,
seriesType,
@@ -590,9 +595,12 @@ export class MediaRequest {
});
logger.info('Sent request to Sonarr', { label: 'Media Request' });
} catch (e) {
throw new Error(
`[MediaRequest] Request failed to send to sonarr: ${e.message}`
);
const errorMessage = `Request failed to send to sonarr: ${e.message}`;
logger.error('Request failed to send to Sonarr', {
label: 'Media Request',
errorMessage,
});
throw new Error(errorMessage);
}
}
}

View File

@@ -109,7 +109,7 @@ requestRoutes.post(
if (!media) {
media = new Media({
tmdbId: tmdbMedia.id,
tvdbId: tmdbMedia.external_ids.tvdb_id,
tvdbId: req.body.tvdbId ?? tmdbMedia.external_ids.tvdb_id,
status: !req.body.is4k ? MediaStatus.PENDING : MediaStatus.UNKNOWN,
status4k: req.body.is4k ? MediaStatus.PENDING : MediaStatus.UNKNOWN,
mediaType: req.body.mediaType,

View File

@@ -6,6 +6,8 @@ import {
ServiceCommonServerWithDetails,
} from '../interfaces/api/serviceInterfaces';
import { getSettings } from '../lib/settings';
import TheMovieDb from '../api/themoviedb';
import logger from '../logger';
const serviceRoutes = Router();
@@ -100,13 +102,13 @@ serviceRoutes.get<{ sonarrId: string }>(
const settings = getSettings();
const sonarrSettings = settings.sonarr.find(
(radarr) => radarr.id === Number(req.params.sonarrId)
(sonarr) => sonarr.id === Number(req.params.sonarrId)
);
if (!sonarrSettings) {
return next({
status: 404,
message: 'Radarr server with provided ID does not exist.',
message: 'Sonarr server with provided ID does not exist.',
});
}
@@ -145,4 +147,52 @@ serviceRoutes.get<{ sonarrId: string }>(
}
);
serviceRoutes.get<{ tmdbId: string }>(
'/sonarr/lookup/:tmdbId',
async (req, res, next) => {
const settings = getSettings();
const tmdb = new TheMovieDb();
const sonarrSettings = settings.sonarr[0];
if (!sonarrSettings) {
logger.error('No sonarr server has been setup', {
label: 'Media Request',
});
return next({
status: 404,
message: 'No sonarr server has been setup',
});
}
const sonarr = new SonarrAPI({
apiKey: sonarrSettings.apiKey,
url: `${sonarrSettings.useSsl ? 'https' : 'http'}://${
sonarrSettings.hostname
}:${sonarrSettings.port}${sonarrSettings.baseUrl ?? ''}/api`,
});
try {
const tv = await tmdb.getTvShow({
tvId: Number(req.params.tmdbId),
language: req.query.language as string,
});
const response = await sonarr.getSeriesByTitle(tv.name);
return res.status(200).json(response);
} catch (e) {
logger.error('Failed to fetch tvdb search results', {
label: 'Media Request',
message: e.message,
});
return next({
status: 500,
message: 'Something went wrong trying to fetch series information',
});
}
}
);
export default serviceRoutes;