コード例 #1
0
        private void StartNewsLetter(NewletterSettings newletterSettings, bool testEmail = false)
        {
            var embySettings = EmbySettings.GetSettings();

            if (embySettings.Enable)
            {
                var html = EmbyNewsletter.GetNewsletterHtml(testEmail);

                var escapedHtml = new string(html.Where(c => !char.IsControl(c)).ToArray());
                Log.Debug(escapedHtml);
                SendNewsletter(newletterSettings, escapedHtml, testEmail, "New Content On Emby!");
            }
            else
            {
                var plexSettings = PlexSettings.GetSettings();
                if (plexSettings.Enable)
                {
                    var html = PlexNewsletter.GetNewsletterHtml(testEmail);

                    var escapedHtml = new string(html.Where(c => !char.IsControl(c)).ToArray());
                    Log.Debug(escapedHtml);
                    SendNewsletter(newletterSettings, html, testEmail);
                }
            }
        }
コード例 #2
0
        private void StartNewsLetter(NewletterSettings newletterSettings, bool testEmail = false)
        {
            var embySettings = EmbySettings.GetSettings();

            if (embySettings.Enable)
            {
                var letter = EmbyNewsletter.GetNewsletter(testEmail) ?? new Newsletter();
                if (letter.Send || testEmail)
                {
                    SendNewsletter(newletterSettings, letter.Html, testEmail, "New Content On Emby!");
                }
                else
                {
                    Log.Warn("There is no new content to send the newsletter");
                }
            }
            else
            {
                var plexSettings = PlexSettings.GetSettings();
                if (plexSettings.Enable)
                {
                    var letter = PlexNewsletter.GetNewsletter(testEmail) ?? new Newsletter();
                    if (letter.Send || testEmail)
                    {
                        SendNewsletter(newletterSettings, letter.Html, testEmail);
                    }
                }
            }
        }
コード例 #3
0
        public void Start()
        {
            JobRecord.SetRunning(true, JobNames.EmbyUserChecker);

            try
            {
                var settings = EmbySettings.GetSettings();
                if (string.IsNullOrEmpty(settings.ApiKey) || !settings.Enable)
                {
                    return;
                }
                var embyUsers = EmbyApi.GetUsers(settings.FullUri, settings.ApiKey);
                var userManagementSettings = UserManagementSettings.GetSettings();

                var dbUsers = Repo.GetAll().ToList();

                // Regular users
                foreach (var user in embyUsers)
                {
                    var dbUser = dbUsers.FirstOrDefault(x => x.EmbyUserId == user.Id);
                    if (dbUser != null)
                    {
                        // we already have a user
                        continue;
                    }

                    // Looks like it's a new user!
                    var m = new EmbyUsers
                    {
                        EmbyUserId  = user.Id,
                        Permissions = UserManagementHelper.GetPermissions(userManagementSettings),
                        Features    = UserManagementHelper.GetFeatures(userManagementSettings),
                        UserAlias   = string.Empty,
                        Username    = user.Name,
                        LoginId     = Guid.NewGuid().ToString()
                    };


                    // If it's the admin, give them the admin permission
                    if (user.Policy.IsAdministrator)
                    {
                        if (!((Permissions)m.Permissions).HasFlag(Permissions.Administrator))
                        {
                            m.Permissions += (int)Permissions.Administrator;
                        }
                    }

                    Repo.Insert(m);
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            finally
            {
                JobRecord.SetRunning(false, JobNames.EmbyUserChecker);
                JobRecord.Record(JobNames.EmbyUserChecker);
            }
        }
コード例 #4
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);
        }
コード例 #5
0
        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);
        }