add google search

This commit is contained in:
Wise Colt
2020-05-01 02:18:21 +03:00
parent fd58d43568
commit a6ba96aa1f
4 changed files with 112 additions and 54 deletions

View File

@@ -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);

View File

@@ -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 itemprop="ratingValue">(.*?)<\/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);
}

View File

@@ -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 itemprop="ratingValue">(.*?)<\/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');
}
})
}
}
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 }

View File

@@ -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": {