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
17 changes: 9 additions & 8 deletions src/controllers/deprecated/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,17 @@ export const getVideo = async(ctx): Promise<MediaMetadataInterface> => {

const existingFailedResult = await FailedLookups.findOne({ $or: failedQuery }, null, { lean: true }).exec();
if (existingFailedResult) {
// we have an existing failure record, so increment it, and throw not found error
await FailedLookups.updateOne({ _id: existingFailedResult._id }, { $inc: { count: 1 } }).exec();
throw new MediaNotFoundError();
}

// the database does not have a record of this file, so begin search for metadata on external apis.
// the database does not have a record of this file, so begin search for metadata on TMDB.

const failedLookupQuery = { episode, imdbID, season, title, year };

if (!title && !imdbIdToSearch) {
// The APIs below require either a title or IMDb ID, so return if we don't have one
await FailedLookups.updateOne(failedLookupQuery, { $inc: { count: 1 } }, { upsert: true, setDefaultsOnInsert: true }).exec();
// TMDB requires either a title or IMDb ID, so return if we don't have one
const reason = 'getVideo (deprecated) failed because there is no title or imdbId';
await FailedLookups.updateOne(failedLookupQuery, { $inc: { count: 1 }, reason }, { upsert: true, setDefaultsOnInsert: true }).exec();
throw new MediaNotFoundError();
}

Expand Down Expand Up @@ -214,7 +213,8 @@ export const getVideo = async(ctx): Promise<MediaMetadataInterface> => {
// End TMDB lookups

if (!tmdbData || _.isEmpty(tmdbData)) {
await FailedLookups.updateOne(failedLookupQuery, { $inc: { count: 1 } }, { upsert: true, setDefaultsOnInsert: true }).exec();
const reason = `getVideo (deprecated) failed because no data was found on TMDB for ${failedLookupQuery}`;
await FailedLookups.updateOne(failedLookupQuery, { $inc: { count: 1 }, reason }, { upsert: true, setDefaultsOnInsert: true }).exec();
throw new MediaNotFoundError();
}

Expand All @@ -235,8 +235,9 @@ export const getVideo = async(ctx): Promise<MediaMetadataInterface> => {
leanMeta = await deprecatedExternalAPIHelper.addPosterFromImages(leanMeta);
return ctx.body = leanMeta;
} catch (e) {
console.error(e,tmdbData);
await FailedLookups.updateOne(failedLookupQuery, { $inc: { count: 1 } }, { upsert: true, setDefaultsOnInsert: true }).exec();
console.error(e, tmdbData);
const reason = `getVideo (deprecated) failed because an error occurred: ${e}`;
await FailedLookups.updateOne(failedLookupQuery, { $inc: { count: 1 }, reason }, { upsert: true, setDefaultsOnInsert: true }).exec();
throw new MediaNotFoundError();
}
};
5 changes: 1 addition & 4 deletions src/models/FailedLookups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ const FailedLookupsSchema = new Schema({
year: { type: String },
failedValidation: { type: Boolean, default: false },
type: { type: String },
reason: {
select: false,
type: String,
},
reason: { type: String },
createdAt: {
default: Date.now,
expires: THIRTY_DAYS_IN_SECONDS,
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/media-video-movie.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ describe('get by all', () => {
expect(error.message).toEqual('Request failed with status code 404');
expect(await FailedLookupsModel.countDocuments()).toEqual(1);

const failedResult = await FailedLookupsModel.findOne();
expect(failedResult.reason).toEqual('getVideoV2 got no tmdb data for areallylongtitlethatsurelywontmatchanymoviename, undefined, undefined, null, NaN, null')

try {
await axios.get(`${appUrl}/api/media/video/v2?title=areallylongtitlethatsurelywontmatchanymoviename`);
} catch (e) {
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/media-video-tv-episode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ describe('get by all', () => {
expect(error.message).toEqual('Request failed with status code 404');
expect(await FailedLookupsModel.countDocuments()).toEqual(1);

const failedResult = await FailedLookupsModel.findOne();
expect(failedResult.reason).toEqual('getVideoV2 got no tmdb data for Lost, undefined, undefined, null, 999, 999')

try {
await axios.get(`${appUrl}/api/media/video/v2?title=${EPISODE_LOST.seriesTitle}&season=999&episode=999`);
} catch (e) {
Expand Down
Loading