Skip to content

Commit

Permalink
requests to tmdb api async by fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan1986 committed Feb 19, 2024
1 parent dee27a9 commit e55c282
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 87 deletions.
21 changes: 4 additions & 17 deletions src/app/lib/views/browser/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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;
Expand Down
70 changes: 16 additions & 54 deletions src/app/lib/views/movie_detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@
this.initKeyboardShortcuts();
healthButton.render();

if (curSynopsis.vstatus !== null && curSynopsis.cast === '') {
this.showCast();
}

$('[data-toggle="tooltip"]').tooltip({
html: true
});
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
},

Expand Down Expand Up @@ -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');
}
Expand Down
19 changes: 3 additions & 16 deletions src/app/lib/views/show_detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,27 +415,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');
}
Expand Down

0 comments on commit e55c282

Please sign in to comment.