From 19be7e4c503be3ec78d428abb505bad519c5359a Mon Sep 17 00:00:00 2001 From: Wise Colt Date: Tue, 1 Jan 2019 00:44:39 +0300 Subject: [PATCH] Hello World --- README.md | 2 + example/getinfo.js | 13 ++++++ index.js | 1 + lib/flixinfo.js | 109 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 33 ++++++++++++++ 5 files changed, 158 insertions(+) create mode 100644 README.md create mode 100644 example/getinfo.js create mode 100644 index.js create mode 100644 lib/flixinfo.js create mode 100644 package.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..ea4c691 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# flixinfo +Get movie detail with netflix id diff --git a/example/getinfo.js b/example/getinfo.js new file mode 100644 index 0000000..5bddbca --- /dev/null +++ b/example/getinfo.js @@ -0,0 +1,13 @@ +const GetFlix = require('../index'); + +const getflix = new GetFlix({tmdbApiKey : 'f29e56ff85f361ff01b5c5403a343021'}); + +getflix.getInfo('70143836', (err, res)=>{ + if(err){ + console.log(err); + } + else{ + console.log(res); + } +}) + diff --git a/index.js b/index.js new file mode 100644 index 0000000..b01bec4 --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = require('./lib/flixinfo'); \ No newline at end of file diff --git a/lib/flixinfo.js b/lib/flixinfo.js new file mode 100644 index 0000000..a28297f --- /dev/null +++ b/lib/flixinfo.js @@ -0,0 +1,109 @@ +// Depend +const cheerio = require('cheerio'); +const request = require('request'); + +class RateFlix { + + constructor(options = {}) { + if (!options.tmdbApiKey) throw new Error('Missing tmdb api key'); + + this.tmdbApiKey = options.tmdbApiKey; + } + + getInfo(netflixId, cb) { + + // Imdb id page + const flixableURL = 'https://flixable.com/title/' + netflixId; + // The Movie DB api page + const theMovieDbURL = 'https://api.themoviedb.org/3/find/'; + // API Key + const tmdbApiKey = this.tmdbApiKey; + + var returnResult = {}; + + request(flixableURL, (err, response, body) => { + + if (err) { + const error = 'flixable.com request error details: ' + err; + const returnResult = null; + cb(error, returnResult) + } + else { + const $ = cheerio.load(body, { + normalizeWhitespace: true, + xmlMode: true + }); + + const imdbResult = $('[class="imdbRatingPlugin"]').length; + + if (imdbResult) { + $('[class="imdbRatingPlugin"]').each(function (i, element) { + + const imdbId = $(this).attr('data-title'); + const tmdbURL = theMovieDbURL + imdbId + '?api_key=' + tmdbApiKey + '&language=en-EN&external_source=imdb_id' + + request(tmdbURL, (err, resultBody, body) => { + + if (err) { + const error = 'tmdb api request error details: ' + err; + const returnResult = null; + cb(error, returnResult) + } + else { + // Result convert the json + const getJson = JSON.parse(resultBody.body); + + if (getJson.tv_results.length) { + // If result is tv + var result = getJson.tv_results[0]; + var originalName = result.original_name; + var name = result.name; + var year = result.first_air_date; + if(result.poster_path != null){ + var poster = 'https://image.tmdb.org/t/p/original/' + result.poster_path; + } + if(result.poster_path != null){ + var backdrop = 'https://image.tmdb.org/t/p/original/' + result.backdrop_path; + } + var country = result.origin_country[0]; + var rate = result.vote_average; + var overviewEN = result.overview; + + } + else if (getJson.movie_results.length) { + // If result is movie + var result = getJson.movie_results[0]; + var originalName = result.original_title; + var name = result.title; + var year = result.release_date; + if(result.poster_path != null){ + var poster = 'https://image.tmdb.org/t/p/original/' + result.poster_path; + } + if(result.poster_path != null){ + var backdrop = 'https://image.tmdb.org/t/p/original/' + result.backdrop_path; + } + var country = result.original_language; + var rate = result.vote_average; + var overviewEN = result.overview; + } + + returnResult = { result: 1, originalName, name, year, poster, backdrop, country, rate, overviewEN }; + const error = null; + // If there is no error return the result + cb(error, returnResult); + } + }); + }) + } + else { + // Return the error if imdb id cannot be found + const error = null + const returnResult = {result: 0}; + cb(error, returnResult) + } + } + }); + } +} + +module.exports = RateFlix; diff --git a/package.json b/package.json new file mode 100644 index 0000000..87616e0 --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "flixinfo", + "version": "1.0.0", + "description": "Get movie detail with netflix id", + "main": "index.js", + "directories": { + "example": "example", + "lib": "lib" + }, + "scripts": { + "test": "test" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/sbilketay/flixinfo.git" + }, + "keywords": [ + "netflix", + "movieinfo", + "movie", + "tvseries" + ], + "author": "wisecolt", + "license": "ISC", + "bugs": { + "url": "https://github.com/sbilketay/flixinfo/issues" + }, + "homepage": "https://github.com/sbilketay/flixinfo#readme", + "dependencies": { + "cheerio": "^1.0.0-rc.2", + "request": "^2.88.0" + } +}