0.1.7 • Published 6 years ago

moviedb-promise-es6 v0.1.7

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

moviedb-promise-es6

A Node library that makes the interaction with themoviedb.org V3 API easy.

This was original a pull request that went stale, so it's its own package now. The original package developed by Dan Zajdband uses callbacks to handle the asynchronous nature of Node, while this package uses native Promises.

The main credit goes to the original moviedb package by Dan Zajdband.

This library was written by Grant Holle and was updated to support ES6 imports.

Installation

npm install moviedb-promise-es6 --save

Usage

Require the module and instantiate the class with your themoviedb.org API KEY.

import MovieDb from 'moviedb-promise-es6';

const moviedb = new MovieDb('your api key')

async/await reminder

All functions return a Promise, which means that you can also use async/await. The caveat of using await when making function calls is that the await has to be within a function that has been declard async. Keep that in mind if you plan to use await.

Examples

// Using just the Promise
moviedb.searchMovie({ query: 'Alien' }).then(res => {
  console.log(res)
}).catch(console.error)

// Using await
// You probably wouldn't ever use it this way...
;(async function () {
  try {
    const res = await moviedb.searchMovie({ query: 'alien' })
    console.log(res)
  } catch (e) {
    console.log(e)
  }
})()

// This is a more reasonable example
const findMovie = async title => {
  const res = await moviedb.searchMovie({ query: title })

  return res
}

try {
  const results = findMovie('alien')
} catch (e) {
  // Do something
}

or

moviedb.movieInfo({ id: 666 }).then(res => {
  console.log(res)
}).catch(console.error)

Some endpoints, such as watchlist endpoints, have an optional account id parameter. If you have a session id, you don't need to provide that parameter.

// This is the same as calling it as
// moviedb.accountMovieWatchlist({ id: '{account_id}' })
moviedb.sessionId = 'my-cached-session-id'
moviedb.accountMovieWatchlist().then(res => {
  // Your watchlist items
  console.log(res)
}).catch(console.error)

// Creating a session id would look something like this
moviedb.requestToken().then(token => {
  // Now you need to visit this url to authorize
  const tokenUrl = `https://www.themoviedb.org/authenticate/${token}`
}).catch(console.error)

// After that has been authorized, you can get the session id
moviedb.session().then(sessionId => {
  // Probably cache this id somewhere to avoid this workflow
  console.log(sessionId)

  // After the sessionId is cached, the next time use instantiate the class,
  // set the sessionId by moviedb.sessionId = 'my-session-id'

  // This can be called now because sessionId is set
  moviedb.accountMovieWatchlist().then(res => {
    // Your watchlist items
    console.log(res)
  }).catch(console.error)
}).catch(console.error)

Advanced use

The MovieDB constructor accepts 3 parameters:

const MovieDb = require('moviedb')
const moviedb = new MovieDb(apiKey, useDefaultLimits, baseURL)

By default, moviedb-promise limits the requests you can send to 39 requests/10 seconds (this is the limit imposed by TheMovieDB). This way you won't have to deal with 429 errors.

If you want to implement your own request rate limiter, you can set useDefaultLimits to false when creating the moviedb instance.

Available methods

The Function column lists all the available functions on the class. The Endpoint column lists possible request parameters (placehoders prefixed with :) needed for the call. If the endpoint doesn't have any placeholders, check out the documentation for the query parameters you can use.

Examples

FunctionEndpoint
tvInfotv/:id
// Two ways:
// The object key matches the placeholder name
moviedb.tvInfo({ id: 61888 }).then(...)

// Or for simplicity, if it only has one placeholder
moviedb.tvInfo(61888).then(...)
FunctionEndpoint
searchMoviesearch/movie

There aren't any placeholders, but the documentation shows there are language, query, page, include_adult, region, year, and primary_release_year available to use. Each expect a certain data type or format, so check out the docs for the details.

const parameters = {
  query: 'Kindergarten Cop',
  language: 'fr' // ISO 639-1 code
}

moviedb.searchMovie(parameters).then(...)

