From a6ba96aa1f952ef9b66d120f37bd2619ab3703ce Mon Sep 17 00:00:00 2001 From: Wise Colt Date: Fri, 1 May 2020 02:18:21 +0300 Subject: [PATCH] add google search --- example/getinfo.js | 2 +- lib/index.js | 73 +++++++++++++------------------------ lib/module.js | 89 ++++++++++++++++++++++++++++++++++++++++++++-- package.json | 2 +- 4 files changed, 112 insertions(+), 54 deletions(-) diff --git a/example/getinfo.js b/example/getinfo.js index fca9e68..afc0708 100644 --- a/example/getinfo.js +++ b/example/getinfo.js @@ -3,7 +3,7 @@ const getflix = new GetFlix('d9d6007d1bcf12043db5a085ae3e5bbb'); getInfo = async () => { try { - const info = await getflix.getNetflixInfo(81082225, 'en-EN', 'en'); + const info = await getflix.getNetflixInfo(81151514, 'en-EN', 'tr'); console.log(info); } catch (error) { console.log(error); diff --git a/lib/index.js b/lib/index.js index 3c3cab3..ba26fc7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,37 +6,6 @@ const config = require('../config'); // Modules const modules = require('./module'); -process.setMaxListeners(0) - -// imdb.com adresine bağlanarak imdb id'sinin geçerliliğini sorgulamayı sağlayan method -const imdbIdReal = async (flixableFindImdbId) => { - - const imdbIdURL = "https://www.imdb.com/title/" + flixableFindImdbId - - return new Promise((resolve, reject) => { - request(imdbIdURL, (error, response, body) => { - if (error) { - reject('imdb id alinirken hata olustu') - } else { - const $ = cheerio.load(body, { - normalizeWhitespace: true, - xmlMode: true - }); - const imdbId = ($($('[property="pageId"]')).attr('content')); - const imdbRate = /(.*?)<\/span>/img.exec(body)[1] - if (imdbId == undefined) { - reject('imdb id bulunamadi') - } else { - resolve({ - id: imdbId, - rate: imdbRate - }) - } - } - }) - }) -} - class FlixInfo { /** @@ -140,7 +109,7 @@ class FlixInfo { if (theMovieDbLanguage) { // Sayfada imdb id varsa, tmdb üzerinden imdb id'ye göre arama yapılır if (findFlixableImdbId) { - const imdb = await imdbIdReal(findFlixableImdbId) + const imdb = await modules.getRealImdbId(findFlixableImdbId) imdbId = imdb.id imdbRate = imdb.rate tmdbURL = theMovieDbURL + 'find/' + imdbId + '?api_key=' + tmdbApiKey + '&language=' + theMovieDbLanguage + '&external_source=imdb_id'; @@ -149,7 +118,7 @@ class FlixInfo { // Sayfada imdb id yoksa, tmdb üzerinden title ve year'a göre arama yapılır tmdbURL = theMovieDbURL + 'search/' + type + '?api_key=' + tmdbApiKey + '&language=' + theMovieDbLanguage + '&query=' + netflixTitle + '&first_air_date_year=' + netflixYear; } - + request(encodeURI(tmdbURL), async (err, resultBody, body) => { if (err) { @@ -171,7 +140,6 @@ class FlixInfo { if (imdbId) { result = type == 'movie' ? getJson.movie_results : getJson.tv_results - } else { result = getJson.results } @@ -179,17 +147,11 @@ class FlixInfo { // if tmdb api result not empty if (result.length) { - const thmdbId = result[0].id; + const tmdbId = result[0].id; try { - // Get detail info - const getDetailResult = await modules.getTmdbDetails('details', thmdbId, type, tmdbApiKey, theMovieDbLanguage) - // Get credits info - const getCreditsResult = await modules.getTmdbDetails('credits', thmdbId, type, tmdbApiKey) - // Get images info - const getImagesResult = await modules.getTmdbDetails('images', thmdbId, type, tmdbApiKey) - // Get trailers - const getTrailersResult = await modules.getTmdbDetails('trailers', thmdbId, type, tmdbApiKey) + // set tmdb info + const { google_search, details, credits, images, trailers } = await modules.setTmdbAllInfos(false, tmdbId, type, tmdbApiKey, theMovieDbLanguage) // Result Object result = { @@ -198,7 +160,7 @@ class FlixInfo { watch: type, imdb: { // imdb üzerinden id alınamadıysa tmdb üzerinden alınmaya çalışılır. tmdb'de de yoksa null döner - id: (imdbId != null ? imdbId : (getDetailResult.imdb_id != undefined ? getDetailResult.imdb_id : null)), + id: (imdbId != null ? imdbId : (details.imdb_id != undefined ? details.imdb_id : null)), rate: imdbRate }, netflix: { @@ -213,10 +175,11 @@ class FlixInfo { cast: netflixCast }, tmdb: { - details: getDetailResult, - credits: getCreditsResult, - images: getImagesResult, - trailers: getTrailersResult + google_search, + details, + credits, + images, + trailers } }; resolve(result); @@ -225,6 +188,18 @@ class FlixInfo { } } else { + let tmdbInfo = null + // Imdb id verisi yoksa (title ve year bilgisine göre arama yapılmışsa) ve Oyuncu bilgisi varsa google ile arama yap + // Oyuncu bilgisi yoksa arama yapılmaz ve tmdb null olarak döner + if (!findFlixableImdbId && netflixCast != []) { + try { + // google arama sonucu veri döndüyse tmdb infoyu doldur + tmdbInfo = await modules.searchGoogleForTmdbId(type, netflixTitle, netflixYear, netflixCast, theMovieDbLanguage, tmdbApiKey) + } catch (error) { + // console.log('Google Search Warning: ' + error); + } + } + // Tmdb verisi yoksa sadece netflix verisi döndür result = { result: 1, @@ -244,7 +219,7 @@ class FlixInfo { genres: netflixGenres, cast: netflixCast }, - tmdb: null + tmdb: tmdbInfo }; resolve(result); } diff --git a/lib/module.js b/lib/module.js index 93f27ff..3b5ce57 100644 --- a/lib/module.js +++ b/lib/module.js @@ -1,7 +1,40 @@ -const config = require('../config'); +// Dependencies +const cheerio = require('cheerio'); const request = require('request'); +// Configs +const config = require('../config'); -module.exports.getTmdbDetails = function (getParamaters, thmdbId, type, tmdbApiKey, language) { +// imdb.com adresine bağlanarak imdb id'sinin geçerliliğini sorgulamayı sağlayan method +const getRealImdbId = (flixableFindImdbId) => { + + const imdbIdURL = "https://www.imdb.com/title/" + flixableFindImdbId + + return new Promise((resolve, reject) => { + request(imdbIdURL, (error, response, body) => { + if (error) { + reject('imdb id alinirken hata olustu') + } else { + const $ = cheerio.load(body, { + normalizeWhitespace: true, + xmlMode: true + }); + const imdbId = ($($('[property="pageId"]')).attr('content')); + const imdbRate = /(.*?)<\/span>/img.exec(body)[1] + if (imdbId == undefined) { + reject('imdb id bulunamadi') + } else { + resolve({ + id: imdbId, + rate: imdbRate + }) + } + } + }) + }) +} + +// The Movie DB Api'ye istek atar +const getTmdbDetails = (getParamaters, thmdbId, type, tmdbApiKey, language) => { return new Promise((resolve, reject) => { if (type == 'movie' || type == 'tv') { @@ -51,4 +84,54 @@ module.exports.getTmdbDetails = function (getParamaters, thmdbId, type, tmdbApiK reject('tmdb get detail wrong watch parameter'); } }) -} \ No newline at end of file +} + +const setTmdbAllInfos = async (googleSearch, tmdbId, type, tmdbApiKey, theMovieDbLanguage) => { + // Get detail info + const details = await getTmdbDetails('details', tmdbId, type, tmdbApiKey, theMovieDbLanguage) + // Get credits info + const credits = await getTmdbDetails('credits', tmdbId, type, tmdbApiKey) + // Get images info + const images = await getTmdbDetails('images', tmdbId, type, tmdbApiKey) + // Get trailers + const trailers = await getTmdbDetails('trailers', tmdbId, type, tmdbApiKey) + + return { google_search: googleSearch, details, credits, images, trailers } +} + +// Tmdb Api'de title ve year bilgilerine göre yapılan aramada sonuç bulunamazsa, netflixTitle, netflixYear, netflixCast +// bilgileri ile google'da arama yapar. Bu arama sonucunda tmdb id bilgisine ulaşır. +const searchGoogleForTmdbId = (type, netflixTitle, netflixYear, netflixCast, theMovieDbLanguage, tmdbApiKey) => { + + return new Promise((resolve, reject) => { + + const searchUrl = "https://www.google.com/search?q=" + netflixTitle + " " + netflixYear + " " + netflixCast[0] + " " + "themoviedb.org&ie=UTF-8" + + request(encodeURI(searchUrl), async (error, response, body) => { + + if (body) { + const $ = cheerio.load(body, { + normalizeWhitespace: true, + xmlMode: true + }); + + try { + + const tmdbIdFind = (/\/url\?q\=https\:\/\/www.themoviedb.org\/(.*?)&/img.exec(body)[1]).split("/")[1] + const tmdbId = /\d+/g.exec(tmdbIdFind)[0] + + // tmdb info ayarlandı + resolve(await setTmdbAllInfos(true, tmdbId, type, tmdbApiKey, theMovieDbLanguage)) + + } catch (error) { + reject('themoviedb id not found') + } + } else { + // google arama sonucu hatalıysa + reject('themoviedb id not found') + } + }) + }) +} + +module.exports = { getTmdbDetails, getRealImdbId, setTmdbAllInfos, searchGoogleForTmdbId } \ No newline at end of file diff --git a/package.json b/package.json index bc5f79d..69a0d0d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "flixinfo", - "version": "1.3.0", + "version": "1.4.0", "description": "Get movie detail with netflix id", "main": "index.js", "directories": {