@@ -6,6 +6,7 @@ import { Episode, EpisodeRequest, ExternalId, SearchMovieRequest, SearchTvReques
66
77import { tmdb } from './tmdb-api' ;
88import { ValidationError } from '../helpers/customErrors' ;
9+ import { traceLog } from '../helpers/logging' ;
910import CollectionMetadata , { CollectionMetadataInterface } from '../models/CollectionMetadata' ;
1011import FailedLookups , { FailedLookupsInterface } from '../models/FailedLookups' ;
1112import LocalizeMetadata , { LocalizeMetadataInterface } from '../models/LocalizeMetadata' ;
@@ -106,7 +107,7 @@ export const getSeriesMetadata = async(
106107 title ?: string ;
107108 tmdbID ?: number ;
108109 type ?: string ;
109- year ?: string ;
110+ year ?: string | { $exists : boolean } ;
110111 count ?: number ;
111112 reason ?: string ;
112113
@@ -175,9 +176,7 @@ export const getSeriesMetadata = async(
175176
176177 // Return early for previously-failed lookups
177178 if ( await FailedLookups . findOne ( failedLookupQuery , '_id' , { lean : true } ) . exec ( ) ) {
178- if ( process . env . VERBOSE === 'true' ) {
179- console . trace ( 'Found previously-failed lookup' , failedLookupQuery ) ;
180- }
179+ traceLog ( 'Found previously-failed lookup' , failedLookupQuery ) ;
181180 await FailedLookups . updateOne ( failedLookupQuery , { $inc : { count : 1 } } ) . exec ( ) ;
182181
183182 // Also store a failed result for the title that the client sent
@@ -189,9 +188,7 @@ export const getSeriesMetadata = async(
189188 }
190189
191190 // Return any previous match
192- if ( process . env . VERBOSE === 'true' ) {
193- console . trace ( 'Looking for TV series in db' , parsedTitle ) ;
194- }
191+ traceLog ( 'Looking for TV series in db' , { parsedTitle } ) ;
195192 const seriesMetadata = await SeriesMetadata . findOne ( titleQuery , null , { lean : true } ) . sort ( sortBy ) . exec ( ) ;
196193 if ( seriesMetadata ) {
197194 // Also cache the result for the title that the client sent, if this is an automatic re-attempt with an appended year (see below)
@@ -202,37 +199,31 @@ export const getSeriesMetadata = async(
202199 { returnDocument : 'after' , lean : true } ,
203200 ) . exec ( ) ;
204201 }
205- if ( process . env . VERBOSE === 'true' ) {
206- console . trace ( 'Found TV series' , seriesMetadata ) ;
207- }
202+
203+ traceLog ( 'Found TV series' , seriesMetadata ) ;
208204
209205 return seriesMetadata ;
210206 }
211207
212208 // Start TMDB lookups
213- if ( process . env . VERBOSE === 'true' ) {
214- console . trace ( 'Looking for TV seriesTMDBID on TMDB' , parsedTitle ) ;
215- }
209+ traceLog ( 'Looking for TV seriesTMDBID on TMDB' , { parsedTitle } ) ;
210+
216211 const seriesTMDBID = await getSeriesTMDBIDFromTMDBAPI ( null , parsedTitle , language , yearNumber ) ;
217212 if ( seriesTMDBID ) {
218- if ( process . env . VERBOSE === 'true' ) {
219- console . trace ( 'Found TV seriesTMDBID for parsedTitle' , seriesTMDBID ) ;
220- }
213+ traceLog ( 'Found TV seriesTMDBID for parsedTitle' , { seriesTMDBID } ) ;
214+
221215 // See if we have an existing record for the now-known media.
222216 const existingResult = await SeriesMetadata . findOne ( { tmdbID : seriesTMDBID } , null , { lean : true } ) . exec ( ) ;
223217 if ( existingResult ) {
224- if ( process . env . VERBOSE === 'true' ) {
225- console . trace ( 'Found existingResult for parsedTitle' , existingResult ) ;
226- }
218+ traceLog ( 'Found existingResult for parsedTitle' , existingResult ) ;
219+
227220 return await SeriesMetadata . findOneAndUpdate (
228221 { tmdbID : seriesTMDBID } ,
229222 { $addToSet : { searchMatches : searchMatch } } ,
230223 { returnDocument : 'after' , lean : true } ,
231224 ) . exec ( ) ;
232225 } else {
233- if ( process . env . VERBOSE === 'true' ) {
234- console . trace ( 'No existingResult for parsedTitle' , existingResult ) ;
235- }
226+ traceLog ( 'No existingResult for parsedTitle' , existingResult ) ;
236227 }
237228
238229 // We do not have an existing record for that series, get the full result from the TMDB API
@@ -241,13 +232,12 @@ export const getSeriesMetadata = async(
241232 id : seriesTMDBID ,
242233 } ;
243234
244- if ( process . env . VERBOSE === 'true' ) {
245- console . trace ( 'Looking for series on TMDB' , parsedTitle ) ;
246- }
235+ traceLog ( 'Looking for series on TMDB' , { parsedTitle } ) ;
236+
247237 const tmdbResponse = await tmdb . tvInfo ( seriesRequest ) ;
248- if ( process . env . VERBOSE === 'true' ) {
249- console . trace ( 'Found series on TMDB' , tmdbResponse ) ;
250- }
238+
239+ traceLog ( 'Found series on TMDB' , tmdbResponse ) ;
240+
251241 tmdbData = mapper . parseTMDBAPISeriesResponse ( tmdbResponse ) ;
252242 }
253243 // End TMDB lookups
@@ -402,44 +392,35 @@ export const getFromTMDBAPI = async(movieOrSeriesTitle?: string, language?: stri
402392 const episodeIMDbID = movieOrEpisodeIMDbID ;
403393 let seriesTMDBID : string | number ;
404394 if ( episodeIMDbID ) {
405- if ( process . env . VERBOSE === 'true' ) {
406- console . trace ( 'Looking for an episode with the IMDb ID' , episodeIMDbID ) ;
407- }
395+ traceLog ( 'Looking for an episode with the IMDb ID' , { episodeIMDbID } ) ;
396+
408397 const findResult = await tmdb . find ( { id : episodeIMDbID , external_source : ExternalId . ImdbId } ) ;
409398 if ( findResult ?. tv_episode_results && findResult ?. tv_episode_results [ 0 ] ) {
410399 const tvEpisodeResult = findResult . tv_episode_results [ 0 ] as SimpleEpisode ;
411400 seriesTMDBID = tvEpisodeResult ?. show_id ;
412- if ( process . env . VERBOSE === 'true' ) {
413- console . trace ( 'Found tvEpisodeResult and seriesTMDBID' , tvEpisodeResult , seriesTMDBID ) ;
414- }
401+
402+ traceLog ( 'Found tvEpisodeResult and seriesTMDBID' , { tvEpisodeResult, seriesTMDBID } ) ;
415403 } else {
416- if ( process . env . VERBOSE === 'true' ) {
417- console . trace ( 'Did not find an episode with the IMDb ID' , episodeIMDbID ) ;
418- }
404+ traceLog ( 'Did not find an episode with the IMDb ID' , { episodeIMDbID } ) ;
419405 }
420406 } else {
421- if ( process . env . VERBOSE === 'true' ) {
422- console . trace ( 'Looking for seriesTMDBID with' , movieOrSeriesTitle , language , yearString ) ;
423- }
407+ traceLog ( 'Looking for seriesTMDBID with' , { movieOrSeriesTitle, language, yearString } ) ;
408+
424409 const seriesMetadata = await getSeriesMetadata ( null , movieOrSeriesTitle , language , yearString ) ;
425410 seriesTMDBID = seriesMetadata ?. tmdbID ;
426411 }
427412
428413 if ( ! seriesTMDBID ) {
429- if ( process . env . VERBOSE === 'true' ) {
430- console . trace ( 'Did not find seriesTMDBID with' , movieOrSeriesTitle , language , yearString ) ;
431- }
414+ traceLog ( 'Did not find seriesTMDBID with' , { movieOrSeriesTitle, language, yearString } ) ;
415+
432416 return null ;
433417 } else {
434- if ( process . env . VERBOSE === 'true' ) {
435- console . trace ( 'Found seriesTMDBID ' + seriesTMDBID + 'with' , movieOrSeriesTitle , language , yearString ) ;
436- }
418+ traceLog ( 'Found seriesTMDBID ' + seriesTMDBID + 'with' , { movieOrSeriesTitle, language, yearString } ) ;
437419 }
438420
439421 for ( let i = 0 ; i < episodeNumbers . length ; i ++ ) {
440- if ( process . env . VERBOSE === 'true' ) {
441- console . trace ( 'Looking for episode number ' + episodeNumbers [ i ] + 'with' , seriesTMDBID , seasonNumber ) ;
442- }
422+ traceLog ( 'Looking for episode number ' + episodeNumbers [ i ] + 'with' , { seriesTMDBID, seasonNumber } ) ;
423+
443424 const episodeRequest : EpisodeRequest = {
444425 append_to_response : 'images,external_ids,credits' ,
445426 episode_number : episodeNumbers [ i ] ,
@@ -461,16 +442,12 @@ export const getFromTMDBAPI = async(movieOrSeriesTitle?: string, language?: stri
461442 if ( tmdbSeriesData ?. imdb_id ) {
462443 metadata . seriesIMDbID = tmdbSeriesData . imdb_id ;
463444 }
464- if ( process . env . VERBOSE === 'true' ) {
465- console . trace ( 'Found metadata' , metadata ) ;
466- }
445+ traceLog ( 'Found metadata' , metadata ) ;
467446 } else {
468447 metadata . title = metadata . title ? metadata . title + ' & ' + tmdbData . name : tmdbData . name ;
469448 }
470449 } else {
471- if ( process . env . VERBOSE === 'true' ) {
472- console . trace ( 'Did not find tmdbData from' , episodeRequest ) ;
473- }
450+ traceLog ( 'Did not find tmdbData from' , episodeRequest ) ;
474451 }
475452 }
476453 } else {
0 commit comments