bug fix and add promise feature
This commit is contained in:
49
lib/index.js
49
lib/index.js
@@ -6,6 +6,7 @@ const config = require('../config');
|
|||||||
// Modules
|
// Modules
|
||||||
const modules = require('./module');
|
const modules = require('./module');
|
||||||
|
|
||||||
|
|
||||||
class FlixInfo {
|
class FlixInfo {
|
||||||
|
|
||||||
constructor(opt = '') {
|
constructor(opt = '') {
|
||||||
@@ -14,10 +15,12 @@ class FlixInfo {
|
|||||||
this.tmdbApiKey = opt;
|
this.tmdbApiKey = opt;
|
||||||
}
|
}
|
||||||
|
|
||||||
getInfo(netflixId, cb) {
|
|
||||||
|
getInfo(netflixId) {
|
||||||
|
|
||||||
// Imdb id page
|
// Imdb id page
|
||||||
const flixableURL = config.filexibleURL + netflixId;
|
const flixableURL = config.filexibleURL + netflixId;
|
||||||
|
|
||||||
// The Movie DB api page
|
// The Movie DB api page
|
||||||
const theMovieDbURL = config.theMovieDbURL;
|
const theMovieDbURL = config.theMovieDbURL;
|
||||||
// API Key
|
// API Key
|
||||||
@@ -25,10 +28,12 @@ class FlixInfo {
|
|||||||
// Result Object
|
// Result Object
|
||||||
var returnResult = {};
|
var returnResult = {};
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
request(flixableURL, (err, response, body) => {
|
request(flixableURL, (err, response, body) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
returnResult = { error: 1, errorMsg: 'flixable.com connection error' };
|
returnResult = { error: 1, errorMsg: 'flixable.com connection error' };
|
||||||
cb(returnResult)
|
reject(returnResult);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const $ = cheerio.load(body, {
|
const $ = cheerio.load(body, {
|
||||||
@@ -37,12 +42,20 @@ class FlixInfo {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const imdbResult = $('[class="imdbRatingPlugin"]').length;
|
const imdbResult = $('[class="imdbRatingPlugin"]').length;
|
||||||
|
let netflixButton = null;
|
||||||
|
|
||||||
if (imdbResult) {
|
try {
|
||||||
$('[class="imdbRatingPlugin"]').each(function (i, element) {
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
// find imdb id attribute and catch
|
|
||||||
const imdbId = $(this).attr('data-title');
|
if (netflixButton != null) {
|
||||||
|
const imdbId = ($($('[class="imdbRatingPlugin"]')).attr('data-title'));
|
||||||
|
|
||||||
|
if (imdbId) {
|
||||||
// create (find) tmdb api url
|
// create (find) tmdb api url
|
||||||
const tmdbURL = theMovieDbURL + 'find/' + 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'
|
||||||
|
|
||||||
@@ -52,14 +65,14 @@ class FlixInfo {
|
|||||||
if (getJson.status_code > 1) {
|
if (getJson.status_code > 1) {
|
||||||
// if wrong api key error
|
// if wrong api key error
|
||||||
returnResult = { error: 1, errorMsg: 'tmdb find id wrong api key error' };
|
returnResult = { error: 1, errorMsg: 'tmdb find id wrong api key error' };
|
||||||
cb(returnResult)
|
reject(returnResult)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
// if thmdb api connection error
|
// if thmdb api connection error
|
||||||
returnResult = { error: 1, errorMsg: 'tmdb find id connection api error' };
|
returnResult = { error: 1, errorMsg: 'tmdb find id connection api error' };
|
||||||
cb(returnResult)
|
reject(returnResult)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
||||||
@@ -78,25 +91,25 @@ class FlixInfo {
|
|||||||
modules.getDetails(thmdbId, watchParameter, tmdbApiKey, (err, getDetailResult) => {
|
modules.getDetails(thmdbId, watchParameter, tmdbApiKey, (err, getDetailResult) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
returnResult = { error: 1, errorMsg: err };
|
returnResult = { error: 1, errorMsg: err };
|
||||||
cb(returnResult);
|
reject(returnResult);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Get credits info
|
// Get credits info
|
||||||
modules.getCredits(thmdbId, watchParameter, tmdbApiKey, (err, getCreditsResult) => {
|
modules.getCredits(thmdbId, watchParameter, tmdbApiKey, (err, getCreditsResult) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
returnResult = { error: 1, errorMsg: err };
|
returnResult = { error: 1, errorMsg: err };
|
||||||
cb(returnResult);
|
reject(returnResult);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Get images info
|
// Get images info
|
||||||
modules.getImages(thmdbId, watchParameter, tmdbApiKey, (err, getImagesResult) => {
|
modules.getImages(thmdbId, watchParameter, tmdbApiKey, (err, getImagesResult) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
returnResult = { error: 1, errorMsg: err };
|
returnResult = { error: 1, errorMsg: err };
|
||||||
cb(returnResult);
|
reject(returnResult);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
returnResult = { result: 1, error: 0, watch: watchParameter, details: getDetailResult, credits: getCreditsResult, images: getImagesResult };
|
returnResult = { result: 1, error: 0, watch: watchParameter, details: getDetailResult, credits: getCreditsResult, images: getImagesResult };
|
||||||
cb(returnResult);
|
resolve(returnResult);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -106,21 +119,25 @@ class FlixInfo {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Return the empty result if tmdb return empty
|
// Return the empty result if tmdb return empty
|
||||||
const returnResult = { result: 0, error: 0, msg: 'tmdb api return is empty'};
|
const returnResult = { result: 0, error: 0, msg: 'tmdb api return is empty' };
|
||||||
cb(returnResult)
|
reject(returnResult)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
} else {
|
||||||
|
returnResult = { error: 1, errorMsg: 'imdb id not found.' };
|
||||||
|
reject(returnResult)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Return the empty result if imdb id cannot be found
|
// Return the empty result if imdb id cannot be found
|
||||||
const returnResult = { result: 0, error: 0, msg: 'imdb id cannot be found' };
|
const returnResult = { result: 0, error: 0, msg: 'imdb id cannot be found' };
|
||||||
cb(returnResult)
|
reject(returnResult)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
module.exports = FlixInfo;
|
module.exports = FlixInfo;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "flixinfo",
|
"name": "flixinfo",
|
||||||
"version": "1.0.4",
|
"version": "1.1.0",
|
||||||
"description": "Get movie detail with netflix id",
|
"description": "Get movie detail with netflix id",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"directories": {
|
"directories": {
|
||||||
@@ -27,8 +27,9 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/sbilketay/flixinfo#readme",
|
"homepage": "https://github.com/sbilketay/flixinfo#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"chai-as-promised": "^7.1.1",
|
||||||
"cheerio": "^1.0.0-rc.2",
|
"cheerio": "^1.0.0-rc.2",
|
||||||
"nodemon": "^1.18.9",
|
"nodemon": "^1.19.4",
|
||||||
"request": "^2.88.0"
|
"request": "^2.88.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -21,65 +21,71 @@ describe('Flix Info', () => {
|
|||||||
|
|
||||||
const flixinfoWrongApiKey = new GetFlix('sfs345k34jlkdflgkjdfglk435j345klj');
|
const flixinfoWrongApiKey = new GetFlix('sfs345k34jlkdflgkjdfglk435j345klj');
|
||||||
|
|
||||||
flixinfoWrongApiKey.getInfo(70143836, (result) => {
|
flixinfoWrongApiKey.getInfo(70143836)
|
||||||
|
.catch(result => {
|
||||||
result.should.have.property('error').equal(1);
|
result.should.have.property('error').equal(1);
|
||||||
result.should.have.property('errorMsg').equal('tmdb find id wrong api key error');
|
result.should.have.property('errorMsg').equal('tmdb find id wrong api key error');
|
||||||
done();
|
done();
|
||||||
});
|
})
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#getInfo()', () => {
|
describe('#getInfo()', () => {
|
||||||
|
|
||||||
const flixinfo = new GetFlix('f29e56ff85f361ff01b5c5403a343021');
|
const flixinfo = new GetFlix('d9d6007d1bcf12043db5a085ae3e5bbb');
|
||||||
|
|
||||||
context('Return result infos', () => {
|
context('Return result infos', () => {
|
||||||
// Movie test
|
// Movie test
|
||||||
it('- movies return right result', (done) => {
|
it('- movies return right result', (done) => {
|
||||||
flixinfo.getInfo(60033299, (result) => {
|
flixinfo.getInfo(70131314)
|
||||||
|
.then(result => {
|
||||||
result.should.have.property('result').equal(1);
|
result.should.have.property('result').equal(1);
|
||||||
result.should.have.property('error').equal(0);
|
result.should.have.property('error').equal(0);
|
||||||
result.should.have.property('details').have.property('original_title').equal('The Butterfly Effect');
|
result.should.have.property('details').have.property('original_title').equal('Inception');
|
||||||
result.should.have.property('details').have.property('title').equal('The Butterfly Effect');
|
result.should.have.property('details').have.property('title').equal('Inception');
|
||||||
result.should.have.property('details').have.property('release_date').equal('2004-01-22');
|
result.should.have.property('details').have.property('release_date').equal('2010-07-15');
|
||||||
result.should.have.property('details').have.property('poster_path').equal('/3PAQy3CyNNJPES772OFMx47lFEE.jpg');
|
result.should.have.property('details').have.property('poster_path').equal('/qmDpIHrmpJINaRKAfWQfftjCdyi.jpg');
|
||||||
result.should.have.property('details').have.property('backdrop_path').equal('/zXTUrm0BIrrZn3nEhybg0hlY275.jpg');
|
result.should.have.property('details').have.property('backdrop_path').equal('/s2bT29y0ngXxxu2IA8AOzzXTRhd.jpg');
|
||||||
result.should.have.property('details').have.property('id').equal(1954);
|
result.should.have.property('details').have.property('id').equal(27205);
|
||||||
result.should.have.property('details').have.property('imdb_id').equal('tt0289879');
|
result.should.have.property('details').have.property('imdb_id').equal('tt1375666');
|
||||||
result.should.have.property('details').have.property('original_language').equal('en');
|
result.should.have.property('details').have.property('original_language').equal('en');
|
||||||
result.should.have.property('credits').have.property('id').equal(1954);
|
result.should.have.property('credits').have.property('id').equal(27205);
|
||||||
result.should.have.property('images').have.property('id').equal(1954);
|
result.should.have.property('images').have.property('id').equal(27205);
|
||||||
done();
|
done();
|
||||||
|
})
|
||||||
});
|
});
|
||||||
});
|
// // Tv test
|
||||||
// Tv test
|
|
||||||
it('- tv series return right result', (done) => {
|
it('- tv series return right result', (done) => {
|
||||||
flixinfo.getInfo(80099656, (result) => {
|
flixinfo.getInfo(80232926)
|
||||||
|
.then(result => {
|
||||||
result.should.have.property('result').equal(1);
|
result.should.have.property('result').equal(1);
|
||||||
result.should.have.property('error').equal(0);
|
result.should.have.property('error').equal(0);
|
||||||
result.should.have.property('details').have.property('original_name').equal('Frontier');
|
result.should.have.property('details').have.property('original_name').equal('Ragnarok');
|
||||||
result.should.have.property('details').have.property('name').equal('Frontier');
|
result.should.have.property('details').have.property('name').equal('Ragnarok');
|
||||||
result.should.have.property('details').have.property('first_air_date').equal('2016-11-06');
|
result.should.have.property('details').have.property('first_air_date').equal('2020-01-31');
|
||||||
result.should.have.property('details').have.property('poster_path').equal('/mhIeCeYgG4WJHNzaw2EyRpvmGX8.jpg');
|
result.should.have.property('details').have.property('poster_path').equal('/bSXE4qqdWWFF903FmxSdKtVRm7t.jpg');
|
||||||
result.should.have.property('details').have.property('backdrop_path').equal('/hDYEGNX6CADoCqhyBbe2Qu7rKVG.jpg');
|
result.should.have.property('details').have.property('backdrop_path').equal('/jM7LHr811U4A6EnY9iMyKhUVsMN.jpg');
|
||||||
result.should.have.property('details').have.property('id').equal(64555);
|
result.should.have.property('details').have.property('id').equal(91557);
|
||||||
result.should.have.property('details').have.property('original_language').equal('en');
|
result.should.have.property('details').have.property('original_language').equal('no');
|
||||||
result.should.have.property('credits').have.property('id').equal(64555);
|
result.should.have.property('credits').have.property('id').equal(91557);
|
||||||
result.should.have.property('images').have.property('id').equal(64555);
|
result.should.have.property('images').have.property('id').equal(91557);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('- return empty result', (done) => {
|
it('- return empty result', (done) => {
|
||||||
flixinfo.getInfo(23424, (result) => {
|
flixinfo.getInfo(70143836)
|
||||||
|
.then(result => {
|
||||||
result.should.have.property('error').equal(0);
|
result.should.have.property('error').equal(0);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('- flixable.com and tmdb.com connection error', (done) => {
|
it('- flixable.com and tmdb.com connection error', (done) => {
|
||||||
nock.disableNetConnect(); //Prevents making request external connection
|
nock.disableNetConnect(); //Prevents making request external connection
|
||||||
flixinfo.getInfo(70143836, (result) => {
|
flixinfo.getInfo(70143836)
|
||||||
|
.catch(result => {
|
||||||
result.should.have.property('error').equal(1);
|
result.should.have.property('error').equal(1);
|
||||||
result.should.have.property('errorMsg');
|
result.should.have.property('errorMsg');
|
||||||
done();
|
done();
|
||||||
|
|||||||
Reference in New Issue
Block a user