private void Start(NewletterSettings newletterSettings, bool testEmail = false) { var sb = new StringBuilder(); var plexSettings = PlexSettings.GetSettings(); var libs = Api.GetLibrarySections(plexSettings.PlexAuthToken, plexSettings.FullUri); var tvSection = libs.Directories.FirstOrDefault(x => x.type.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase)); var movieSection = libs.Directories.FirstOrDefault(x => x.type.Equals(PlexMediaType.Movie.ToString(), StringComparison.CurrentCultureIgnoreCase)); var recentlyAddedTv = Api.RecentlyAdded(plexSettings.PlexAuthToken, plexSettings.FullUri, tvSection.Key); var recentlyAddedMovies = Api.RecentlyAdded(plexSettings.PlexAuthToken, plexSettings.FullUri, movieSection.Key); GenerateMovieHtml(recentlyAddedMovies, plexSettings, sb); GenerateTvHtml(recentlyAddedTv, plexSettings, sb); var template = new RecentlyAddedTemplate(); var html = template.LoadTemplate(sb.ToString()); Send(newletterSettings, html, plexSettings, testEmail); }
private Newsletter GetHtml(bool test) { var sb = new StringBuilder(); var newsletter = new Newsletter(); var plexSettings = PlexSettings.GetSettings(); var plexContent = Content.GetAll().ToList(); var series = plexContent.Where(x => x.Type == PlexMediaType.Show).ToList(); var episodes = Episodes.GetAll().ToList(); var movie = plexContent.Where(x => x.Type == PlexMediaType.Movie).ToList(); var recentlyAdded = RecentlyAddedLog.GetAll().ToList(); var firstRun = !recentlyAdded.Any(); 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 filteredSeries = series.Where(x => recentlyAdded.All(c => c.ProviderId != x.ProviderId)).ToList(); var info = new List <PlexRecentlyAddedModel>(); foreach (var m in filteredMovies) { var i = Api.GetMetadata(plexSettings.PlexAuthToken, plexSettings.FullUri, m.ItemId); if (i.Video == null) { continue; } info.Add(new PlexRecentlyAddedModel { Metadata = i, Content = m }); } GenerateMovieHtml(info, sb); newsletter.MovieCount = info.Count; info.Clear(); if (filteredEp.Any()) { var recentlyAddedModel = new List <PlexRecentlyAddedModel>(); foreach (var plexEpisodes in filteredEp) { // Find related series item var relatedSeries = series.FirstOrDefault(x => x.ProviderId == plexEpisodes.ProviderId); if (relatedSeries == null) { continue; } // Get series information var i = Api.GetMetadata(plexSettings.PlexAuthToken, plexSettings.FullUri, relatedSeries.ItemId); var episodeInfo = Api.GetEpisodeMetaData(plexSettings.PlexAuthToken, plexSettings.FullUri, plexEpisodes.RatingKey); // Check if we already have this series var existingSeries = recentlyAddedModel.FirstOrDefault(x => x.Metadata.Directory.RatingKey == i.Directory.RatingKey); if (existingSeries != null) { existingSeries.EpisodeMetadata.Add(episodeInfo); } else { recentlyAddedModel.Add(new PlexRecentlyAddedModel { Metadata = i, EpisodeMetadata = new List <PlexEpisodeMetadata>() { episodeInfo }, Content = relatedSeries }); } } info.AddRange(recentlyAddedModel); } else { foreach (var t in filteredSeries) { var i = Api.GetMetadata(plexSettings.PlexAuthToken, plexSettings.FullUri, t.ItemId); if (i.Directory == null) { continue; } info.Add(new PlexRecentlyAddedModel { Metadata = i, Content = t }); } } 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.ProviderId, AddedAt = DateTime.UtcNow }); } foreach (var a in filteredEp) { RecentlyAddedLog.Insert(new RecentlyAddedLog { ProviderId = a.ProviderId, AddedAt = DateTime.UtcNow }); } foreach (var a in filteredSeries) { RecentlyAddedLog.Insert(new RecentlyAddedLog { ProviderId = a.ProviderId, AddedAt = DateTime.UtcNow }); } } var escapedHtml = new string(html.Where(c => !char.IsControl(c)).ToArray()); Log.Debug(escapedHtml); newsletter.Html = escapedHtml; return(newsletter); }
private void Start(NewletterSettings newletterSettings, bool testEmail = false) { var sb = new StringBuilder(); var plexSettings = PlexSettings.GetSettings(); Log.Debug("Got Plex Settings"); var libs = Api.GetLibrarySections(plexSettings.PlexAuthToken, plexSettings.FullUri); Log.Debug("Getting Plex Library Sections"); var tvSections = libs.Directories.Where(x => x.type.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase)); // We could have more than 1 lib Log.Debug("Filtered sections for TV"); var movieSection = libs.Directories.Where(x => x.type.Equals(PlexMediaType.Movie.ToString(), StringComparison.CurrentCultureIgnoreCase)); // We could have more than 1 lib Log.Debug("Filtered sections for Movies"); var plexVersion = Api.GetStatus(plexSettings.PlexAuthToken, plexSettings.FullUri).Version; var html = string.Empty; if (plexVersion.StartsWith("1.3")) { var tvMetadata = new List <Metadata>(); var movieMetadata = new List <Metadata>(); foreach (var tvSection in tvSections) { var item = Api.RecentlyAdded(plexSettings.PlexAuthToken, plexSettings.FullUri, tvSection?.Key); if (item?.MediaContainer?.Metadata != null) { tvMetadata.AddRange(item?.MediaContainer?.Metadata); } } Log.Debug("Got RecentlyAdded TV Shows"); foreach (var movie in movieSection) { var recentlyAddedMovies = Api.RecentlyAdded(plexSettings.PlexAuthToken, plexSettings.FullUri, movie?.Key); if (recentlyAddedMovies?.MediaContainer?.Metadata != null) { movieMetadata.AddRange(recentlyAddedMovies?.MediaContainer?.Metadata); } } Log.Debug("Got RecentlyAdded Movies"); Log.Debug("Started Generating Movie HTML"); GenerateMovieHtml(movieMetadata, plexSettings, sb); Log.Debug("Finished Generating Movie HTML"); Log.Debug("Started Generating TV HTML"); GenerateTvHtml(tvMetadata, plexSettings, sb); Log.Debug("Finished Generating TV HTML"); var template = new RecentlyAddedTemplate(); html = template.LoadTemplate(sb.ToString()); Log.Debug("Loaded the template"); } else { // Old API var tvChild = new List <RecentlyAddedChild>(); var movieChild = new List <RecentlyAddedChild>(); foreach (var tvSection in tvSections) { var recentlyAddedTv = Api.RecentlyAddedOld(plexSettings.PlexAuthToken, plexSettings.FullUri, tvSection?.Key); if (recentlyAddedTv?._children != null) { tvChild.AddRange(recentlyAddedTv?._children); } } Log.Debug("Got RecentlyAdded TV Shows"); foreach (var movie in movieSection) { var recentlyAddedMovies = Api.RecentlyAddedOld(plexSettings.PlexAuthToken, plexSettings.FullUri, movie?.Key); if (recentlyAddedMovies?._children != null) { tvChild.AddRange(recentlyAddedMovies?._children); } } Log.Debug("Got RecentlyAdded Movies"); Log.Debug("Started Generating Movie HTML"); GenerateMovieHtml(movieChild, plexSettings, sb); Log.Debug("Finished Generating Movie HTML"); Log.Debug("Started Generating TV HTML"); GenerateTvHtml(tvChild, plexSettings, sb); Log.Debug("Finished Generating TV HTML"); var template = new RecentlyAddedTemplate(); html = template.LoadTemplate(sb.ToString()); Log.Debug("Loaded the template"); } Send(newletterSettings, html, plexSettings, testEmail); }
private string GetHtml(bool test) { var sb = new StringBuilder(); 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.ProviderId)).ToList(); var filteredEp = episodes.Where(m => recentlyAdded.All(x => x.ProviderId != m.ProviderId)).ToList(); var info = new List <EmbyRecentlyAddedModel>(); foreach (var m in filteredMovies) { var i = Api.GetInformation(m.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Movie, embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); info.Add(new EmbyRecentlyAddedModel { EmbyInformation = i, EmbyContent = m }); } GenerateMovieHtml(info, sb); info.Clear(); foreach (var t in series) { 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 episodeList = new List <EmbyEpisodeInformation>(); 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); } info.Add(new EmbyRecentlyAddedModel { EmbyContent = t, EmbyInformation = i, EpisodeInformation = episodeList }); } } GenerateTvHtml(info, sb); 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.ProviderId, AddedAt = DateTime.UtcNow }); } foreach (var a in filteredEp) { RecentlyAddedLog.Insert(new RecentlyAddedLog { ProviderId = a.ProviderId, AddedAt = DateTime.UtcNow }); } } var escapedHtml = new string(html.Where(c => !char.IsControl(c)).ToArray()); Log.Debug(escapedHtml); return(escapedHtml); }
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); }
private string GetHtml(bool test) { var sb = new StringBuilder(); var plexSettings = PlexSettings.GetSettings(); var plexContent = Content.GetAll().ToList(); var series = plexContent.Where(x => x.Type == PlexMediaType.Show).ToList(); var episodes = Episodes.GetAll().ToList(); var movie = plexContent.Where(x => x.Type == PlexMediaType.Movie).ToList(); var recentlyAdded = RecentlyAddedLog.GetAll().ToList(); var firstRun = !recentlyAdded.Any(); 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 filteredSeries = series.Where(x => recentlyAdded.All(c => c.ProviderId != x.ProviderId)).ToList(); var info = new List <PlexRecentlyAddedModel>(); foreach (var m in filteredMovies) { var i = Api.GetMetadata(plexSettings.PlexAuthToken, plexSettings.FullUri, m.ItemId); if (i.Video == null) { continue; } info.Add(new PlexRecentlyAddedModel { Metadata = i, Content = m }); } GenerateMovieHtml(info, sb); info.Clear(); foreach (var t in filteredSeries) { var i = Api.GetMetadata(plexSettings.PlexAuthToken, plexSettings.FullUri, t.ItemId); if (i.Directory == null) { continue; } //var ep = filteredEp.Where(x => x.ShowTitle == t.Title); info.Add(new PlexRecentlyAddedModel { Metadata = i, Content = t }); //if (ep.Any()) //{ // var episodeList = new List<EmbyEpisodeInformation>(); // 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); // } // info.Add(new EmbyRecentlyAddedModel // { // EmbyContent = t, // EmbyInformation = i, // EpisodeInformation = episodeList // }); //} } GenerateTvHtml(info, sb); 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.ProviderId, AddedAt = DateTime.UtcNow }); } foreach (var a in filteredEp) { RecentlyAddedLog.Insert(new RecentlyAddedLog { ProviderId = a.ProviderId, AddedAt = DateTime.UtcNow }); } foreach (var a in filteredSeries) { RecentlyAddedLog.Insert(new RecentlyAddedLog { ProviderId = a.ProviderId, AddedAt = DateTime.UtcNow }); } } var escapedHtml = new string(html.Where(c => !char.IsControl(c)).ToArray()); Log.Debug(escapedHtml); return(escapedHtml); }