diff --git a/config/index.js b/config/index.js index 4cfa8fc..5e64cfc 100644 --- a/config/index.js +++ b/config/index.js @@ -1,8 +1,9 @@ -var config= { - // Filexible URL - filexibleURL: 'https://flixable.com/title/', - // Tmdb URL - theMovieDbURL: 'https://api.themoviedb.org/3/find/' - } - module.exports = config; - \ No newline at end of file +var config = { + // Filexible URL + filexibleURL: 'https://flixable.com/title/', + // Tmdb URL + theMovieDbURL: 'https://api.themoviedb.org/3/', + // Tmdb image URL + theMovieDbImageURL: 'https://image.tmdb.org/t/p/original/', +} +module.exports = config; diff --git a/example/getinfo.js b/example/getinfo.js index 4525192..9aa9246 100644 --- a/example/getinfo.js +++ b/example/getinfo.js @@ -2,7 +2,7 @@ const GetFlix = require('../index'); try { const getflix = new GetFlix('f29e56ff85f361ff01b5c5403a343021'); - getflix.getInfo('70143836', (res) => { + getflix.getInfo('70242311', (res) => { if (res.error) { console.log(res.errorMsg); } @@ -14,4 +14,3 @@ try { catch (error) { console.log(error.message) } - diff --git a/lib/index.js b/lib/index.js index 78cc6b1..cbdcb80 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,6 +3,8 @@ const cheerio = require('cheerio'); const request = require('request'); // Config const config = require('../config'); +// Modules +const modules = require('./module'); class FlixInfo { @@ -20,13 +22,12 @@ class FlixInfo { const theMovieDbURL = config.theMovieDbURL; // API Key const tmdbApiKey = this.tmdbApiKey; - + // Result Object var returnResult = {}; request(flixableURL, (err, response, body) => { - if (err) { - returnResult = { error: 1, errorMsg: 'flixable.com error' }; + returnResult = { error: 1, errorMsg: 'flixable.com connection error' }; cb(returnResult) } else { @@ -41,62 +42,71 @@ class FlixInfo { $('[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' + + const tmdbURL = theMovieDbURL + 'find/' + imdbId + '?api_key=' + tmdbApiKey + '&language=en-EN&external_source=imdb_id' request(tmdbURL, (err, resultBody, body) => { const getJson = JSON.parse(resultBody.body); - - if(getJson.status_code > 1){ - // if wrong api key error - returnResult = { error: 1, errorMsg: 'tmdb wrong api key error' }; - cb(returnResult) + + if (getJson.status_code > 1) { + // if wrong api key error + returnResult = { error: 1, errorMsg: 'tmdb find id wrong api key error' }; + cb(returnResult) } - else{ + else { if (err) { // if thmdb api connection error - returnResult = { error: 1, errorMsg: 'tmdb api error' }; + returnResult = { error: 1, errorMsg: 'tmdb find id connection api error' }; cb(returnResult) } else { - 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 (getJson.tv_results.length || getJson.movie_results.length) { + + if (getJson.movie_results.length) { + var watchParameter = 'movie'; + var thmdbId = getJson.movie_results[0].id; } - if (result.poster_path != null) { - var backdrop = 'https://image.tmdb.org/t/p/original/' + result.backdrop_path; + else if (getJson.tv_results.length) { + var watchParameter = 'tv'; + var thmdbId = getJson.tv_results[0].id; } - var country = result.origin_country[0]; - var rate = result.vote_average; - var overviewEN = result.overview; - + // Get detail info + modules.getDetails(thmdbId, watchParameter, tmdbApiKey, (err, getDetailResult) => { + if (err) { + returnResult = { error: 1, errorMsg: err }; + cb(returnResult); + } + else { + // Get credits info + modules.getCredits(thmdbId, watchParameter, tmdbApiKey, (err, getCreditsResult) => { + if (err) { + returnResult = { error: 1, errorMsg: err }; + cb(returnResult); + } + else { + // Get images info + modules.getImages(thmdbId, watchParameter, tmdbApiKey, (err, getImagesResult) => { + if (err) { + returnResult = { error: 1, errorMsg: err }; + cb(returnResult); + } + else { + returnResult = { result: 1, error: 0, tv_results: getDetailResult, credits: getCreditsResult, images: getImagesResult }; + cb(returnResult); + } + }); + } + }); + } + }); } - 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; + else { + // Return the error if imdb id cannot be found + const returnResult = { result: 0, error: 0 }; + cb(returnResult) } - - returnResult = { result: 1, error: 0, originalName, name, year, poster, backdrop, country, rate, overviewEN }; - // If there is no error return the result - cb(returnResult); } } }); diff --git a/lib/module.js b/lib/module.js new file mode 100644 index 0000000..cf66389 --- /dev/null +++ b/lib/module.js @@ -0,0 +1,119 @@ +const config = require('../config'); +const request = require('request'); + +module.exports.getDetails = function (detailsId, movieOrTv, tmdbApiKey, cb) { + + if (movieOrTv == 'movie' || movieOrTv == 'tv') { + + const detailURL = config.theMovieDbURL + movieOrTv + '/' + detailsId + '?api_key=' + tmdbApiKey + '&language=en-US'; + + request(detailURL, (err, resultBody, body) => { + + const getJson = JSON.parse(resultBody.body); + + if (getJson.status_code > 1) { + // if wrong api key error + const error = 'tmdb get detail wrong api key error'; + const result = null; + cb(error, result); + } + else { + if (err) { + // if thmdb api connection error + const error = 'tmdb get detail api connection error'; + const result = null; + cb(error, result); + } + else { + const error = null; + const result = getJson; + cb(error, result); + } + } + + }); + } + else { + const error = 'tmdb get detail wrong watch parameter'; + const result = null; + cb(error, result); + } +} + +module.exports.getCredits = function (detailsId, movieOrTv, tmdbApiKey, cb) { + + if (movieOrTv == 'movie' || movieOrTv == 'tv') { + + const creditsURL = config.theMovieDbURL + movieOrTv + '/' + detailsId + '/credits?api_key=' + tmdbApiKey + '&language=en-US'; + + request(creditsURL, (err, resultBody, body) => { + + const getJson = JSON.parse(resultBody.body); + + if (getJson.status_code > 1) { + // if wrong api key error + const error = 'tmdb get credits wrong api key error'; + const result = null; + cb(error, result); + } + else { + if (err) { + // if thmdb api connection error + const error = 'tmdb get credits api connection error'; + const result = null; + cb(error, result); + } + else { + const error = null; + const result = getJson; + cb(error, result); + } + } + + }); + } + else { + const error = 'tmdb get credits wrong watch parameter'; + const result = null; + cb(error, result); + } +} + +module.exports.getImages = function (detailsId, movieOrTv, tmdbApiKey, cb) { + + if (movieOrTv == 'movie' || movieOrTv == 'tv') { + + const creditsURL = config.theMovieDbURL + movieOrTv + '/' + detailsId + '/images?api_key=' + tmdbApiKey + '&language=en-US&include_image_language=en,null'; + + request(creditsURL, (err, resultBody, body) => { + + const getJson = JSON.parse(resultBody.body); + + if (getJson.status_code > 1) { + // if wrong api key error + const error = 'tmdb get images wrong api key error'; + const result = null; + cb(error, result); + } + else { + if (err) { + // if thmdb api connection error + const error = 'tmdb get images api connection error'; + const result = null; + cb(error, result); + } + else { + const error = null; + const result = getJson; + cb(error, result); + } + } + + }); + } + else { + const error = 'tmdb get images wrong watch parameter'; + const result = null; + cb(error, result); + } +}