private Response RequestMovieAndUpdateStatus(RequestedModel request, string qualityId)
        {
            var cpSettings = CpService.GetSettings();
            var cp         = new CouchPotatoApi();

            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 = Service.UpdateRequest(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 = cp.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 = Service.UpdateRequest(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."
            }));
        }
Exemple #2
0
        public async Task <bool> CouchPotato([FromBody] CouchPotatoSettings settings)
        {
            try
            {
                var result = await CouchPotatoApi.Status(settings.FullUri, settings.ApiKey);

                return(result?.success ?? false);
            }
            catch (Exception e)
            {
                Log.LogError(LoggingEvents.Api, e, "Could not test CP");
                return(false);
            }
        }
Exemple #3
0
 private void CacheCouchPotatoQualityProfiles(ICacheProvider cacheProvider)
 {
     try
     {
         var cpSettingsService = new SettingsServiceV2 <CouchPotatoSettings>(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), cacheProvider));
         var cpSettings        = cpSettingsService.GetSettings();
         if (cpSettings.Enabled)
         {
             Log.Info("Begin executing GetProfiles call to CouchPotato for quality profiles");
             CouchPotatoApi cpApi    = new CouchPotatoApi();
             var            profiles = cpApi.GetProfiles(cpSettings.FullUri, cpSettings.ApiKey);
             cacheProvider.Set(CacheKeys.CouchPotatoQualityProfiles, profiles);
             Log.Info("Finished executing GetProfiles call to CouchPotato for quality profiles");
         }
     }
     catch (Exception ex)
     {
         Log.Error(ex, "Failed to cache CouchPotato quality profiles!");
     }
 }
        private Response RequestMovie(int movieId)
        {
            var movieApi      = new TheMovieDbApi();
            var movieInfo     = movieApi.GetMovieInformation(movieId).Result;
            var fullMovieName = $"{movieInfo.Title}{(movieInfo.ReleaseDate.HasValue ? $" ({movieInfo.ReleaseDate.Value.Year})" : string.Empty)}";

            Log.Trace("Getting movie info from TheMovieDb");
            Log.Trace(movieInfo.DumpJson);
            //#if !DEBUG

            var settings = PrService.GetSettings();

            // check if the movie has already been requested
            Log.Info("Requesting movie with id {0}", movieId);
            var existingRequest = RequestService.CheckRequest(movieId);

            if (existingRequest != null)
            {
                // check if the current user is already marked as a requester for this movie, if not, add them
                if (!existingRequest.UserHasRequested(Username))
                {
                    existingRequest.RequestedUsers.Add(Username);
                    RequestService.UpdateRequest(existingRequest);
                }

                return(Response.AsJson(new JsonResponseModel {
                    Result = true, Message = settings.UsersCanViewOnlyOwnRequests ? $"{fullMovieName} was successfully added!" : $"{fullMovieName} has already been requested!"
                }));
            }

            Log.Debug("movie with id {0} doesnt exists", movieId);

            try
            {
                var movies = Checker.GetPlexMovies();
                if (Checker.IsMovieAvailable(movies.ToArray(), movieInfo.Title, movieInfo.ReleaseDate?.Year.ToString()))
                {
                    return(Response.AsJson(new JsonResponseModel {
                        Result = false, Message = $"{fullMovieName} is already in Plex!"
                    }));
                }
            }
            catch (ApplicationSettingsException)
            {
                return(Response.AsJson(new JsonResponseModel {
                    Result = false, Message = $"We could not check if {fullMovieName} is in Plex, are you sure it's correctly setup?"
                }));
            }
            //#endif

            var model = new RequestedModel
            {
                ProviderId     = movieInfo.Id,
                Type           = RequestType.Movie,
                Overview       = movieInfo.Overview,
                ImdbId         = movieInfo.ImdbId,
                PosterPath     = "http://image.tmdb.org/t/p/w150/" + movieInfo.PosterPath,
                Title          = movieInfo.Title,
                ReleaseDate    = movieInfo.ReleaseDate ?? DateTime.MinValue,
                Status         = movieInfo.Status,
                RequestedDate  = DateTime.UtcNow,
                Approved       = false,
                RequestedUsers = new List <string> {
                    Username
                },
                Issues = IssueState.None,
            };

            Log.Trace(settings.DumpJson());
            if (ShouldAutoApprove(RequestType.Movie, settings))
            {
                var cpSettings = CpService.GetSettings();

                Log.Trace("Settings: ");
                Log.Trace(cpSettings.DumpJson);
                if (cpSettings.Enabled)
                {
                    Log.Info("Adding movie to CP (No approval required)");
                    var result = CouchPotatoApi.AddMovie(model.ImdbId, cpSettings.ApiKey, model.Title,
                                                         cpSettings.FullUri, cpSettings.ProfileId);
                    Log.Debug("Adding movie to CP result {0}", result);
                    if (result)
                    {
                        model.Approved = true;
                        Log.Info("Adding movie to database (No approval required)");
                        RequestService.AddRequest(model);


                        if (ShouldSendNotification())
                        {
                            var notificationModel = new NotificationModel
                            {
                                Title            = model.Title,
                                User             = Username,
                                DateTime         = DateTime.Now,
                                NotificationType = NotificationType.NewRequest
                            };
                            NotificationService.Publish(notificationModel);
                        }
                        return(Response.AsJson(new JsonResponseModel {
                            Result = true, Message = $"{fullMovieName} was successfully added!"
                        }));
                    }
                    return
                        (Response.AsJson(new JsonResponseModel
                    {
                        Result = false,
                        Message =
                            "Something went wrong adding the movie to CouchPotato! Please check your settings."
                    }));
                }
                else
                {
                    model.Approved = true;
                    Log.Info("Adding movie to database (No approval required)");
                    RequestService.AddRequest(model);

                    if (ShouldSendNotification())
                    {
                        var notificationModel = new NotificationModel
                        {
                            Title            = model.Title,
                            User             = Username,
                            DateTime         = DateTime.Now,
                            NotificationType = NotificationType.NewRequest
                        };
                        NotificationService.Publish(notificationModel);
                    }

                    return(Response.AsJson(new JsonResponseModel {
                        Result = true, Message = $"{fullMovieName} was successfully added!"
                    }));
                }
            }

            try
            {
                Log.Info("Adding movie to database");
                var id = RequestService.AddRequest(model);

                var notificationModel = new NotificationModel {
                    Title = model.Title, User = Username, DateTime = DateTime.Now, NotificationType = NotificationType.NewRequest
                };
                NotificationService.Publish(notificationModel);

                return(Response.AsJson(new JsonResponseModel {
                    Result = true, Message = $"{fullMovieName} was successfully added!"
                }));
            }
            catch (Exception e)
            {
                Log.Fatal(e);

                return(Response.AsJson(new JsonResponseModel {
                    Result = false, Message = "Something went wrong adding the movie to CouchPotato! Please check your settings."
                }));
            }
        }