Beispiel #1
0
        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 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);
        }
Beispiel #3
0
        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);
        }
Beispiel #4
0
        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);
        }