Exemplo n.º 1
0
        private Response CouchPotatoTest()
        {
            var couchPotatoSettings = this.Bind <CouchPotatoSettings>();
            var valid = this.Validate(couchPotatoSettings);

            if (!valid.IsValid)
            {
                return(Response.AsJson(valid.SendJsonError()));
            }
            try
            {
                var status = CpApi.GetStatus(couchPotatoSettings.FullUri, couchPotatoSettings.ApiKey);
                return(status.success
               ? Response.AsJson(new JsonResponseModel {
                    Result = true, Message = "Connected to CouchPotato successfully!"
                })
               : Response.AsJson(new JsonResponseModel {
                    Result = false, Message = "Could not connect to CouchPotato, please check your settings."
                }));
            }
            catch (Exception e) // Exceptions are expected if we cannot connect so we will just log and swallow them.
            {
                Log.Warn("Exception thrown when attempting to get CP's status: ");
                Log.Warn(e);
                var message = $"Could not connect to CouchPotato, please check your settings. <strong>Exception Message:</strong> {e.Message}";
                if (e.InnerException != null)
                {
                    message = $"Could not connect to CouchPotato, please check your settings. <strong>Exception Message:</strong> {e.InnerException.Message}";
                }
                return(Response.AsJson(new JsonResponseModel {
                    Result = false, Message = message
                }));
            }
        }
