test written
This commit is contained in:
8
config/index.js
Normal file
8
config/index.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
var config= {
|
||||||
|
// Filexible URL
|
||||||
|
filexibleURL: 'https://flixable.com/title/',
|
||||||
|
// Tmdb URL
|
||||||
|
theMovieDbURL: 'https://api.themoviedb.org/3/find/'
|
||||||
|
}
|
||||||
|
module.exports = config;
|
||||||
|
|
||||||
2
index.js
2
index.js
@@ -1 +1 @@
|
|||||||
module.exports = require('./lib/flixinfo');
|
module.exports = require('./lib');
|
||||||
104
lib/flixinfo.js
104
lib/flixinfo.js
@@ -1,104 +0,0 @@
|
|||||||
// Depend
|
|
||||||
const cheerio = require('cheerio');
|
|
||||||
const request = require('request');
|
|
||||||
|
|
||||||
class FlixInfo {
|
|
||||||
|
|
||||||
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) {
|
|
||||||
returnResult = { error: 1, errorMsg: 'flixable.com request error details: ' + err };
|
|
||||||
cb(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) {
|
|
||||||
returnResult = { error: 1, errorMsg: 'tmdb api request error details: ' + err };
|
|
||||||
cb(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, error: 0, originalName, name, year, poster, backdrop, country, rate, overviewEN };
|
|
||||||
// If there is no error return the result
|
|
||||||
cb(returnResult);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Return the error if imdb id cannot be found
|
|
||||||
const returnResult = { result: 0, error: 0 };
|
|
||||||
cb(returnResult)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
module.exports = FlixInfo;
|
|
||||||
114
lib/index.js
Normal file
114
lib/index.js
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
// Dependencies
|
||||||
|
const cheerio = require('cheerio');
|
||||||
|
const request = require('request');
|
||||||
|
// Config
|
||||||
|
const config = require('../config');
|
||||||
|
|
||||||
|
class FlixInfo {
|
||||||
|
|
||||||
|
constructor(opt = '') {
|
||||||
|
if (opt == '') throw new Error('missing tmdb api key');
|
||||||
|
|
||||||
|
this.tmdbApiKey = opt;
|
||||||
|
}
|
||||||
|
|
||||||
|
getInfo(netflixId, cb) {
|
||||||
|
|
||||||
|
// Imdb id page
|
||||||
|
const flixableURL = config.filexibleURL + netflixId;
|
||||||
|
// The Movie DB api page
|
||||||
|
const theMovieDbURL = config.theMovieDbURL;
|
||||||
|
// API Key
|
||||||
|
const tmdbApiKey = this.tmdbApiKey;
|
||||||
|
|
||||||
|
var returnResult = {};
|
||||||
|
|
||||||
|
request(flixableURL, (err, response, body) => {
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
returnResult = { error: 1, errorMsg: 'flixable.com error' };
|
||||||
|
cb(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) => {
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
// if thmdb api connection error
|
||||||
|
returnResult = { error: 1, errorMsg: 'tmdb 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 (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, error: 0, originalName, name, year, poster, backdrop, country, rate, overviewEN };
|
||||||
|
// If there is no error return the result
|
||||||
|
cb(returnResult);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Return the error if imdb id cannot be found
|
||||||
|
const returnResult = { result: 0, error: 0 };
|
||||||
|
cb(returnResult)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
module.exports = FlixInfo;
|
||||||
10
package.json
10
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "flixinfo",
|
"name": "flixinfo",
|
||||||
"version": "1.0.2",
|
"version": "1.0.3",
|
||||||
"description": "Get movie detail with netflix id",
|
"description": "Get movie detail with netflix id",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"directories": {
|
"directories": {
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
"lib": "lib"
|
"lib": "lib"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "test"
|
"test": "mocha --exit --timeout 0"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -28,6 +28,12 @@
|
|||||||
"homepage": "https://github.com/sbilketay/flixinfo#readme",
|
"homepage": "https://github.com/sbilketay/flixinfo#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cheerio": "^1.0.0-rc.2",
|
"cheerio": "^1.0.0-rc.2",
|
||||||
|
"nodemon": "^1.18.9",
|
||||||
"request": "^2.88.0"
|
"request": "^2.88.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"chai": "^4.2.0",
|
||||||
|
"mocha": "^5.2.0",
|
||||||
|
"nock": "^10.0.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
69
test/flixinfo.test.js
Normal file
69
test/flixinfo.test.js
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
const chai = require('chai');
|
||||||
|
const expect = chai.expect;
|
||||||
|
const config = require('../config');
|
||||||
|
const GetFlix = require('../index');
|
||||||
|
const nock = require('nock');
|
||||||
|
let should = chai.should();
|
||||||
|
|
||||||
|
describe('Flix Info', () => {
|
||||||
|
|
||||||
|
describe('#constructor()', () => {
|
||||||
|
|
||||||
|
context('Api Key is empty', () => {
|
||||||
|
it('- throw an error', () => {
|
||||||
|
expect(() => { new GetFlix(''); })
|
||||||
|
.to.throw('missing tmdb api key');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
context('Api Key is wrong', () => {
|
||||||
|
it('- wrong tmdb api key error', (done) => {
|
||||||
|
|
||||||
|
const flixinfoWrongApiKey = new GetFlix('sfs345k34jlkdflgkjdfglk435j345klj');
|
||||||
|
|
||||||
|
flixinfoWrongApiKey.getInfo(70143836, (result) => {
|
||||||
|
result.should.have.property('error').equal(1);
|
||||||
|
result.should.have.property('errorMsg').equal('tmdb wrong api key error');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#getInfo()', () => {
|
||||||
|
|
||||||
|
const flixinfo = new GetFlix('f29e56ff85f361ff01b5c5403a343021');
|
||||||
|
|
||||||
|
context('Return result infos', () => {
|
||||||
|
it('- return right result', (done) => {
|
||||||
|
flixinfo.getInfo(70143836, (result) => {
|
||||||
|
result.should.have.property('result').equal(1);
|
||||||
|
result.should.have.property('error').equal(0);
|
||||||
|
result.should.have.property('originalName').equal('Breaking Bad');
|
||||||
|
result.should.have.property('name').equal('Breaking Bad');
|
||||||
|
result.should.have.property('year').equal('2008-01-20');
|
||||||
|
result.should.have.property('poster').equal('https://image.tmdb.org/t/p/original//1yeVJox3rjo2jBKrrihIMj7uoS9.jpg');
|
||||||
|
result.should.have.property('backdrop').equal('https://image.tmdb.org/t/p/original//eSzpy96DwBujGFj0xMbXBcGcfxX.jpg');
|
||||||
|
result.should.have.property('country').equal('US');
|
||||||
|
result.should.have.property('rate').to.not.be.null
|
||||||
|
result.should.have.property('overviewEN').to.not.be.null
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it('- return empty result', (done) => {
|
||||||
|
flixinfo.getInfo(23424, (result) => {
|
||||||
|
result.should.have.property('error').equal(0);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it('- flixable.com and tmdb.com connection error', (done) => {
|
||||||
|
nock.disableNetConnect(); //Prevents making request external connection
|
||||||
|
flixinfo.getInfo(70143836, (result) => {
|
||||||
|
result.should.have.property('error').equal(1);
|
||||||
|
result.should.have.property('errorMsg');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user