-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
284 lines (247 loc) · 7.01 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
const Promise = require('bluebird')
const moment = require('moment')
const _ = require('lodash')
const tmdb = require('./lib/tmdb')
const omdb = require('./lib/omdb')
const imdb = require('./lib/imdb')
const metacritic = require('./lib/metacritic')
const anthropic = require('./lib/anthropic')
const getTmdb = function (tmdbId) {
return tmdb.getMovie(tmdbId)
}
const getTmdbDetails = function (movies) {
return Promise
.resolve(movies)
.mapSeries(function (movie) {
return getTmdb(movie.id)
.then(function (tmdbMovie) {
return _.assign(movie, {
tmdb_id: tmdbMovie.id,
imdb_id: tmdbMovie.imdb_id,
budget: tmdbMovie.budget === 0 ? null : tmdbMovie.budget,
revenue: tmdbMovie.revenue === 0 ? null : tmdbMovie.revenue,
top_actors: _.chain(tmdbMovie.credits.cast)
.take(3)
.map('name')
.value(),
director: tmdbMovie.credits.crew.find(c => c.job === 'Director')?.name,
production_companies: _.map(tmdbMovie.production_companies, 'name'),
genres: _.chain(tmdbMovie.genres)
.map('name')
.map(name => _.snakeCase(name).toLowerCase())
.value()
})
})
})
}
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 normalizeTitle = function (title) {
return title.replace(/[^\w]/gi, '').toLowerCase()
}
const getMetacriticRatings = async function (movies) {
const metacriticMovies = await metacritic()
const mappedMovies = _.chain(metacriticMovies)
.keyBy(m => normalizeTitle(m.title))
.mapValues('score')
.value()
return movies.map(function (movie) {
return _.assign(movie, {
metacritic_score: mappedMovies[normalizeTitle(movie.title)]
})
})
}
const evaluateMovies = async function (movies) {
const system = `
You are a movie critic that is given a list of movies released in the last 4 months. Your goal is to suggest and sort order the most popular movies.
You will be given a list of movies with the following details:
- Title
- Production Companies
- Release Date
- Genres
- Budget
- Revenue
- Metacritic Score (0-100)
- Rotten Tomatoes Score (0-100)
- IMDb Rating (0-10)
- IMDb Vote Count
- TMDB Score (0-10)
- TMDB Vote Count
- Top 3 actors in the movie
- Director
- Writer
When evaluating the popularity of a movie, consider:
- The budget of the movie and and how much revenue it made. Don't consider ROI, just consider how large the spend or revenue is.
- The number of votes the movie received and the rating of the movie. Be sure to consider the number of votes so that one vote does not skew the results.
- The production companies of the movie and the quality of the movies they have produced, and how well known the companies are.
- The actors & directors in the movie and how well known they are.
A null value means that the data could not be found or isn't publicly available.
Explain your reasoning first, then return the IDs of the most popular movies, in sorted order, in a JSON array. Comments in the JSON is invalid JSON.
Include, at most, 15 movies.
Your response should look similar to:
\`\`\`json
[
123,
456,
789
]
\`\`\`
`
const moviesData = movies.map(function (movie) {
return _.pick(movie, [
'id',
'title',
'production_companies',
'release_date',
'genres',
'budget',
'revenue',
'metacritic_score',
'imdb_rating',
'imdb_votes',
'rt_score',
'vote_average',
'vote_count',
'top_actors',
'director'
])
})
const response = await anthropic.prompt(system, JSON.stringify(moviesData))
const suggestedMovies = _.map(response, id => movies.find(movie => movie.id === id))
return suggestedMovies
}
const sanatizeForResponse = function (movies) {
return Promise
.resolve(movies)
.map(function (movie) {
return _.pick(movie, [
'title',
'tmdb_id',
'imdb_id',
'poster_url',
'genres'
])
})
}
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 rejectArrayValues = function (key, values) {
return function (movies) {
if (_.isNil(values)) {
return movies
}
return _.reject(movies, function (movie) {
return values.some(value => _.get(movie, key, []).includes(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',
'genres',
'budget',
'revenue',
'production_companies',
'top_actors',
'director',
'writer'
])
}
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(tmdb.getMovies())
.then(calculateMovieAge)
.then(filterByMaxValue('age', 120))
.then(filterByMinValue('age', 0))
.then(getTmdbDetails)
.then(getMetacriticRatings)
.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(rejectArrayValues('genres', opts.exclude_genres))
.then(sanatizeForResponse)
}
ListBuilder.prototype.evaluate = function () {
return Promise
.resolve(getMovies())
.then(evaluateMovies)
.then(sanatizeForResponse)
}
ListBuilder.prototype.dump = function () {
return allMovies
}
return ListBuilder
})()