From febdaa0a1591deccee5117688c2f54fe3e72678a Mon Sep 17 00:00:00 2001 From: tidusjar Date: Tue, 14 Feb 2017 21:56:48 +0000 Subject: [PATCH] Added a TMDB Rate limiter for the newsletter --- .../EmbyRecentlyAddedNewsletter.cs | 58 +++++++++++-------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs b/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs index ca65135e2..b78f64dcc 100644 --- a/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs +++ b/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs @@ -31,6 +31,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading; using NLog; using Ombi.Api; using Ombi.Api.Interfaces; @@ -41,6 +42,7 @@ using Ombi.Services.Jobs.Templates; using Ombi.Store.Models; using Ombi.Store.Models.Emby; using Ombi.Store.Repository; +using TMDbLib.Objects.Exceptions; using EmbyMediaType = Ombi.Store.Models.Plex.EmbyMediaType; namespace Ombi.Services.Jobs.RecentlyAddedNewsletter @@ -79,7 +81,7 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter { try { - return GetHtml(test); + return GetHtml(test); } catch (Exception e) { @@ -196,32 +198,42 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter ""); foreach (var movie in orderedMovies) { + // We have a try within a try so we can catch the rate limit without ending the loop (finally block) try { - - var imdbId = movie.ProviderIds.Imdb; - var info = MovieApi.GetMovieInformation(imdbId).Result; - if (info == null) + try { - throw new Exception($"Movie with Imdb id {imdbId} returned null from the MovieApi"); - } - AddImageInsideTable(sb, $"https://image.tmdb.org/t/p/w500{info.BackdropPath}"); - sb.Append(""); - sb.Append( - ""); + sb.Append( + "
"); + var imdbId = movie.ProviderIds.Imdb; + var info = MovieApi.GetMovieInformation(imdbId).Result; + if (info == null) + { + throw new Exception($"Movie with Imdb id {imdbId} returned null from the MovieApi"); + } + AddImageInsideTable(sb, $"https://image.tmdb.org/t/p/w500{info.BackdropPath}"); - Href(sb, $"https://www.imdb.com/title/{info.ImdbId}/"); - Header(sb, 3, $"{info.Title} {info.ReleaseDate?.ToString("yyyy") ?? string.Empty}"); - EndTag(sb, "a"); + sb.Append("
"); + + Href(sb, $"https://www.imdb.com/title/{info.ImdbId}/"); + Header(sb, 3, $"{info.Title} {info.ReleaseDate?.ToString("yyyy") ?? string.Empty}"); + EndTag(sb, "a"); - if (info.Genres.Any()) + if (info.Genres.Any()) + { + AddParagraph(sb, + $"Genre: {string.Join(", ", info.Genres.Select(x => x.Name.ToString()).ToArray())}"); + } + + AddParagraph(sb, info.Overview); + } + catch (RequestLimitExceededException limit) { - AddParagraph(sb, - $"Genre: {string.Join(", ", info.Genres.Select(x => x.Name.ToString()).ToArray())}"); + // We have hit a limit, we need to now wait. + Thread.Sleep(TimeSpan.FromSeconds(10)); + Log.Info(limit); } - - AddParagraph(sb, info.Overview); } catch (Exception e) { @@ -260,8 +272,8 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter { var seriesItem = t.EmbyInformation.SeriesInformation; var relatedEpisodes = t.EpisodeInformation; - - + + try { var info = TvApi.ShowLookupByTheTvDbId(int.Parse(seriesItem.ProviderIds.Tvdb)); @@ -328,8 +340,8 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter sb.Append("


"); } - - + + private void EndLoopHtml(StringBuilder sb) {