-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
270 lines (164 loc) · 5.24 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
/* global define */
'use strict';
( function ( root, cx ) {
if ( typeof define === 'function' && define.amd ) {
// AMD
define( [ 'isomorphic-fetch' ], cx )
} else if ( typeof exports === 'object' ) {
// Node, CommonJS-like
module.exports = cx( require( 'isomorphic-fetch' ) )
} else {
// Browser globals (root is window)
root.movieTrailer = cx( root.fetch )
}
} )( this, fetch => {
function handleErrors( error ) {
const message = ( error && error.message ) ? error.message : error
console.warn( `movie-trailer: ${message}` )
// Throw Error( error.message )
}
function handleFetchErrors( response ) {
if ( !response.ok ) {
throw new Error( response.statusText )
}
return response
}
function toUrl( videoId ) {
return encodeURI( 'https://www.youtube.com/watch?v=' + videoId )
}
function getMovieId( search, options ) {
/* Fetch a Movie ID for querying the TMDB API */
const path = options && options.videoType === 'tv' ? '/3/search/tv?api_key=' : '/3/search/movie?api_key='
const endpoint = 'https://api.themoviedb.org' + encodeURI( path + options.apiKey + '&query=' + search + ( ( options.year === null ) ? '' : '&year=' + options.year ) + ( ( options.language === null ) ? '' : '&language=' + options.language ) )
const result = fetch( endpoint, {
method: 'GET'
} )
.then( handleFetchErrors )
.then( response => response.json() )
.then( json => {
if ( typeof json.status_message !== 'undefined' ) {
// Error
throw new TypeError( json.status_message )
} else if ( json.results.length === 0 ) {
// Retry failed search without year
if ( options.year !== null ) {
const { year, ...rest } = options
return getMovieId( search, rest )
}
// Error
throw new Error( `No TMDB Movie found with that search query, try searching https://www.themoviedb.org/search?query=${encodeURIComponent( search )} to verify one exists` )
} else {
return json.results[0].id
}
} )
.catch( error => {
handleErrors( error )
return null
} )
return result
}
function getTrailer( movieId, options ) {
const path = options && options.videoType === 'tv' ? '/3/tv/' : '/3/movie/'
/* Fetch single or multiple movie trailers via the TMDB API */
const endpoint = 'https://api.themoviedb.org' + encodeURI( path + movieId + '/videos?api_key=' + options.apiKey + ( ( options.language === null ) ? '' : '&language=' + options.language ) )
const result = fetch( endpoint, {
method: 'GET'
} )
.then( handleFetchErrors )
.then( response => response.json() )
.then( json => {
if ( typeof ( json.status_message ) !== 'undefined' ) {
// Error
throw new TypeError( `movie-trailer: ${json.status_message}` )
}
if ( json.results.length === 0 ) {
// Error
throw new Error( 'No trailers found for that TMDB ID' )
}
let { results } = json
// Strip all but videoId
results = results.map( result => {
return result.key
} )
if ( !options.id ) {
// Return Youtube videoId or full `Watch` URL
results = results.map( videoId => toUrl( videoId ) )
}
return options.multi ? [ ...new Set( results ) ] : results[0]
} )
.catch( error => {
handleErrors( error )
return null
} )
return result
}
async function movieTrailer( movie, options, cb, legacy ) {
/* Fetch movie trailers */
// Massage inputs
let config = {
multi: false,
id: false,
year: null,
language: null,
videoType: 'movie', // Added videoType property
// Public Key on purpose
apiKey: '9d2bff12ed955c7f1f74b83187f188ae'
}
if ( !options ) {
options = {}
}
if ( typeof movie !== 'string' && !options.tmdbId ) {
throw new Error( 'Expected first parameter to be a movie or TMDB ID (string)' )
} else if ( typeof options === 'function' ) {
// Second parameter is the callback
cb = options
options = null
} else if ( typeof options === 'boolean' || options === 'true' ) {
// Second parameter is multi
config.multi = options
} else if ( typeof options === 'string' || typeof options === 'number' ) {
// Second parameter is year
config.year = options
/* BACKWARDS-COMPATABILITY FOR v1 */
if ( typeof legacy === 'function' && ( typeof cb === 'boolean' || ( typeof cb === 'string' && cb === 'true' ) ) ) {
// Third parameter is multi
config.multi = cb
cb = legacy
}
/* END BACKWARDS-COMPATABILITY */
} else if ( typeof options === 'object' ) {
// Set options
config = Object.assign( config, options )
}
// Remove invalid callback
if ( typeof cb !== 'function' ) {
cb = null
}
const movieId = config.tmdbId ? config.tmdbId : ( await getMovieId( movie, config )
.catch( error => {
handleErrors( error )
return null
} ) )
// Get the TMDB content ID
if ( !movieId ) {
// Failed
return null
}
// Get the trailers themselves
const result = getTrailer( movieId, config )
if ( !result ) {
// Failed
return null
}
// Call callback if supplied
if ( cb ) {
return result
.then( response => cb( null, response ) )
.catch( error => cb( error, null ) )
}
// Return promise
return result
}
// Exposed public method
return movieTrailer
} )