├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Mojtaba Rabiei 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # imdb-search 2 | [![NPM](https://nodei.co/npm/imdb-search.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/imdb-search/) 3 | 4 | A wrapper on omdb module to easy select correct wanted item with all info 5 | Use ES6 Promise for search and get a title info. 6 | 7 | ## Installation 8 | with npm: 9 | ```bash 10 | $ npm install --save imdb-search 11 | 12 | ``` 13 | or with yarn: 14 | ```bash 15 | $ yarn add imdb-search 16 | ``` 17 | ## Example 18 | ```js 19 | const imdb = require('imdb-search'); 20 | 21 | // Search for a Movie, result is an array of movies 22 | imdb.search('Doctor Strange') 23 | .then(result => { 24 | for (let movie of result) { 25 | console.log(`${movie.id}: ${movie.title} - ${movie.year}`); 26 | } 27 | }) 28 | .catch(err => { 29 | console.log(err); 30 | }); 31 | 32 | // We can pass year and type as optional args to get right result 33 | // Because of omdb dependency movies can have this types: `series, movie, episode` 34 | imdb.search('Doctor Strange', 2016, 'movie') 35 | .then(result => { 36 | for (let movie of result) { 37 | console.log(`${movie.id}: ${movie.title} - ${movie.year}`); 38 | } 39 | }) 40 | .catch(err => { 41 | console.log(err); 42 | }); 43 | 44 | // We can select item number in returned result 45 | imdb.search('Doctor Strange', 2016, 'movie') 46 | .then(result => { 47 | return imdb.get(0); // Select first movie on the list 48 | }) 49 | .then(movie => { 50 | console.log(`${movie.title} - ${movie.year}`); 51 | }) 52 | .catch(err => { 53 | console.log(err); 54 | }); 55 | 56 | 57 | // Get a movie with imdb id 58 | imdb.get('tt1211837') 59 | .then((movie) => { 60 | console.log(`${movie.title} - ${movie.year}`); 61 | }) 62 | .catch((err) => { 63 | console.log(err); 64 | }); 65 | ``` 66 | ## Methods 67 | 68 | ### `search(name[, year, type])` 69 | Use this method to search on imdb with name, you can pass year and type as optional arguments. 70 | Search method return an array of movies. 71 | 72 | **Note:** Each item of search array have one field named`id`(internal id), use this id to get more info. 73 | 74 | ### `get(id)` 75 | Use this method to get a movie from the list of movies on the last search 76 | 77 | **Note:** `id` value can be an internal id or imdb specific id. 78 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const omdb = require('omdb'); 2 | 3 | class Imdb { 4 | constructor() { 5 | this.movies = []; 6 | this.lastMovie = null; 7 | } 8 | 9 | search(name = '', year = '', type = '') { 10 | this.movies = []; 11 | 12 | let pattern = { 13 | terms: name, 14 | year: year, 15 | type: type 16 | }; 17 | 18 | return new Promise((resolve, reject) => { 19 | omdb.search(pattern, (err, movies) => { 20 | if (err) { 21 | reject(err); 22 | } 23 | 24 | if (typeof movies === 'undefined' || !movies.length) { 25 | reject('Movie not found!'); 26 | } else { 27 | for (let i = 0; i < movies.length; i += 1) { 28 | movies[i].id = i; 29 | this.movies.push(movies[i]); 30 | } 31 | 32 | resolve(this.movies); 33 | } 34 | }); 35 | }) 36 | } 37 | 38 | getImdb(id) { 39 | let imdb = this.movies.filter(movie => movie.id === id); 40 | 41 | if (imdb.length) { 42 | return imdb[0].imdb; 43 | } 44 | 45 | return 0; 46 | } 47 | 48 | get(id) { 49 | let imdb = ''; 50 | 51 | if (typeof id === 'string' && id.startsWith('tt')) { 52 | imdb = id; 53 | } else { 54 | imdb = this.getImdb(parseInt(id)); 55 | } 56 | 57 | let options = { fullPlot: true, tomatoes: true }; 58 | 59 | return new Promise((resolve, reject) => { 60 | omdb.get({ imdb: imdb }, options, (err, movie) => { 61 | if (err) { 62 | reject(err); 63 | } 64 | 65 | if (typeof movie === 'undefined' || !movie) { 66 | reject('Movie not found!'); 67 | } else { 68 | 69 | this.lastMovie = movie; 70 | 71 | resolve(movie); 72 | } 73 | }); 74 | }); 75 | } 76 | 77 | } 78 | 79 | module.exports = new Imdb(); 80 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "imdb-search", 3 | "version": "0.1.5", 4 | "description": "A wrapper on omdb module to easy select correct wanted item with all info", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "git+https://github.com/newvertex/imdb-search.git" 9 | }, 10 | "keywords": [ 11 | "imdb", 12 | "omdb", 13 | "search", 14 | "movies" 15 | ], 16 | "author": "Mojtaba Rabiei ", 17 | "license": "MIT", 18 | "dependencies": { 19 | "omdb": "^0.7.0" 20 | }, 21 | "bugs": { 22 | "url": "https://github.com/newvertex/imdb-search/issues" 23 | }, 24 | "homepage": "https://github.com/newvertex/imdb-search#readme" 25 | } 26 | --------------------------------------------------------------------------------