예제 #1
0
        private Negotiator CouchPotato()
        {
            dynamic model    = new ExpandoObject();
            var     settings = CpService.GetSettings();

            model = settings;

            return(View["CouchPotato", model]);
        }
        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."
            }));
        }
예제 #3
0
        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."
                }));
            }
        }
예제 #4
0
        private Negotiator CouchPotato()
        {
            var settings = CpService.GetSettings();

            return(View["CouchPotato", settings]);
        }
예제 #5
0
        private Response UpdateRequests(RequestedModel[] requestedModels)
        {
            var cpSettings      = CpService.GetSettings();
            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 TvSender(SonarrApi, SickRageApi);
                    var sr     = SickRageSettings.GetSettings();
                    var sonarr = SonarrSettings.GetSettings();
                    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 = 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 = Service.BatchUpdate(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!"
                }));
            }
        }
예제 #6
0
        // GET: Layout
        public ActionResult ApplicationMenu()
        {
            var model = new List <LayoutModel>();

            foreach (var app in Enum.GetValues(typeof(Applications)))
            {
                switch ((Applications)app)
                {
                case Applications.SabNZBD:
                    var sabService = SabService.GetSettings();
                    if (sabService.Enabled)
                    {
                        model.Add(
                            new LayoutModel {
                            Name = "SabNzbd", Url = "/SabNzbd"
                        });
                    }
                    break;

                case Applications.Sickbeard: break;

                case Applications.CouchPotato:
                    var cpService = CpService.GetSettings();
                    if (cpService.Enabled)
                    {
                        model.Add(
                            new LayoutModel {
                            Name = "CouchPotato", Url = "/CouchPotato"
                        });
                    }
                    break;

                case Applications.Kodi: break;

                case Applications.Sonarr:
                    var sonarrService = SonarrService.GetSettings();
                    if (sonarrService.Enabled)
                    {
                        model.Add(
                            new LayoutModel {
                            Name = "Sonarr", Url = "/Sonarr"
                        });
                    }
                    break;

                case Applications.Plex:
                    var plexService = PlexService.GetSettings();
                    if (plexService.Enabled)
                    {
                        model.Add(
                            new LayoutModel {
                            Name = "Plex", Url = "/Plex"
                        });
                    }
                    break;

                case Applications.NzbGet:
                    var nzbgetService = NzbService.GetSettings();
                    if (nzbgetService.Enabled)
                    {
                        model.Add(
                            new LayoutModel {
                            Name = "NzbGet", Url = "/NzbGet"
                        });
                    }
                    break;

                case Applications.Headphones: break;

                default:
                    throw new ArgumentOutOfRangeException("application");
                }
            }

            return(PartialView("NavBarItems", model));
        }
예제 #7
0
        private Response ProcessMovies(MovieSearchType searchType, string searchTerm)
        {
            List <Task> taskList   = new List <Task>();
            var         cpSettings = CpService.GetSettings();

            List <MovieResult> apiMovies = new List <MovieResult>();

            taskList.Add(Task.Factory.StartNew <List <MovieResult> >(() =>
            {
                switch (searchType)
                {
                case MovieSearchType.Search:
                    return(MovieApi.SearchMovie(searchTerm).Result.Select(x => new MovieResult()
                    {
                        Adult = x.Adult,
                        BackdropPath = x.BackdropPath,
                        GenreIds = x.GenreIds,
                        Id = x.Id,
                        OriginalLanguage = x.OriginalLanguage,
                        OriginalTitle = x.OriginalTitle,
                        Overview = x.Overview,
                        Popularity = x.Popularity,
                        PosterPath = x.PosterPath,
                        ReleaseDate = x.ReleaseDate,
                        Title = x.Title,
                        Video = x.Video,
                        VoteAverage = x.VoteAverage,
                        VoteCount = x.VoteCount
                    }).ToList());

                case MovieSearchType.CurrentlyPlaying:
                    return(MovieApi.GetCurrentPlayingMovies().Result.ToList());

                case MovieSearchType.Upcoming:
                    return(MovieApi.GetUpcomingMovies().Result.ToList());

                default:
                    return(new List <MovieResult>());
                }
            }).ContinueWith((t) =>
            {
                apiMovies = t.Result;
            }));

            Dictionary <int, RequestedModel> dbMovies = new Dictionary <int, RequestedModel>();

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

            Task.WaitAll(taskList.ToArray());

            int[] cpCached   = CpCacher.QueuedIds();
            var   plexMovies = Checker.GetPlexMovies();

            List <SearchMovieViewModel> viewMovies = new List <SearchMovieViewModel>();

            foreach (MovieResult movie in apiMovies)
            {
                var viewMovie = new SearchMovieViewModel()
                {
                    Adult            = movie.Adult,
                    BackdropPath     = movie.BackdropPath,
                    GenreIds         = movie.GenreIds,
                    Id               = movie.Id,
                    OriginalLanguage = movie.OriginalLanguage,
                    OriginalTitle    = movie.OriginalTitle,
                    Overview         = movie.Overview,
                    Popularity       = movie.Popularity,
                    PosterPath       = movie.PosterPath,
                    ReleaseDate      = movie.ReleaseDate,
                    Title            = movie.Title,
                    Video            = movie.Video,
                    VoteAverage      = movie.VoteAverage,
                    VoteCount        = movie.VoteCount
                };

                if (Checker.IsMovieAvailable(plexMovies.ToArray(), movie.Title, movie.ReleaseDate?.Year.ToString()))
                {
                    viewMovie.Available = true;
                }
                else if (dbMovies.ContainsKey(movie.Id)) // compare to the requests db
                {
                    var dbm = dbMovies[movie.Id];

                    viewMovie.Requested = true;
                    viewMovie.Approved  = dbm.Approved;
                    viewMovie.Available = dbm.Available;
                }
                else if (cpCached.Contains(movie.Id)) // compare to the couchpotato db
                {
                    viewMovie.Requested = true;
                }

                viewMovies.Add(viewMovie);
            }

            return(Response.AsJson(viewMovies));
        }