bug fix and add promise feature
This commit is contained in:
201
lib/index.js
201
lib/index.js
@@ -6,6 +6,7 @@ const config = require('../config');
|
||||
// Modules
|
||||
const modules = require('./module');
|
||||
|
||||
|
||||
class FlixInfo {
|
||||
|
||||
constructor(opt = '') {
|
||||
@@ -14,10 +15,12 @@ class FlixInfo {
|
||||
this.tmdbApiKey = opt;
|
||||
}
|
||||
|
||||
getInfo(netflixId, cb) {
|
||||
|
||||
getInfo(netflixId) {
|
||||
|
||||
// Imdb id page
|
||||
const flixableURL = config.filexibleURL + netflixId;
|
||||
|
||||
// The Movie DB api page
|
||||
const theMovieDbURL = config.theMovieDbURL;
|
||||
// API Key
|
||||
@@ -25,101 +28,115 @@ class FlixInfo {
|
||||
// Result Object
|
||||
var returnResult = {};
|
||||
|
||||
request(flixableURL, (err, response, body) => {
|
||||
if (err) {
|
||||
returnResult = { error: 1, errorMsg: 'flixable.com connection error' };
|
||||
cb(returnResult)
|
||||
}
|
||||
else {
|
||||
const $ = cheerio.load(body, {
|
||||
normalizeWhitespace: true,
|
||||
xmlMode: true
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
const imdbResult = $('[class="imdbRatingPlugin"]').length;
|
||||
|
||||
if (imdbResult) {
|
||||
$('[class="imdbRatingPlugin"]').each(function (i, element) {
|
||||
|
||||
// find imdb id attribute and catch
|
||||
const imdbId = $(this).attr('data-title');
|
||||
// create (find) tmdb api url
|
||||
const tmdbURL = theMovieDbURL + 'find/' + imdbId + '?api_key=' + tmdbApiKey + '&language=en-EN&external_source=imdb_id'
|
||||
|
||||
request(tmdbURL, (err, resultBody, body) => {
|
||||
// Api find all result (for get tmdb id)
|
||||
const getJson = JSON.parse(resultBody.body);
|
||||
if (getJson.status_code > 1) {
|
||||
// if wrong api key error
|
||||
returnResult = { error: 1, errorMsg: 'tmdb find id wrong api key error' };
|
||||
cb(returnResult)
|
||||
}
|
||||
else {
|
||||
|
||||
if (err) {
|
||||
// if thmdb api connection error
|
||||
returnResult = { error: 1, errorMsg: 'tmdb find id connection api error' };
|
||||
cb(returnResult)
|
||||
}
|
||||
else {
|
||||
|
||||
// if tmdb api result not empty
|
||||
if (getJson.tv_results.length || getJson.movie_results.length) {
|
||||
|
||||
if (getJson.movie_results.length) {
|
||||
var watchParameter = 'movie';
|
||||
var thmdbId = getJson.movie_results[0].id;
|
||||
}
|
||||
else if (getJson.tv_results.length) {
|
||||
var watchParameter = 'tv';
|
||||
var thmdbId = getJson.tv_results[0].id;
|
||||
}
|
||||
// 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, watch: watchParameter, details: getDetailResult, credits: getCreditsResult, images: getImagesResult };
|
||||
cb(returnResult);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
// Return the empty result if tmdb return empty
|
||||
const returnResult = { result: 0, error: 0, msg: 'tmdb api return is empty'};
|
||||
cb(returnResult)
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
request(flixableURL, (err, response, body) => {
|
||||
if (err) {
|
||||
returnResult = { error: 1, errorMsg: 'flixable.com connection error' };
|
||||
reject(returnResult);
|
||||
}
|
||||
else {
|
||||
// Return the empty result if imdb id cannot be found
|
||||
const returnResult = { result: 0, error: 0, msg: 'imdb id cannot be found' };
|
||||
cb(returnResult)
|
||||
const $ = cheerio.load(body, {
|
||||
normalizeWhitespace: true,
|
||||
xmlMode: true
|
||||
});
|
||||
|
||||
const imdbResult = $('[class="imdbRatingPlugin"]').length;
|
||||
let netflixButton = null;
|
||||
|
||||
try {
|
||||
netflixButton = /class=\"btn btn-primary watch-on-netflix\"(.*?)/img.exec(body)[0];
|
||||
} catch (error) {
|
||||
returnResult = { error: 1, errorMsg: 'this content was not found!' };
|
||||
reject(returnResult)
|
||||
}
|
||||
|
||||
|
||||
if (netflixButton != null) {
|
||||
const imdbId = ($($('[class="imdbRatingPlugin"]')).attr('data-title'));
|
||||
|
||||
if (imdbId) {
|
||||
// create (find) tmdb api url
|
||||
const tmdbURL = theMovieDbURL + 'find/' + imdbId + '?api_key=' + tmdbApiKey + '&language=en-EN&external_source=imdb_id'
|
||||
|
||||
request(tmdbURL, (err, resultBody, body) => {
|
||||
// Api find all result (for get tmdb id)
|
||||
const getJson = JSON.parse(resultBody.body);
|
||||
if (getJson.status_code > 1) {
|
||||
// if wrong api key error
|
||||
returnResult = { error: 1, errorMsg: 'tmdb find id wrong api key error' };
|
||||
reject(returnResult)
|
||||
}
|
||||
else {
|
||||
|
||||
if (err) {
|
||||
// if thmdb api connection error
|
||||
returnResult = { error: 1, errorMsg: 'tmdb find id connection api error' };
|
||||
reject(returnResult)
|
||||
}
|
||||
else {
|
||||
|
||||
// if tmdb api result not empty
|
||||
if (getJson.tv_results.length || getJson.movie_results.length) {
|
||||
|
||||
if (getJson.movie_results.length) {
|
||||
var watchParameter = 'movie';
|
||||
var thmdbId = getJson.movie_results[0].id;
|
||||
}
|
||||
else if (getJson.tv_results.length) {
|
||||
var watchParameter = 'tv';
|
||||
var thmdbId = getJson.tv_results[0].id;
|
||||
}
|
||||
// Get detail info
|
||||
modules.getDetails(thmdbId, watchParameter, tmdbApiKey, (err, getDetailResult) => {
|
||||
if (err) {
|
||||
returnResult = { error: 1, errorMsg: err };
|
||||
reject(returnResult);
|
||||
}
|
||||
else {
|
||||
// Get credits info
|
||||
modules.getCredits(thmdbId, watchParameter, tmdbApiKey, (err, getCreditsResult) => {
|
||||
if (err) {
|
||||
returnResult = { error: 1, errorMsg: err };
|
||||
reject(returnResult);
|
||||
}
|
||||
else {
|
||||
// Get images info
|
||||
modules.getImages(thmdbId, watchParameter, tmdbApiKey, (err, getImagesResult) => {
|
||||
if (err) {
|
||||
returnResult = { error: 1, errorMsg: err };
|
||||
reject(returnResult);
|
||||
}
|
||||
else {
|
||||
returnResult = { result: 1, error: 0, watch: watchParameter, details: getDetailResult, credits: getCreditsResult, images: getImagesResult };
|
||||
resolve(returnResult);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
// Return the empty result if tmdb return empty
|
||||
const returnResult = { result: 0, error: 0, msg: 'tmdb api return is empty' };
|
||||
reject(returnResult)
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
returnResult = { error: 1, errorMsg: 'imdb id not found.' };
|
||||
reject(returnResult)
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Return the empty result if imdb id cannot be found
|
||||
const returnResult = { result: 0, error: 0, msg: 'imdb id cannot be found' };
|
||||
reject(returnResult)
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ 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);
|
||||
|
||||
Reference in New Issue
Block a user