Skip to content

Commit

Permalink
refactor imdbTask code
Browse files Browse the repository at this point in the history
  • Loading branch information
Asing1001 committed Apr 14, 2017
1 parent d07e527 commit 8a01a12
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
13 changes: 4 additions & 9 deletions src/task/imdbTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Movie from "../models/movie";

export async function updateImdbInfo() {
let movieInfos = await getNewImdbInfos();
removeEmptyInfos(movieInfos);
logResult(movieInfos);
return updateYahooMovies(movieInfos);
}
Expand Down Expand Up @@ -33,13 +32,6 @@ function filterNeedCrawlMovie({ englishTitle, releaseDate, imdbLastCrawlTime }:
return shouldCrawl;
}

function removeEmptyInfos(movieInfos: Movie[]){
movieInfos.forEach(info=>{
!info.imdbID && delete info.imdbID;
!info.imdbRating && delete info.imdbRating;
})
}

function logResult(movieInfos: Movie[]) {
const foundMovies = movieInfos.filter(movie => movie.imdbID);
const notfoundMovieIds = movieInfos.filter(movie => !movie.imdbID).map(movie => movie.yahooId);
Expand All @@ -48,5 +40,8 @@ function logResult(movieInfos: Movie[]) {
}

function updateYahooMovies(movieInfos: Movie[]) {
return movieInfos.map(imdbInfo => db.updateDocument({ yahooId: imdbInfo.yahooId }, imdbInfo, "yahooMovies"))
return movieInfos.map(({ yahooId, imdbID, imdbRating, imdbLastCrawlTime }) => {
const newInfo = imdbID ? { imdbID, imdbRating, imdbLastCrawlTime } : { imdbLastCrawlTime }
return db.updateDocument({ yahooId }, newInfo, "yahooMovies")
})
}
2 changes: 1 addition & 1 deletion src/test/imdbTask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('imdbTask', () => {
await updateImdbInfo();
sandbox.assert.calledWith(stubUpdateDocument,
{ yahooId: yahooMovie.yahooId },
{ yahooId: yahooMovie.yahooId, imdbLastCrawlTime: moment().format('YYYY-MM-DDTHH') });
{ imdbLastCrawlTime: moment().format('YYYY-MM-DDTHH') });
});
});
});

0 comments on commit 8a01a12

Please sign in to comment.