From b9c0c20af645cb3c4f3bf10c5e733b98d05f3109 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Thu, 16 Mar 2017 22:10:24 +0000 Subject: [PATCH] Looks like we were overloading emby with out api calls. Put a bit of handling around that to wait when we get some errors. Problem about this is it can take a long time to generate the infomation when we have episode searching enabled. #1109 --- Ombi.Api/EmbyApi.cs | 1 - .../EmbyRecentlyAddedNewsletter.cs | 89 ++++++++++++------- Ombi.UI/Views/Admin/NewsletterSettings.cshtml | 10 ++- 3 files changed, 62 insertions(+), 38 deletions(-) diff --git a/Ombi.Api/EmbyApi.cs b/Ombi.Api/EmbyApi.cs index 83028153a..1f066f133 100644 --- a/Ombi.Api/EmbyApi.cs +++ b/Ombi.Api/EmbyApi.cs @@ -185,7 +185,6 @@ namespace Ombi.Api throw new ArgumentOutOfRangeException(nameof(type), type, null); } - var info = new EmbyInformation(); switch (type) { diff --git a/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs b/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs index f92d61faf..7bda0d5af 100644 --- a/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs +++ b/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs @@ -32,6 +32,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; +using Newtonsoft.Json; using NLog; using Ombi.Api; using Ombi.Api.Interfaces; @@ -116,7 +117,6 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter var filteredMovies = movie.Where(m => recentlyAdded.All(x => x.ProviderId != m.ProviderId)).ToList(); var filteredEp = episodes.Where(m => recentlyAdded.All(x => x.ProviderId != m.ProviderId)).ToList(); - var info = new List(); foreach (var m in filteredMovies) { @@ -137,28 +137,41 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter { var i = Api.GetInformation(t.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Series, embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); - var ep = filteredEp.Where(x => x.ParentId == t.EmbyId); - - if (ep.Any()) + var ep = filteredEp.Where(x => x.ParentId == t.EmbyId).ToList(); + var item = new EmbyRecentlyAddedModel { - var episodeList = new List(); - foreach (var embyEpisodese in ep) + EmbyContent = t, + EmbyInformation = i, + }; + if (ep.Any() && embySettings.EnableEpisodeSearching) + { + try { - var epInfo = Api.GetInformation(embyEpisodese.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Episode, - embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); - episodeList.Add(epInfo.EpisodeInformation); + var episodeList = new List(); + foreach (var embyEpisodese in ep) + { + var epInfo = Api.GetInformation(embyEpisodese.EmbyId, + Ombi.Api.Models.Emby.EmbyMediaType.Episode, + embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); + episodeList.Add(epInfo.EpisodeInformation); + Thread.Sleep(200); // Let's not try and overload the server + } + item.EpisodeInformation = episodeList; } - info.Add(new EmbyRecentlyAddedModel + catch (JsonReaderException) { - EmbyContent = t, - EmbyInformation = i, - EpisodeInformation = episodeList - }); + Log.Error( + "Failed getting episode information, we may have overloaded Emby's api... Waiting and we will skip this one and go to the next"); + Thread.Sleep(500); + } } + + info.Add(item); } GenerateTvHtml(info, sb); newsletter.TvCount = info.Count; + var template = new RecentlyAddedTemplate(); var html = template.LoadTemplate(sb.ToString()); Log.Debug("Loaded the template"); @@ -183,10 +196,12 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter } } + var escapedHtml = new string(html.Where(c => !char.IsControl(c)).ToArray()); Log.Debug(escapedHtml); newsletter.Html = escapedHtml; return newsletter; + } private void GenerateMovieHtml(IEnumerable recentlyAddedMovies, StringBuilder sb) @@ -276,11 +291,12 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter { var seriesItem = t.EmbyInformation.SeriesInformation; var relatedEpisodes = t.EpisodeInformation; - + var endLoop = false; try { var info = TvApi.ShowLookupByTheTvDbId(int.Parse(seriesItem.ProviderIds.Tvdb)); + if(info == null)continue; var banner = info.image?.original; if (!string.IsNullOrEmpty(banner)) @@ -299,30 +315,33 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter Header(sb, 3, title); EndTag(sb, "a"); - var results = relatedEpisodes.GroupBy(p => p.ParentIndexNumber, - (key, g) => new - { - ParentIndexNumber = key, - IndexNumber = g.ToList() - } - ); - // Group the episodes - foreach (var embyEpisodeInformation in results.OrderBy(x => x.ParentIndexNumber)) + if (relatedEpisodes != null) { - var epSb = new StringBuilder(); - for (var i = 0; i < embyEpisodeInformation.IndexNumber.Count; i++) - { - var ep = embyEpisodeInformation.IndexNumber[i]; - if (i < embyEpisodeInformation.IndexNumber.Count) + var results = relatedEpisodes.GroupBy(p => p.ParentIndexNumber, + (key, g) => new { - epSb.Append($"{ep.IndexNumber},"); + ParentIndexNumber = key, + IndexNumber = g.ToList() } - else + ); + // Group the episodes + foreach (var embyEpisodeInformation in results.OrderBy(x => x.ParentIndexNumber)) + { + var epSb = new StringBuilder(); + for (var i = 0; i < embyEpisodeInformation.IndexNumber.Count; i++) { - epSb.Append(ep); + var ep = embyEpisodeInformation.IndexNumber[i]; + if (i < embyEpisodeInformation.IndexNumber.Count) + { + epSb.Append($"{ep.IndexNumber},"); + } + else + { + epSb.Append(ep); + } } + AddParagraph(sb, $"Season: {embyEpisodeInformation.ParentIndexNumber}, Episode: {epSb}"); } - AddParagraph(sb, $"Season: {embyEpisodeInformation.ParentIndexNumber}, Episode: {epSb}"); } if (info.genres.Any()) @@ -331,6 +350,7 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter } AddParagraph(sb, string.IsNullOrEmpty(seriesItem.Overview) ? info.summary : seriesItem.Overview); + endLoop = true; } catch (Exception e) { @@ -338,7 +358,8 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter } finally { - EndLoopHtml(sb); + if(endLoop) + EndLoopHtml(sb); } } sb.Append("

"); diff --git a/Ombi.UI/Views/Admin/NewsletterSettings.cshtml b/Ombi.UI/Views/Admin/NewsletterSettings.cshtml index 2802e68d6..32bc10dce 100644 --- a/Ombi.UI/Views/Admin/NewsletterSettings.cshtml +++ b/Ombi.UI/Views/Admin/NewsletterSettings.cshtml @@ -32,7 +32,9 @@
- + +
@@ -84,6 +86,8 @@ $('#recentlyAddedBtn').click(function (e) { e.preventDefault(); + + generateNotify("This could take some time depending on if you have episode searching enabled and also how many new items have been added!", "info"); var base = '@Html.GetBaseUrl()'; var url = createBaseUrl(base, '/admin/testnewsletteradminemail'); $('#testEmailSpinner').attr("class", "fa fa-spinner fa-spin"); @@ -94,11 +98,11 @@ success: function (response) { if (response) { generateNotify(response.message, "success"); - $('#testSendMassEmailSpinner').attr("class", "fa fa-check"); + $('#testEmailSpinner').attr("class", "fa fa-check"); } else { generateNotify(response.message, "danger"); - $('#testSendMassEmailSpinner').attr("class", "fa fa-times"); + $('#testEmailSpinner').attr("class", "fa fa-times"); } }, error: function (e) {