From 9c19db066275f0f202c8f7d49eb2f4ee2619afe6 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/browser/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/browser/item.js b/src/app/lib/views/browser/item.js index 28db599cea..5d45732efb 100644 --- a/src/app/lib/views/browser/item.js +++ b/src/app/lib/views/browser/item.js @@ -165,7 +165,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){ @@ -174,22 +174,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 95ae3706c8..9a5467ecf3 100644 --- a/src/app/lib/views/movie_detail.js +++ b/src/app/lib/views/movie_detail.js @@ -127,10 +127,6 @@ this.initKeyboardShortcuts(); healthButton.render(); - if (curSynopsis.vstatus !== null && curSynopsis.cast === '') { - this.showCast(); - } - $('[data-toggle="tooltip"]').tooltip({ html: true }); @@ -216,26 +212,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; @@ -251,22 +235,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(); } }, @@ -402,27 +377,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 cc11454072..4e628f47a8 100644 --- a/src/app/lib/views/show_detail.js +++ b/src/app/lib/views/show_detail.js @@ -414,27 +414,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'); }