Example #1
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."
            }));
        }
Example #2
0
        private async Task <Response> UpdateRequestsAsync(RequestedModel[] requestedModels)
        {
            var cpSettings = await CpService.GetSettingsAsync();

            var updatedRequests = new List <RequestedModel>();

            foreach (var r in requestedModels)
            {
                if (r.Type == RequestType.Movie)
                {
                    if (cpSettings.Enabled)
                    {
                        var res = SendMovie(cpSettings, r, CpApi);
                        if (res)
                        {
                            r.Approved = true;
                            updatedRequests.Add(r);
                        }
                        else
                        {
                            Log.Error("Could not approve and send the movie {0} to couch potato!", r.Title);
                        }
                    }
                    else
                    {
                        r.Approved = true;
                        updatedRequests.Add(r);
                    }
                }
                if (r.Type == RequestType.TvShow)
                {
                    var sender = new TvSenderOld(SonarrApi, SickRageApi); // TODO put back
                    var sr     = await SickRageSettings.GetSettingsAsync();

                    var sonarr = await SonarrSettings.GetSettingsAsync();

                    if (sr.Enabled)
                    {
                        var res = sender.SendToSickRage(sr, r);
                        if (res?.result == "success")
                        {
                            r.Approved = true;
                            updatedRequests.Add(r);
                        }
                        else
                        {
                            Log.Error("Could not approve and send the TV {0} to SickRage!", r.Title);
                            Log.Error("SickRage Message: {0}", res?.message);
                        }
                    }

                    else if (sonarr.Enabled)
                    {
                        var res = await sender.SendToSonarr(sonarr, r);

                        if (!string.IsNullOrEmpty(res?.title))
                        {
                            r.Approved = true;
                            updatedRequests.Add(r);
                        }
                        else
                        {
                            Log.Error("Could not approve and send the TV {0} to Sonarr!", r.Title);
                            res?.ErrorMessages?.ForEach(x => Log.Error("Error messages: {0}", x));
                        }
                    }
                    else
                    {
                        r.Approved = true;
                        updatedRequests.Add(r);
                    }
                }
            }
            try
            {
                var result = await Service.BatchUpdateAsync(updatedRequests);

                return(Response.AsJson(result
                    ? new JsonResponseModel {
                    Result = true
                }
                    : new JsonResponseModel {
                    Result = false, Message = "We could not approve all of the requests. Please try again or check the logs."
                }));
            }
            catch (Exception e)
            {
                Log.Fatal(e);
                return(Response.AsJson(new JsonResponseModel {
                    Result = false, Message = "Something bad happened, please check the logs!"
                }));
            }
        }
Example #3
0
        private async Task <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");
            //#if !DEBUG

            var settings = await PrService.GetSettingsAsync();

            // check if the movie has already been requested
            Log.Info("Requesting movie with id {0}", movieId);
            var existingRequest = await RequestService.CheckRequestAsync(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);
                    await RequestService.UpdateRequestAsync(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 (Exception e)
            {
                Log.Error(e);
                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     = "https://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,
            };

            if (ShouldAutoApprove(RequestType.Movie, settings))
            {
                var cpSettings = await CpService.GetSettingsAsync();

                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)");
                        await RequestService.AddRequestAsync(model);

                        if (ShouldSendNotification())
                        {
                            var notificationModel = new NotificationModel
                            {
                                Title            = model.Title,
                                User             = Username,
                                DateTime         = DateTime.Now,
                                NotificationType = NotificationType.NewRequest
                            };
                            await 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)");
                    await RequestService.AddRequestAsync(model);

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

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

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

                var notificationModel = new NotificationModel {
                    Title = model.Title, User = Username, DateTime = DateTime.Now, NotificationType = NotificationType.NewRequest
                };
                await 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."
                }));
            }
        }