Skip to content

Commit

Permalink
Add uid=1659308120(tmorrow) gid=410487729 groups=410487729,12(everyon…
Browse files Browse the repository at this point in the history
…e),62(netaccounts),79(_appserverusr),80(admin),81(_appserveradm),98(_lpadmin),264(_webdeveloper),702(com.apple.sharepoint.group.2),701(com.apple.sharepoint.group.1),33(_appstore),100(_lpoperator),204(_developer),250(_analyticsusers),395(com.apple.access_ftp),398(com.apple.access_screensharing),399(com.apple.access_ssh) option
  • Loading branch information
lacymorrow committed Oct 29, 2019
1 parent a32a498 commit d22c71a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 26 deletions.
32 changes: 19 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ In the browser:
## Usage

```js
const movieTrailer = require('movie-trailer');
const movieTrailer = require( 'movie-trailer' )

movieTrailer('Crash').then(console.log)
movieTrailer( 'Crash' ).then( console.log ).catch( console.error )

//=> https://www.youtube.com/watch?v=durNwe9pL0E
```

##### Search using release date year
```js
movieTrailer('Oceans Eleven', 1960)
movieTrailer( 'Oceans Eleven', 1960 )
.then( response => console.log( response ) )

//=> http://path/to/trailer
```

##### Return an array of URLs
##### Return an array of video IDs
```js
movieTrailer('Oceans Eleven', true)
movieTrailer( 'Oceans Eleven', {id: true, multi: true} )
.then( response => console.log( response ) )

//=> [ ... ]
Expand Down Expand Up @@ -91,23 +91,29 @@ movieTrailer( 'Oceans Eleven', ( error, response ) => {

Type: `object`

* ##### year
* ##### id _(`false`)_

Type: `string` || `number`
Type: `boolean`

Optional movie year.
_(optional)_ Return only Youtube video IDs.

Use `-y` or `--year` on the CLI
_Use `-i` or `--id` on the CLI_

* ##### multi
* ##### multi _(`false`)_

Type: `boolean`

Optionally return array of urls instead of a single url.
_(optional)_ Return an array of urls vs a single url.

_Use `-m` or `--multi` on the CLI_

* ##### year

Type: `string` || `number`

Use `-m` or `--multi` on the CLI
_(optional)_ Movie release year.

***You may specify either option or an object containing `multi` and `year` properties***
_Use `-y` or `--year` on the CLI_


* #### callback(error, response)
Expand Down
13 changes: 10 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ const cli = meow(
$ movie-trailer movie [year] [multi]
Options
--year, -y Specify a release year to search
--multi, -m Returns an array of URLs instead of a single URL
--language, -l Specify a language code (eg: 'de_DE')
--year, -y Specify a release year to search.
--multi, -m Returns an array of URLs instead of a single URL.
--language, -l Specify a language code (eg: 'de_DE').
--id -i Return just the Youtube video ID.
Example
$ movie-trailer 'Oceans Eleven' --year 1960
// => http://path/to/trailer
`,
{
flags: {
id: {
alias: 'i',
type: 'boolean'
},
multi: {
alias: 'm',
type: 'boolean'
Expand All @@ -36,11 +41,13 @@ const cli = meow(
)

let opts = {
id: false,
multi: false,
year: null,
language: null
}

if ( cli.flags.i ) opts.id = !!cli.flags.i
if ( cli.flags.m ) opts.multi = !!cli.flags.m
if ( cli.flags.y ) opts.year = cli.flags.y
if ( cli.flags.l ) opts.language = cli.flags.l
Expand Down
42 changes: 34 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
// Public Key on purpose
const apiKey = '9d2bff12ed955c7f1f74b83187f188ae'

function toUrl ( videoId ) {

return encodeURI( 'https://www.youtube.com/watch?v=' + videoId )

}

function getMovieId ( search, year, language ) {

/* Fetch a Movie ID for querying the TMDB API */
Expand Down Expand Up @@ -70,11 +76,11 @@

}

function getTrailer ( movieId, multi, language ) {
function getTrailer ( movieId, multi, videoId, language ) {

/* Fetch single or multiple movie trailers via the TMDB API */
const url = 'https://api.themoviedb.org' + encodeURI( '/3/movie/' + movieId + '/videos?api_key=' + apiKey + ( ( language !== null ) ? '&language=' + language : '' ) )
const response = fetch( url, {
const endpoint = 'https://api.themoviedb.org' + encodeURI( '/3/movie/' + movieId + '/videos?api_key=' + apiKey + ( ( language !== null ) ? '&language=' + language : '' ) )
const response = fetch( endpoint, {
method: 'GET'
} )
.then(
Expand All @@ -93,14 +99,32 @@
// Error
return Promise.reject( new Error( 'API - No results found' ) )

} else if ( multi ) {
}

let results = json.results

// Strip all but videoId
results = results.map( result => {

return result.key

} )

if ( !videoId ) {

// Return Youtube videoID or full `Watch` URL
results = results.map( toUrl )

}

if ( multi ) {

// Return *unique* urls
return Array.from( new Set( json.results.map( e => encodeURI( 'https://www.youtube.com/watch?v=' + e.key ) ) ) )
return Array.from( new Set( results ) )

} else {

return encodeURI( 'https://www.youtube.com/watch?v=' + json.results[0].key )
return results[0]

}

Expand All @@ -117,9 +141,11 @@
// Massage inputs
let opts = {
multi: false,
id: false,
year: null,
language: null
}

if ( typeof movie !== 'string' ) {

throw new Error( 'Expected first parameter to be a movie (string)' )
Expand All @@ -130,7 +156,7 @@
cb = options
options = null

} else if ( typeof options === 'boolean' || ( typeof options === 'string' && options === 'true' ) ) {
} else if ( typeof options === 'boolean' || options === 'true' ) {

// Second parameter is multi
opts.multi = options
Expand Down Expand Up @@ -163,7 +189,7 @@
const response = getMovieId( movie, opts.year, opts.language )
.then( movieId => {

return getTrailer( movieId, opts.multi, opts.language )
return getTrailer( movieId, opts.multi, opts.id, opts.language )

} )

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "movie-trailer",
"version": "2.0.4",
"version": "2.0.5",
"description": "Get movie trailer url(s): Oceans Eleven ➔ http://path/to/trailer",
"license": "MIT",
"main": "index.js",
Expand Down
13 changes: 12 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
'use strict'
import test from 'ava'
import movieTrailer from './index'

Expand All @@ -24,6 +24,17 @@ test( 'fetch movie trailer with year', async t => {

} )

test( 'fetch movie trailer as video ID', async t => {

t.plan( 2 )

const trailer = await movieTrailer( 'oceans eleven', { id: true } )

t.is( trailer.indexOf( 'http' ), -1, 'does not return a url' )
t.is( trailer.indexOf( 'youtube' ), -1, 'is not a youtube url' )

} )

test( 'fetch movie trailer with language', async t => {

t.plan( 1 )
Expand Down

0 comments on commit d22c71a

Please sign in to comment.