summaryrefslogtreecommitdiffstats
path: root/lib/helpers.js
diff options
context:
space:
mode:
authorphenomenz <david@reallysuccessful.com>2014-07-07 11:42:40 -0400
committerphenomenz <david@reallysuccessful.com>2014-07-07 11:42:40 -0400
commit6c9c8cbde8daf4d014a27f39f014e7e8b90ec47c (patch)
tree6ea3d98b32c0d6e9fe0bd6637bbc6719abe9f4ec /lib/helpers.js
parentc25fc3e1ba68f175a205a34f8f534837bd4d8045 (diff)
downloadpopcorn-api-6c9c8cbde8daf4d014a27f39f014e7e8b90ec47c.zip
popcorn-api-6c9c8cbde8daf4d014a27f39f014e7e8b90ec47c.tar.gz
popcorn-api-6c9c8cbde8daf4d014a27f39f014e7e8b90ec47c.tar.bz2
Adding error catch for trakt
Diffstat (limited to 'lib/helpers.js')
-rwxr-xr-xlib/helpers.js136
1 files changed, 75 insertions, 61 deletions
diff --git a/lib/helpers.js b/lib/helpers.js
index 6af825c..1559cf1 100755
--- a/lib/helpers.js
+++ b/lib/helpers.js
@@ -121,59 +121,68 @@ var helpers = {
});
}
else {
- trakt.request('show', 'summary', {title: imdb, extended: "full"}, function(err, show) {
- if(!show) return callback(null, show);
- else {
- var seasons = show.seasons;
- async.each(seasons, function(season, cbs) {
- var episodes = season.episodes;
- async.each(episodes, function(episode, cbe){
- var aired = episode.first_aired_iso;
- if(aired){
- var year_aired = aired.substring(0, aired.indexOf("-"));
- var date_aired = aired.substring(aired.indexOf("-") + 1, aired.indexOf("T")).replace("-", "/");
- if(typeof(data[year_aired]) != 'undefined' && typeof(data[year_aired][date_aired]) != 'undefined')
- {
- thisEpisode = {
- tvdb_id: episode.tvdb_id,
- season: episode.season,
- episode: episode.episode,
- title: episode.title,
- overview: episode.overview,
- first_aired: episode.first_aired_utc,
- date_based: true,
- year: year_aired,
- day_aired: date_aired,
- watched : {watched: false},
- torrents: []
- };
- thisEpisode.torrents = data[year_aired][date_aired];
- thisEpisode.torrents[0] = data[year_aired][date_aired]["480p"] ? data[year_aired][date_aired]["480p"] : data[year_aired][date_aired]["720p"]; // Prevents breaking the app
- thisEpisodes.push(thisEpisode);
+
+ try {
+
+ trakt.request('show', 'summary', {title: imdb, extended: "full"}, function(err, show) {
+ if(!show) return callback(null, show);
+ else {
+ var seasons = show.seasons;
+ async.each(seasons, function(season, cbs) {
+ var episodes = season.episodes;
+ async.each(episodes, function(episode, cbe){
+ var aired = episode.first_aired_iso;
+ if(aired){
+ var year_aired = aired.substring(0, aired.indexOf("-"));
+ var date_aired = aired.substring(aired.indexOf("-") + 1, aired.indexOf("T")).replace("-", "/");
+ if(typeof(data[year_aired]) != 'undefined' && typeof(data[year_aired][date_aired]) != 'undefined')
+ {
+ thisEpisode = {
+ tvdb_id: episode.tvdb_id,
+ season: episode.season,
+ episode: episode.episode,
+ title: episode.title,
+ overview: episode.overview,
+ first_aired: episode.first_aired_utc,
+ date_based: true,
+ year: year_aired,
+ day_aired: date_aired,
+ watched : {watched: false},
+ torrents: []
+ };
+ thisEpisode.torrents = data[year_aired][date_aired];
+ thisEpisode.torrents[0] = data[year_aired][date_aired]["480p"] ? data[year_aired][date_aired]["480p"] : data[year_aired][date_aired]["720p"]; // Prevents breaking the app
+ thisEpisodes.push(thisEpisode);
+ }
}
- }
- return cbe();
- },
- function(err, res) {return cbs()})
- }, function(err, res) {
- // Only change "lastUpdated" date if there are new episodes
- Show.find({imdb_id: imdb}, function(err, show) {
- if(err) return callback(err, null);
- if(show.episodes != thisEpisodes) {
- Show.update({ imdb_id: imdb },
- { $set: { episodes: thisEpisodes, last_updated: +new Date(), num_seasons: numSeasons}},
- function(err, show) {
- return callback(err, null);
- });
- }
- else {
- return callback(null, show);
- }
- });
+ return cbe();
+ },
+ function(err, res) {return cbs()})
+ }, function(err, res) {
+ // Only change "lastUpdated" date if there are new episodes
+ Show.find({imdb_id: imdb}, function(err, show) {
+ if(err) return callback(err, null);
+ if(show.episodes != thisEpisodes) {
+ Show.update({ imdb_id: imdb },
+ { $set: { episodes: thisEpisodes, last_updated: +new Date(), num_seasons: numSeasons}},
+ function(err, show) {
+ return callback(err, null);
+ });
+ }
+ else {
+ return callback(null, show);
+ }
+ });
+
+ });
+ }
+ });
+
+ } catch (err) {
+ console.log("Error:", err);
+ return callback(err, null);
+ }
- });
- }
- });
}
});
@@ -261,17 +270,22 @@ var helpers = {
console.log("TTL expired, updating info");
show.imdb_id = doc.imdb_id;
//TODO: Change this to just get new rating or something
- trakt.request('show', 'summary', {title: slug}, function(err, data) {
- if (!err && data) {
- Show.update({ _id: doc._id },
- { $set: { rating: data.ratings, status: data.status}},
- function(err, doc1) {
- helpers.extractShowInfo(show, function(err, show) {
- return callback(err, show);
+ try {
+ trakt.request('show', 'summary', {title: slug}, function(err, data) {
+ if (!err && data) {
+ Show.update({ _id: doc._id },
+ { $set: { rating: data.ratings, status: data.status}},
+ function(err, doc1) {
+ helpers.extractShowInfo(show, function(err, show) {
+ return callback(err, show);
+ });
});
- });
- }
- });
+ }
+ });
+ } catch (err) {
+ console.log("Error:", err)
+ return callback(err, null);
+ }
}
else {
return callback(null, show);