Skip to content

Commit

Permalink
galore ⭐
Browse files Browse the repository at this point in the history
  • Loading branch information
lacymorrow committed Feb 27, 2018
1 parent 346ed5e commit 2d1d1c7
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 64 deletions.
97 changes: 55 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# movie-art [![npm version](https://badge.fury.io/js/movie-art.svg)](https://badge.fury.io/js/movie-art) [![Build Status](https://travis-ci.org/lacymorrow/movie-art.svg?branch=master)](https://travis-ci.org/lacymorrow/movie-art)
# movie-art [![npm version](https://badge.fury.io/js/movie-art.svg)](https://badge.fury.io/js/movie-art) [![Build Status](https://travis-ci.org/lacymorrow/movie-art.svg?branch=master)](https://travis-ci.org/lacymorrow/movie-art) [![Try movie-art on RunKit](https://badge.runkitcdn.com/movie-art.svg)](https://npm.runkit.com/movie-art)

> Get a movie (or TV-series) poster and backdrop image url: "Oceans Eleven" ➔ http://path/to/oceans_eleven_poster.jpg
Expand All @@ -7,57 +7,76 @@

## Install

Use with your favorite module loader or package manager. In the browser:

```html
<!-- movieInfo window global -->
<script type="text/javascript" src="https://unpkg.com/movie-art" />
```
Using [NPM](https://npmjs.com):
```bash
$ npm install --save movie-art
$ npm install -g movie-art
```
## Features
* Use anywhere, browser or Node - UMD _([Browser Support](https://caniuse.com/#feat=fetch))_
* Promise and Callback API
* Fetch images for movies or television
* Poster or backdrop photos
## Usage
```js
var movieArt = require('movie-art')
movieArt('Oceans Eleven').then(console.log)
```
##### Basic usage
##### Callback
```js
movieArt('Oceans Eleven', function (err, url) {
console.log(url)
//=> http://path/to/oceans_eleven_poster.jpg
movieArt('Oceans Eleven', (err, res) => {
console.log(res)
//=> http://path/to/oceans_eleven.jpg
})
```
##### Usage with landscape orientation backdrop
##### Usage with backdrop and poster
```js
movieArt('Oceans Eleven', {landscape: true}, function (err, url) {
console.log(url)
//=> http://path/to/oceans_eleven_backdrop.jpg
})
movieArt('Oceans Eleven', {output: 'all'})
.then( res => console.log(res.backdrop) )
//=> http://path/to/oceans_eleven_backdrop.jpg
```
##### Usage with size and year options
```js
movieArt('Oceans Eleven', {year: '1960', size: 'w92'}, function (err, url) {
console.log(url)
//=> http://path/to/oceans_eleven_poster_1960_small.jpg
})
movieArt('Oceans Eleven', {year: '1960', size: 'w92'})
.then( res => console.log(res.backdrop) )
//=> http://path/to/oceans_eleven_poster_1960_small.jpg
```
##### Query television art
```js
movieArt('Star Trek: The Original Series', {type: 'tv'}, function (err, url) {
console.log(url)
//=> http://path/to/star_trek_the_original_series_poster.jpg
})
movieArt('Star Trek: The Original Series', {type: 'tv'})
.then( res => console.log(res.backdrop) )
//=> http://path/to/star_trek_the_original_series_poster.jpg
```
## API
### movieArt(movie [, options] [, callback])
### movieArt(search [, options] [, callback])
Returns a Promise which resolves to a string URL
Accepts a movie or television show title (string) as input.
Returns a Promise which resolves to a string URL.
#### movie
#### search
*Required*
Type: `string`
Expand All @@ -66,7 +85,7 @@ Movie to search for.
#### callback(error, response)
Function to be called when complete or on error
Function to be called when complete or on error.
### Options
Expand All @@ -85,46 +104,40 @@ Optional movie year.
Type: `string`
Requested poster size.
Call `movieArt(function(e){console.log(e);});` or run the CLI command with no arguments to retrieve the list of available sizes.

*possible values at time of writing:* `w92`, `w154`, `w185`, `w342`, `w500`, `w780`, `original`
*possible values:* `w92`, `w154`, `w185`, `w342`, `w500`, `w780`, `original`
#### type
Type: `string`
The type of request: either `tv` or `movie`. Defaults to `movie`.
*possible values:* `tv`, `movie`
#### landscape
#### output
Type: `boolean`
Returns a wider, landscape orientation backdrop if true

`backdrop` returns a wider, backdrop output backdrop.
`all` returns an object like `{poster:..., backdrop: ...}`
Default: `poster`
*possible values:* `poster`, `backdrop`, `all`
## CLI

You can also use it as a CLI app by installing it globally:

```bash
$ npm install --global movie-art
```
#### Usage
#### CLI Usage
```
$ movie-art --help
Usage
$ movie-art movie [year] [size] [type] [landscape]
$ movie-art movie [year] [size] [type] [output]
Options
--year, -y Release date year
--size, -s Possible values: [w92, w154, w185, w342, w500, w780, original]
--type, -t Possible values: [tv, movie]
--landscape, -l Return wider backdrop image if true
--year, -y Release date year
--size, -s Possible values: [w92, w154, w185, w342, w500, w780, original]
--type, -t Possible values: [tv, movie]
--output, -o Possible values: [poster, backdrop, all]
Example
$ movie-art 'Oceans Eleven' --year 1960 --size w92
Expand Down
20 changes: 10 additions & 10 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ const movieArt = require( './index' )

const cli = meow( `
Usage
$ movie-art movie [year] [size] [type] [landscape]
$ movie-art movie [year] [size] [type] [output]
Options
--year, -y Release date year
--size, -s Possible values: [w92, w154, w185, w342, w500, w780, original]
--type, -t Possible values: [tv, movie]
--landscape, -l Return wider backdrop image if true
--year, -y Release date year
--size, -s Possible values: [w92, w154, w185, w342, w500, w780, original]
--type, -t Possible values: [tv, movie]
--output, -o Possible values: [poster, backdrop, all]
Example
$ movie-art 'Oceans Eleven' --year 1960
// => ...
`, {
flags: {
landscape: {
type: 'boolean',
alias: 'l'
output: {
type: 'string',
alias: 'o'
},
size: {
type: 'string',
Expand All @@ -41,13 +41,13 @@ let opts = {
size: null,
type: null,
year: null,
landscape: false
output: 'poster'
}

if ( cli.flags.s ) opts.size = cli.flags.s
if ( cli.flags.t ) opts.type = cli.flags.t
if ( cli.flags.y ) opts.year = cli.flags.y
if ( cli.flags.l ) opts.landscape = cli.flags.l
if ( cli.flags.o ) opts.output = cli.flags.o
if ( cli.input[1] ) opts.year = cli.flags.y
if ( !cli.input[0] ) cli.showHelp()

Expand Down
24 changes: 15 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
year: null,
size: null,
type: 'movie',
landscape: false
output: 'poster'
}, options )

if ( opts.type === null || ( opts.type !== 'tv' && opts.type !== 'movie' ) ) {
Expand All @@ -92,6 +92,12 @@

}

if ( opts.output !== 'all' && opts.output !== 'backdrop' ) ) {

opts.output = 'poster'

}

// Get configuration vars
const { baseURL, sizes } = await getConfiguration()

Expand Down Expand Up @@ -148,15 +154,15 @@
} else {

// Success
const image = opts.landscape ? json.results[0].backdrop_path : json.results[0].poster_path
if ( sizes.indexOf( opts.size ) !== -1 ) {

return encodeURI( baseURL + opts.size + image )

const size = sizes.indexOf( opts.size ) !== -1 ? opts.size : sizes[sizes.length - 1]
if( opts.output === 'all' ) {
return {
backdrop: encodeURI( baseURL + size + json.results[0].backdrop_path ),
poster: encodeURI( baseURL + size + json.results[0].poster_path )
}
} else {

return encodeURI( baseURL + sizes[sizes.length - 1] + image )

const image = opts.output === 'backdrop' ? json.results[0].backdrop_path : json.results[0].poster_path
return encodeURI( baseURL + size + image)
}

}
Expand Down
15 changes: 12 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ test( 'returns a url with year ', async t => {

} )

test( 'landscape returns a different url ', async t => {
test( 'backdrop returns a different url ', async t => {

t.plan( 1 )

const response1 = await movieArt( 'crash' )
const response2 = await movieArt( 'crash', { landscape: true } )
t.not( response1, response2, 'portrait and landscape are different URLs' )
const response2 = await movieArt( 'crash', { output: 'backdrop' } )
t.not( response1, response2, 'poster and backdrop are different URLs' )

} )

Expand All @@ -48,6 +48,15 @@ test( 'returns tv results ', async t => {

} )

test( 'fetch backdrop and poster', async t => {

t.plan( 1 )

const response = await movieArt( 'crash', { output: 'all' } )
t.is( typeof response, 'object', 'response is an object' )

} )

test.cb( 'callback returns a url ', t => {

t.plan( 1 )
Expand Down

0 comments on commit 2d1d1c7

Please sign in to comment.