Complete list

FunctionEndpoint
configurationconfiguration
findfind/:id
searchMoviesearch/movie
searchTvsearch/tv
searchMultisearch/multi
searchCollectionsearch/collection
searchPersonsearch/person
searchListsearch/list
searchCompanysearch/company
searchKeywordsearch/keyword
collectionInfocollection/:id
collectionImagescollection/:id/images
discoverMoviediscover/movie
discoverTvdiscover/tv
movieInfomovie/:id
movieAlternativeTitlesmovie/:id/alternative_titles
movieCreditsmovie/:id/credits
movieImagesmovie/:id/images
movieVideosmovie/:id/videos
movieKeywordsmovie/:id/keywords
movieReleasesmovie/:id/releases
movieReleaseDatesmovie/:id/release_dates
movieTrailersmovie/:id/trailers
movieTranslationsmovie/:id/translations
movieSimilarmovie/:id/similar_movies
movieReviewsmovie/:id/reviews
movieListsmovie/:id/lists
movieChangesmovie/:id/changes
movieRatingUpdatemovie/:id/rating
tvInfotv/:id
tvCreditstv/:id/credits
tvExternalIdstv/:id/external_ids
tvImagestv/:id/images
tvVideostv/:id/videos
tvSimilartv/:id/similar
tvTranslationstv/:id/translations
tvSeasonInfotv/:id/season/:season_number
tvSeasonCreditstv/:id/season/:season_number/credits
tvSeasonVideostv/:id/season/:season_number/videos
tvSeasonExternalIdstv/:id/season/:season_number/external_ids
tvSeasonImagestv/:id/season/:season_number/images
tvEpisodeInfotv/:id/season/:season_number/episode/:episode_number
tvEpisodeCreditstv/:id/season/:season_number/episode/:episode_number/credits
tvEpisodeExternalIdstv/:id/season/:season_number/episode/:episode_number/external_ids
tvEpisodeImagestv/:id/season/:season_number/episode/:episode_number/images
tvOnTheAirtv/on_the_air
tvAiringTodaytv/airing_today
tvRecommendtv/:id/recommendations
tvChangestv/:id/changes
personInfoperson/:id
personMovieCreditsperson/:id/movie_credits
personTvCreditsperson/:id/tv_credits
personCombinedCreditsperson/:id/combined_credits
personImagesperson/:id/images
personTaggedImagesperson/:id/tagged_images
personChangesperson/:id/changes
personLatestperson/latest
personPopularperson/popular
personExternalIdsperson/:id/external_ids
creditInfocredit/:id
listInfolist/:id
genreMovieListgenre/movie/list
genreTvListgenre/tv/list
genreMoviesgenre/:id/movies
keywordInfokeyword/:id
keywordMovieskeyword/:id/movies
companyInfocompany/:id
companyMoviescompany/:id/movies
accountInfoaccount
accountListsaccount/:id/lists
accountFavoriteMoviesaccount/:id/favorite_movies
accountFavoriteUpdateaccount/:id/favorite
accountRatedMoviesaccount/:id/rated_movies
accountMovieWatchlistaccount/:id/watchlist/movies
accountTvWatchlistaccount/:id/watchlist/tv
accountWatchlistUpdateaccount/:id/watchlist
miscLatestMoviesmovie/latest
miscUpcomingMoviesmovie/upcoming
miscNowPlayingMoviesmovie/now_playing
miscPopularMoviesmovie/popular
miscTopRatedMoviesmovie/top_rated
miscChangedMoviesmovie/changes
miscChangedTvstv/changes
miscChangedPeopleperson/changes
miscTopRatedTvstv/top_rated
miscPopularTvstv/popular
requestTokenauthentication/token/new
sessionauthentication/session/new

Contributing

First, thanks for taking the time!

Second, before submitting a pull request, make sure to run npm run test to make sure the tests pass and npm run lint to fix any code style errors.

If you make any endpoint changes, you can run npm run table to generate the markdown table for the readme.

License

MIT

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago