private Newsletter GetHtml(bool test) { var sb = new StringBuilder(); var newsletter = new Newsletter(); var embySettings = EmbySettings.GetSettings(); var embyContent = Content.GetAll().ToList(); var series = embyContent.Where(x => x.Type == EmbyMediaType.Series).ToList(); var episodes = Episodes.GetAll().ToList(); var movie = embyContent.Where(x => x.Type == EmbyMediaType.Movie).ToList(); var recentlyAdded = RecentlyAddedLog.GetAll().ToList(); var firstRun = !recentlyAdded.Any(); var filteredMovies = movie.Where(m => recentlyAdded.All(x => x.ProviderId != m.EmbyId)).ToList(); var filteredEp = episodes.Where(m => recentlyAdded.All(x => x.ProviderId != m.EmbyId)).ToList(); var filteredSeries = series.Where(m => recentlyAdded.All(x => x.ProviderId != m.EmbyId)).ToList(); var info = new List <EmbyRecentlyAddedModel>(); foreach (var m in filteredMovies.OrderByDescending(x => x.AddedAt)) { var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception thrown when processing an emby movie for the newsletter, Retrying {0}", timespan)); var result = policy.Execute(() => { var i = Api.GetInformation(m.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Movie, embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); return(new EmbyRecentlyAddedModel { EmbyInformation = i, EmbyContent = m }); }); info.Add(result); } GenerateMovieHtml(info, sb); newsletter.MovieCount = info.Count; info.Clear(); // Check if there are any epiosdes, then get the series info. // Otherwise then just add the series to the newsletter if (test && !filteredEp.Any() && episodes.Any()) { // if this is a test make sure we show something filteredEp = episodes.Take(5).ToList(); } if (filteredEp.Any()) { var recentlyAddedModel = new List <EmbyRecentlyAddedModel>(); foreach (var embyEpisodes in filteredEp) { try { var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception thrown when processing an emby episode for the newsletter, Retrying {0}", timespan)); policy.Execute(() => { // Find related series item var relatedSeries = series.FirstOrDefault(x => x.EmbyId == embyEpisodes.ParentId); if (relatedSeries == null) { return; } // Get series information var i = Api.GetInformation(relatedSeries.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Series, embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); Thread.Sleep(200); var episodeInfo = Api.GetInformation(embyEpisodes.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Episode, embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); // Check if we already have this series var existingSeries = recentlyAddedModel.FirstOrDefault(x => x.EmbyInformation.SeriesInformation.Id.Equals(i.SeriesInformation.Id, StringComparison.CurrentCultureIgnoreCase)); if (existingSeries != null) { existingSeries.EpisodeInformation.Add(episodeInfo.EpisodeInformation); } else { recentlyAddedModel.Add(new EmbyRecentlyAddedModel { EmbyInformation = i, EpisodeInformation = new List <EmbyEpisodeInformation>() { episodeInfo.EpisodeInformation }, EmbyContent = relatedSeries }); } }); } catch (JsonReaderException) { Log.Error("Failed getting information from Emby, we may have overloaded Emby's api... Waiting and we will skip this one and go to the next"); Thread.Sleep(1000); } } info.AddRange(recentlyAddedModel); } else { foreach (var t in filteredSeries.OrderByDescending(x => x.AddedAt)) { var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception thrown when processing an emby series for the newsletter, Retrying {0}", timespan)); var item = policy.Execute(() => { var i = Api.GetInformation(t.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Series, embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); var model = new EmbyRecentlyAddedModel { EmbyContent = t, EmbyInformation = i, }; return(model); }); 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"); if (!test || firstRun) { foreach (var a in filteredMovies) { RecentlyAddedLog.Insert(new RecentlyAddedLog { ProviderId = a.EmbyId, AddedAt = DateTime.UtcNow }); } foreach (var a in filteredEp) { RecentlyAddedLog.Insert(new RecentlyAddedLog { ProviderId = a.EmbyId, AddedAt = DateTime.UtcNow }); } foreach (var s in filteredSeries) { RecentlyAddedLog.Insert(new RecentlyAddedLog { ProviderId = s.EmbyId, AddedAt = DateTime.UtcNow }); } } var escapedHtml = new string(html.Where(c => !char.IsControl(c)).ToArray()); Log.Debug(escapedHtml); newsletter.Html = escapedHtml; return(newsletter); }