From 20041d1d0ca18a6399e43c941176e893ac38c4c2 Mon Sep 17 00:00:00 2001 From: Ivan Borzenkov Date: Fri, 11 Feb 2022 00:58:31 +0300 Subject: [PATCH] requests to tmdb api async by fetch --- src/app/lib/views/item.js | 21 ++-------- src/app/lib/views/movie_detail.js | 70 +++++++------------------------ src/app/lib/views/show_detail.js | 19 ++------- 3 files changed, 23 insertions(+), 87 deletions(-) diff --git a/src/app/lib/views/item.js b/src/app/lib/views/item.js index 2ce504f773..000fe5bf43 100644 --- a/src/app/lib/views/item.js +++ b/src/app/lib/views/item.js @@ -169,7 +169,7 @@ } }, - loadImage: function () { + loadImage: async function () { var noimg = 'images/posterholder.png'; var poster = this.model.get('image'); if (!poster && this.model.get('images') && this.model.get('images').poster){ @@ -178,22 +178,9 @@ poster = this.model.get('poster'); } else { var imdb = this.model.get('imdb_id'), - api_key = Settings.tmdb.api_key, - movie = (function () { - var tmp = null; - $.ajax({ - url: 'http://api.themoviedb.org/3/movie/' + imdb + '?api_key=' + api_key + '&append_to_response=videos', - type: 'get', - dataType: 'json', - timeout: 5000, - async: false, - global: false, - success: function (data) { - tmp = data; - } - }); - return tmp; - }()); + api_key = Settings.tmdb.api_key; + const response = await fetch('http://api.themoviedb.org/3/movie/' + imdb + '?api_key=' + api_key + '&append_to_response=videos'); + const movie = await response.json(); poster = movie && movie.poster_path ? 'http://image.tmdb.org/t/p/w500' + movie.poster_path : noimg; this.model.set('poster', poster); !this.model.get('synopsis') && movie && movie.overview ? this.model.set('synopsis', movie.overview) : null; diff --git a/src/app/lib/views/movie_detail.js b/src/app/lib/views/movie_detail.js index 2144d19523..11bac79013 100644 --- a/src/app/lib/views/movie_detail.js +++ b/src/app/lib/views/movie_detail.js @@ -125,10 +125,6 @@ this.initKeyboardShortcuts(); healthButton.render(); - if (curSynopsis.vstatus !== null && curSynopsis.cast === '') { - this.showCast(); - } - $('[data-toggle="tooltip"]').tooltip({ html: true }); @@ -214,26 +210,14 @@ } }, - getMetaData: function () { + getMetaData: async function () { curSynopsis.vstatus = false; var imdb = this.model.get('imdb_id'), - api_key = Settings.tmdb.api_key, - lang = Settings.language, - movie = (function () { - var tmp = null; - $.ajax({ - url: 'http://api.themoviedb.org/3/movie/' + imdb + '?api_key=' + api_key + '&language=' + lang + '&append_to_response=videos,credits', - type: 'get', - dataType: 'json', - timeout: 5000, - async: false, - global: false, - success: function (data) { - tmp = data; - } - }); - return tmp; - }()); + api_key = Settings.tmdb.api_key, + lang = Settings.language; + const response = await fetch('http://api.themoviedb.org/3/movie/' + imdb + '?api_key=' + api_key + '&language=' + lang + '&append_to_response=videos,credits'); + const movie = await response.json(); + (!this.model.get('synopsis') || (Settings.translateSynopsis && Settings.language !== 'en')) && movie && movie.overview ? this.model.set('synopsis', movie.overview) : null; (!this.model.get('rating') || this.model.get('rating') === '0' || this.model.get('rating') === '0.0') && movie && movie.vote_average ? this.model.set('rating', movie.vote_average) : null; (!this.model.get('runtime') || this.model.get('runtime') === '0') && movie && movie.runtime ? this.model.set('runtime', movie.runtime) : null; @@ -249,22 +233,13 @@ } // Fallback to english when source and TMDb call in default language that is other than english fail to fetch synopsis if (!this.model.get('synopsis') && Settings.language !== 'en') { - movie = (function () { - var tmp = null; - $.ajax({ - url: 'http://api.themoviedb.org/3/movie/' + imdb + '?api_key=' + api_key, - type: 'get', - dataType: 'json', - timeout: 5000, - async: false, - global: false, - success: function (data) { - tmp = data; - } - }); - return tmp; - }()); - movie && movie.overview ? this.model.set('synopsis', movie.overview) : null; + const responseEn = await fetch('http://api.themoviedb.org/3/movie/' + imdb + '?api_key=' + api_key); + const movieEn = await responseEn.json(); + movieEn && movieEn.overview ? this.model.set('synopsis', movieEn.overview) : null; + } + + if (curSynopsis.vstatus === false && curSynopsis.cast === '') { + this.showCast(); } }, @@ -400,27 +375,14 @@ } }, - openTmdb: function(e) { + openTmdb: async function(e) { let imdb = this.model.get('imdb_id'), tmdb = this.model.get('tmdb_id'), api_key = Settings.tmdb.api_key; if (!tmdb && !this.model.get('getmetarunned')) { - let movie = (function () { - let tmp = null; - $.ajax({ - url: 'http://api.themoviedb.org/3/find/' + imdb + '?api_key=' + api_key + '&external_source=imdb_id', - type: 'get', - dataType: 'json', - timeout: 5000, - async: false, - global: false, - success: function (data) { - tmp = data; - } - }); - return tmp; - }()); + const response = await fetch('http://api.themoviedb.org/3/movie/' + imdb + '?api_key=' + api_key + '&external_source=imdb_id'); + const movie = await response.json(); movie && movie.movie_results && movie.movie_results[0] && movie.movie_results[0].id ? this.model.set('tmdb_id', movie.movie_results[0].id) : null; tmdb = this.model.get('tmdb_id'); } diff --git a/src/app/lib/views/show_detail.js b/src/app/lib/views/show_detail.js index b7abfa14c3..e9f2a94ac4 100644 --- a/src/app/lib/views/show_detail.js +++ b/src/app/lib/views/show_detail.js @@ -417,27 +417,14 @@ } }, - openTmdb: function(e) { + openTmdb: async function(e) { let imdb = this.model.get('imdb_id'), tmdb = this.model.get('tmdb_id'), api_key = Settings.tmdb.api_key; if (!tmdb) { - let show = (function () { - let tmp = null; - $.ajax({ - url: 'http://api.themoviedb.org/3/find/' + imdb + '?api_key=' + api_key + '&external_source=imdb_id', - type: 'get', - dataType: 'json', - timeout: 5000, - async: false, - global: false, - success: function (data) { - tmp = data; - } - }); - return tmp; - }()); + const response = await fetch('http://api.themoviedb.org/3/movie/' + imdb + '?api_key=' + api_key + '&external_source=imdb_id'); + const show = await response.json(); show && show.tv_results && show.tv_results[0] && show.tv_results[0].id ? this.model.set('tmdb_id', show.tv_results[0].id) : null; tmdb = this.model.get('tmdb_id'); }