Exemplo n.º 1
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.º 2
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.º 3
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));
        }
        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.º 5
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.º 6
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));
        }