private Response GetSickRageSettings()
        {
            var model = new ApiModel <SickRageSettings>();

            try
            {
                var settings = SickRageSettings.GetSettings();
                model.Data = settings;
                return(ReturnReponse(model));
            }
            catch (Exception e)
            {
                model.ErrorMessage = e.Message;
                model.Error        = true;
                return(ReturnReponse(model));
            }
        }
        private Response GetTvShows() // TODO: async await the API calls
        {
            var settings = PrSettings.GetSettings();

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

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

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

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

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

            if (IsAdmin)
            {
                var sonarrSettings = SonarrSettings.GetSettings();
                if (sonarrSettings.Enabled)
                {
                    taskList.Add(Task.Factory.StartNew(() =>
                    {
                        return(Cache.GetOrSet(CacheKeys.SonarrQualityProfiles, () =>
                        {
                            return SonarrApi.GetProfiles(sonarrSettings.ApiKey, sonarrSettings.FullUri); // TODO: cache this!
                        }));
                    }).ContinueWith((t) =>
                    {
                        qualities = t.Result.Select(x => new QualityModel()
                        {
                            Id = x.id.ToString(), Name = x.name
                        }).ToList();
                    }));
                }
                else
                {
                    var sickRageSettings = SickRageSettings.GetSettings();
                    if (sickRageSettings.Enabled)
                    {
                        qualities = sickRageSettings.Qualities.Select(x => new QualityModel()
                        {
                            Id = x.Key, Name = x.Value
                        }).ToList();
                    }
                }
            }

            Task.WaitAll(taskList.ToArray());

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

            return(Response.AsJson(viewModel));
        }
        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!"
                }));
            }
        }
        private Response RequestTvAndUpdateStatus(RequestedModel request, string qualityId)
        {
            var sender = new TvSender(SonarrApi, SickRageApi);

            var sonarrSettings = SonarrSettings.GetSettings();

            if (sonarrSettings.Enabled)
            {
                Log.Trace("Sending to Sonarr");
                var result = sender.SendToSonarr(sonarrSettings, request, qualityId);
                Log.Trace("Sonarr Result: ");
                Log.Trace(result.DumpJson());
                if (!string.IsNullOrEmpty(result.title))
                {
                    Log.Info("Sent successfully, Approving request now.");
                    request.Approved = true;
                    var requestResult = Service.UpdateRequest(request);
                    Log.Trace("Approval result: {0}", requestResult);
                    if (requestResult)
                    {
                        return(Response.AsJson(new JsonResponseModel {
                            Result = true
                        }));
                    }
                    return
                        (Response.AsJson(new JsonResponseModel
                    {
                        Result = false,
                        Message = "Updated Sonarr but could not approve it in PlexRequests :("
                    }));
                }
                return(Response.AsJson(ValidationHelper.SendSonarrError(result.ErrorMessages)));
            }

            var srSettings = SickRageSettings.GetSettings();

            if (srSettings.Enabled)
            {
                Log.Trace("Sending to SickRage");
                var result = sender.SendToSickRage(srSettings, request, qualityId);
                Log.Trace("SickRage Result: ");
                Log.Trace(result.DumpJson());
                if (result?.result == "success")
                {
                    Log.Info("Sent successfully, Approving request now.");
                    request.Approved = true;
                    var requestResult = Service.UpdateRequest(request);
                    Log.Trace("Approval result: {0}", requestResult);
                    return(Response.AsJson(requestResult
                        ? new JsonResponseModel {
                        Result = true
                    }
                        : new JsonResponseModel {
                        Result = false, Message = "Updated SickRage but could not approve it in PlexRequests :("
                    }));
                }
                return(Response.AsJson(new JsonResponseModel
                {
                    Result = false,
                    Message = result?.message != null ? "<b>Message From SickRage: </b>" + result.message : "Could not add the series to SickRage"
                }));
            }


            request.Approved = true;
            var res = Service.UpdateRequest(request);

            return(Response.AsJson(res
                ? new JsonResponseModel {
                Result = true, Message = "This has been approved, but It has not been sent to Sonarr/SickRage because it has not been configured"
            }
                : new JsonResponseModel {
                Result = false, Message = "Updated SickRage but could not approve it in PlexRequests :("
            }));
        }
Exemple #5
0
        private async Task <Response> GetTvShows()
        {
            var settings = PrSettings.GetSettings();

            var requests = await Service.GetAllAsync();

            requests = requests.Where(x => x.Type == RequestType.TvShow);

            var dbTv = requests;

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

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

            if (IsAdmin)
            {
                var sonarrSettings = SonarrSettings.GetSettings();
                if (sonarrSettings.Enabled)
                {
                    var result = Cache.GetOrSetAsync(CacheKeys.SonarrQualityProfiles, async() =>
                    {
                        return(await Task.Run(() => SonarrApi.GetProfiles(sonarrSettings.ApiKey, sonarrSettings.FullUri)));
                    });
                    qualities = result.Result.Select(x => new QualityModel()
                    {
                        Id = x.id.ToString(), Name = x.name
                    }).ToList();
                }
                else
                {
                    var sickRageSettings = SickRageSettings.GetSettings();
                    if (sickRageSettings.Enabled)
                    {
                        qualities = sickRageSettings.Qualities.Select(x => new QualityModel()
                        {
                            Id = x.Key, Name = x.Value
                        }).ToList();
                    }
                }
            }

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

            return(Response.AsJson(viewModel));
        }