-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
172 lines (153 loc) · 4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const Promise = require('bluebird')
const moment = require('moment')
const _ = require('lodash')
const tmdb = require('./lib/tmdb')
const fsCache = require('./lib/fs_cache')
const metacritic = require('./lib/metacritic')
const omdb = require('./lib/omdb')
const imdb = require('./lib/imdb')
const getTmdbDetails = function (movies) {
return Promise
.resolve(movies)
.mapSeries(function (movie) {
return tmdb.searchMovie(movie.title)
.then(function (tmdbMovie) {
tmdbMovie.metacritic_score = movie.score
return tmdbMovie
})
})
}
const getImdbId = function (tmdbId) {
return fsCache.wrap(tmdbId, function () {
return tmdb.getMovie(tmdbId)
.then(function (movie) {
return { imdbId: movie.imdb_id }
})
})
}
const associateImdbIds = function (movies) {
return Promise
.resolve(movies)
.mapSeries(function (movie) {
// we then need to map an imdb_id to each and every movie
return getImdbId(movie.id)
.then(function ({ imdbId }) {
movie.imdb_id = imdbId
movie.tmdb_id = movie.id
return movie
})
})
}
const getImdbRatings = function (movies) {
return Promise
.resolve(movies)
.map(function (movie) {
if (movie.imdb_rating && movie.imdb_rating !== 'N/A') {
return movie
}
return imdb(movie.imdb_id)
.then(function (ratings) {
return _.assign(movie, ratings)
})
}, {
concurrency: 1
})
}
const getOmdbRatings = function (movies) {
return Promise
.resolve(movies)
.mapSeries(function (movie) {
return omdb(movie.imdb_id)
.then(function (ratings) {
return _.defaults(movie, ratings)
})
})
}
const uniqueMovies = function (movies) {
return _.uniqBy(movies, 'imdb_id')
}
const sanatizeForResponse = function (movies) {
return Promise
.resolve(movies)
.map(function (movie) {
return _.pick(movie, ['title', 'tmdb_id', 'imdb_id', 'poster_url'])
})
}
const filterByMinValue = function (key, value = 0) {
return function (movies) {
return _.filter(movies, function (movie) {
return _.get(movie, key, 0) >= value
})
}
}
const filterByMaxValue = function (key, value = 0) {
return function (movies) {
return _.filter(movies, function (movie) {
return _.get(movie, key, 0) <= value
})
}
}
const calculateMovieAge = function (movies) {
return _.map(movies, function (movie) {
movie.age = moment().diff(movie.release_date, 'days')
return movie
})
}
const logger = function (movies) {
console.table(movies, [
'id',
'imdb_id',
'tmdb_id',
'title',
'release_date',
'age',
'metacritic_score',
'imdb_rating',
'imdb_votes',
'rt_score',
'popularity',
'vote_average',
'vote_count'
])
}
module.exports = (function () {
//
// Class builder functions to help cache content but be
// able to filter after the fact with options
//
let allMovies = null
const getMovies = function () {
if (allMovies) {
return allMovies
}
return Promise
.resolve(metacritic())
.then(getTmdbDetails)
.then(filterByMinValue('vote_count', 10))
.then(filterByMinValue('popularity', 30))
.then(calculateMovieAge)
.then(filterByMinValue('age', 21))
.then(filterByMaxValue('age', 365))
.then(associateImdbIds)
.then(uniqueMovies)
.then(getOmdbRatings)
.then(getImdbRatings)
.tap(logger)
.tap(function (movies) {
allMovies = movies
})
}
const ListBuilder = function () {}
ListBuilder.prototype.filter = function (opts = {}) {
return Promise
.resolve(getMovies())
.then(filterByMinValue('metacritic_score', opts.min_metacritic_score))
.then(filterByMinValue('rt_score', opts.min_rt_score))
.then(filterByMinValue('imdb_rating', opts.min_imdb_rating))
.then(sanatizeForResponse)
}
ListBuilder.prototype.dump = function () {
return allMovies
}
return ListBuilder
})()