Exemplo n.º 2
0
        private async Task <List <QualityModel> > GetQualityProfiles()
        {
            var qualities = new List <QualityModel>();

            if (IsAdmin)
            {
                var cpSettings = CpSettings.GetSettings();
                if (cpSettings.Enabled)
                {
                    try
                    {
                        var result = await Cache.GetOrSetAsync(CacheKeys.CouchPotatoQualityProfiles, async() =>
                        {
                            return(await Task.Run(() => CpApi.GetProfiles(cpSettings.FullUri, cpSettings.ApiKey)).ConfigureAwait(false));
                        });

                        if (result != null)
                        {
                            qualities = result.list.Select(x => new QualityModel {
                                Id = x._id, Name = x.label
                            }).ToList();
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Info(e);
                    }
                }
            }
            return(qualities);
        }
Exemplo n.º 3
0
        public void Queued()
        {
            Log.Trace("Getting the settings");

            var settings = CpSettings.GetSettings();

            if (settings.Enabled)
            {
                Job.SetRunning(true, JobNames.CpCacher);
                Log.Trace("Getting all movies from CouchPotato");
                try
                {
                    var movies = CpApi.GetMovies(settings.FullUri, settings.ApiKey, new[] { "active" });
                    if (movies != null)
                    {
                        Cache.Set(CacheKeys.CouchPotatoQueued, movies, CacheKeys.TimeFrameMinutes.SchedulerCaching);
                    }
                }
                catch (System.Exception ex)
                {
                    Log.Error(ex, "Failed caching queued items from CouchPotato");
                }
                finally
                {
                    Job.Record(JobNames.CpCacher);
                    Job.SetRunning(false, JobNames.CpCacher);
                }
            }
        }
Exemplo n.º 4
0
        private bool ProcessMovies(RequestedModel model, CouchPotatoSettings cp)
        {
            try
            {
                if (cp.Enabled)
                {
                    var result = CpApi.AddMovie(model.ImdbId, cp.ApiKey, model.Title,
                                                cp.FullUri, cp.ProfileId);

                    if (result)
                    {
                        // Approve it now
                        model.Approved = true;
                        RequestService.UpdateRequest(model);
                    }
                    ;

                    return(result);
                }
                return(false);
            }
            catch (Exception e)
            {
                Log.Error(e);
                return(false); // It fails so it will get added back into the queue
            }
        }
Exemplo n.º 5
0
        private MovieSenderResult SendToCp(RequestedModel model, CouchPotatoSettings settings, string qualityId)
        {
            var result = CpApi.AddMovie(model.ImdbId, settings.ApiKey, model.Title, settings.FullUri, qualityId);

            return(new MovieSenderResult {
                Result = result, MovieSendingEnabled = true
            });
        }
Exemplo n.º 6
0
        private async Task <Response> RequestMovieAndUpdateStatus(RequestedModel request, string qualityId)
        {
            var cpSettings = await CpService.GetSettingsAsync();

            Log.Info("Adding movie to CouchPotato : {0}", request.Title);
            if (!cpSettings.Enabled)
            {
                // Approve it
                request.Approved = true;
                Log.Warn("We approved movie: {0} but could not add it to CouchPotato because it has not been setup", request.Title);

                // Update the record
                var inserted = await Service.UpdateRequestAsync(request);

                return(Response.AsJson(inserted
                    ? new JsonResponseModel {
                    Result = true, Message = "This has been approved, but It has not been sent to CouchPotato because it has not been configured."
                }
                    : new JsonResponseModel
                {
                    Result = false,
                    Message = "We could not approve this request. Please try again or check the logs."
                }));
            }

            var result = CpApi.AddMovie(request.ImdbId, cpSettings.ApiKey, request.Title, cpSettings.FullUri, string.IsNullOrEmpty(qualityId) ? cpSettings.ProfileId : qualityId);

            Log.Trace("Adding movie to CP result {0}", result);
            if (result)
            {
                // Approve it
                request.Approved = true;

                // Update the record
                var inserted = await Service.UpdateRequestAsync(request);

                return(Response.AsJson(inserted
                    ? new JsonResponseModel {
                    Result = true
                }
                    : new JsonResponseModel
                {
                    Result = false,
                    Message = "We could not approve this request. Please try again or check the logs."
                }));
            }
            return
                (Response.AsJson(
                     new
            {
                Result = false,
                Message =
                    "Something went wrong adding the movie to CouchPotato! Please check your settings."
            }));
        }
Exemplo n.º 7
0
        private Response GetCpApiKey()
        {
            var settings = this.Bind <CouchPotatoSettings>();

            if (string.IsNullOrEmpty(settings.Username) || string.IsNullOrEmpty(settings.Password))
            {
                return(Response.AsJson(new { Message = "Please enter a username and password to request the Api Key", Result = false }));
            }
            var key = CpApi.GetApiKey(settings.FullUri, settings.Username, settings.Password);


            return(Response.AsJson(key));
        }
Exemplo n.º 8
0
        private Response GetCpProfiles()
        {
            var settings = this.Bind <CouchPotatoSettings>();
            var valid    = this.Validate(settings);

            if (!valid.IsValid)
            {
                return(Response.AsJson(valid.SendJsonError()));
            }
            var profiles = CpApi.GetProfiles(settings.FullUri, settings.ApiKey);

            // set the cache
            if (profiles != null)
            {
                Cache.Set(CacheKeys.CouchPotatoQualityProfiles, profiles);
            }

            return(Response.AsJson(profiles));
        }
Exemplo n.º 9
0
        private async Task <Response> GetCpProfiles()
        {
            var settings = this.Bind <CouchPotatoSettings>();
            var valid    = this.Validate(settings);

            if (!valid.IsValid)
            {
                return(Response.AsJson(valid.SendJsonError()));
            }
            var profiles = CpApi.GetProfiles(settings.FullUri, settings.ApiKey);

            // set the cache
            if (profiles != null)
            {
                Cache.Set(CacheKeys.CouchPotatoQualityProfiles, profiles);
            }

            // Save the first profile found (user might not press save...)
            settings.ProfileId = profiles?.list?.FirstOrDefault()?._id;
            await CpService.SaveSettingsAsync(settings);

            return(Response.AsJson(profiles));
        }
Exemplo n.º 10
0
        private Response GetMovies() // TODO: async await the API calls
        {
            var settings = PrSettings.GetSettings();

            List <Task> taskList = new List <Task>();

            List <RequestedModel> dbMovies = new List <RequestedModel>();

            taskList.Add(Task.Factory.StartNew(() =>
            {
                return(Service.GetAll().Where(x => x.Type == RequestType.Movie));
            }).ContinueWith((t) =>
            {
                dbMovies = t.Result.ToList();

                if (settings.UsersCanViewOnlyOwnRequests && !IsAdmin)
                {
                    dbMovies = dbMovies.Where(x => x.UserHasRequested(Username)).ToList();
                }
            }));


            List <QualityModel> qualities = new List <QualityModel>();

            if (IsAdmin)
            {
                var cpSettings = CpSettings.GetSettings();
                if (cpSettings.Enabled)
                {
                    taskList.Add(Task.Factory.StartNew(() =>
                    {
                        return(Cache.GetOrSet(CacheKeys.CouchPotatoQualityProfiles, () =>
                        {
                            return CpApi.GetProfiles(cpSettings.FullUri, cpSettings.ApiKey); // TODO: cache this!
                        }));
                    }).ContinueWith((t) =>
                    {
                        qualities = t.Result.list.Select(x => new QualityModel()
                        {
                            Id = x._id, Name = x.label
                        }).ToList();
                    }));
                }
            }

            Task.WaitAll(taskList.ToArray());

            var viewModel = dbMovies.Select(movie =>
            {
                return(new RequestViewModel
                {
                    ProviderId = movie.ProviderId,
                    Type = movie.Type,
                    Status = movie.Status,
                    ImdbId = movie.ImdbId,
                    Id = movie.Id,
                    PosterPath = movie.PosterPath,
                    ReleaseDate = movie.ReleaseDate,
                    ReleaseDateTicks = movie.ReleaseDate.Ticks,
                    RequestedDate = movie.RequestedDate,
                    Released = DateTime.Now > movie.ReleaseDate,
                    RequestedDateTicks = DateTimeHelper.OffsetUTCDateTime(movie.RequestedDate, DateTimeOffset).Ticks,
                    Approved = movie.Available || movie.Approved,
                    Title = movie.Title,
                    Overview = movie.Overview,
                    RequestedUsers = IsAdmin ? movie.AllUsers.ToArray() : new string[] { },
                    ReleaseYear = movie.ReleaseDate.Year.ToString(),
                    Available = movie.Available,
                    Admin = IsAdmin,
                    Issues = movie.Issues.ToString().CamelCaseToWords(),
                    OtherMessage = movie.OtherMessage,
                    AdminNotes = movie.AdminNote,
                    Qualities = qualities.ToArray()
                });
            }).ToList();

            return(Response.AsJson(viewModel));
        }
Exemplo n.º 11
0
        private async Task <Response> GetMovies()
        {
            var settings = PrSettings.GetSettings();

            var allRequests = await Service.GetAllAsync();

            allRequests = allRequests.Where(x => x.Type == RequestType.Movie);

            var dbMovies = allRequests.ToList();

            if (settings.UsersCanViewOnlyOwnRequests && !IsAdmin)
            {
                dbMovies = dbMovies.Where(x => x.UserHasRequested(Username)).ToList();
            }

            List <QualityModel> qualities = new List <QualityModel>();

            if (IsAdmin)
            {
                var cpSettings = CpSettings.GetSettings();
                if (cpSettings.Enabled)
                {
                    try
                    {
                        var result = await Cache.GetOrSetAsync(CacheKeys.CouchPotatoQualityProfiles, async() =>
                        {
                            return(await Task.Run(() => CpApi.GetProfiles(cpSettings.FullUri, cpSettings.ApiKey)).ConfigureAwait(false));
                        });

                        if (result != null)
                        {
                            qualities = result.list.Select(x => new QualityModel {
                                Id = x._id, Name = x.label
                            }).ToList();
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Info(e);
                    }
                }
            }

            var viewModel = dbMovies.Select(movie => new RequestViewModel
            {
                ProviderId         = movie.ProviderId,
                Type               = movie.Type,
                Status             = movie.Status,
                ImdbId             = movie.ImdbId,
                Id                 = movie.Id,
                PosterPath         = movie.PosterPath,
                ReleaseDate        = movie.ReleaseDate,
                ReleaseDateTicks   = movie.ReleaseDate.Ticks,
                RequestedDate      = movie.RequestedDate,
                Released           = DateTime.Now > movie.ReleaseDate,
                RequestedDateTicks = DateTimeHelper.OffsetUTCDateTime(movie.RequestedDate, DateTimeOffset).Ticks,
                Approved           = movie.Available || movie.Approved,
                Title              = movie.Title,
                Overview           = movie.Overview,
                RequestedUsers     = IsAdmin ? movie.AllUsers.ToArray() : new string[] { },
                ReleaseYear        = movie.ReleaseDate.Year.ToString(),
                Available          = movie.Available,
                Admin              = IsAdmin,
                IssueId            = movie.IssueId,
                Qualities          = qualities.ToArray()
            }).ToList();

            return(Response.AsJson(viewModel));
        }
Exemplo n.º 12
0
        private async Task <Response> GetMovies()
        {
            var allRequests = await Service.GetAllAsync();

            allRequests = allRequests.Where(x => x.Type == RequestType.Movie);

            var dbMovies = allRequests.ToList();

            if (Security.HasPermissions(User, Permissions.UsersCanViewOnlyOwnRequests) && !IsAdmin)
            {
                dbMovies = dbMovies.Where(x => x.UserHasRequested(Username)).ToList();
            }

            List <QualityModel> qualities = new List <QualityModel>();
            var rootFolders = new List <RootFolderModel>();

            var radarr = await Radarr.GetSettingsAsync();

            if (IsAdmin)
            {
                try
                {
                    var cpSettings = await CpSettings.GetSettingsAsync();

                    if (cpSettings.Enabled)
                    {
                        try
                        {
                            var result = await Cache.GetOrSetAsync(CacheKeys.CouchPotatoQualityProfiles, async() =>
                            {
                                return
                                (await Task.Run(() => CpApi.GetProfiles(cpSettings.FullUri, cpSettings.ApiKey))
                                 .ConfigureAwait(false));
                            });

                            if (result != null)
                            {
                                qualities =
                                    result.list.Select(x => new QualityModel {
                                    Id = x._id, Name = x.label
                                }).ToList();
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Info(e);
                        }
                    }
                    if (radarr.Enabled)
                    {
                        var rootFoldersResult = await Cache.GetOrSetAsync(CacheKeys.RadarrRootFolders, async() =>
                        {
                            return(await Task.Run(() => RadarrApi.GetRootFolders(radarr.ApiKey, radarr.FullUri)));
                        });

                        rootFolders =
                            rootFoldersResult.Select(
                                x => new RootFolderModel {
                            Id = x.id.ToString(), Path = x.path, FreeSpace = x.freespace
                        })
                            .ToList();

                        var result = await Cache.GetOrSetAsync(CacheKeys.RadarrQualityProfiles, async() =>
                        {
                            return(await Task.Run(() => RadarrApi.GetProfiles(radarr.ApiKey, radarr.FullUri)));
                        });

                        qualities = result.Select(x => new QualityModel {
                            Id = x.id.ToString(), Name = x.name
                        }).ToList();
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
            }


            var canManageRequest = Security.HasAnyPermissions(User, Permissions.Administrator, Permissions.ManageRequests);
            var allowViewUsers   = Security.HasAnyPermissions(User, Permissions.Administrator, Permissions.ViewUsers);

            var viewModel = dbMovies.Select(movie => new RequestViewModel
            {
                ProviderId         = movie.ProviderId,
                Type               = movie.Type,
                Status             = movie.Status,
                ImdbId             = movie.ImdbId,
                Id                 = movie.Id,
                PosterPath         = movie.PosterPath,
                ReleaseDate        = movie.ReleaseDate,
                ReleaseDateTicks   = movie.ReleaseDate.Ticks,
                RequestedDate      = movie.RequestedDate,
                Released           = DateTime.Now > movie.ReleaseDate,
                RequestedDateTicks = DateTimeHelper.OffsetUTCDateTime(movie.RequestedDate, DateTimeOffset).Ticks,
                Approved           = movie.Available || movie.Approved,
                Title              = movie.Title,
                Overview           = movie.Overview,
                RequestedUsers     = canManageRequest || allowViewUsers ? movie.AllUsers.ToArray() : new string[] { },
                ReleaseYear        = movie.ReleaseDate.Year.ToString(),
                Available          = movie.Available,
                Admin              = canManageRequest,
                IssueId            = movie.IssueId,
                Denied             = movie.Denied,
                DeniedReason       = movie.DeniedReason,
                Qualities          = qualities.ToArray(),
                HasRootFolders     = rootFolders.Any(),
                RootFolders        = rootFolders.ToArray(),
                CurrentRootPath    = radarr.Enabled ? GetRootPath(movie.RootFolderSelected, radarr).Result : null
            }).ToList();

            return(Response.AsJson(viewModel));
        }