/// <summary>
        /// TODO This is EXCATLY the same as the PlexAvailabilityChecker. Refactor Please future Jamie
        /// </summary>
        /// <returns></returns>
        private async Task ProcessTv()
        {
            var tv           = _tvRepo.GetChild().Where(x => !x.Available);
            var embyEpisodes = _repo.GetAllEpisodes().Include(x => x.Series);

            foreach (var child in tv)
            {
                IQueryable <EmbyEpisode> seriesEpisodes;
                if (child.ParentRequest.TvDbId > 0)
                {
                    seriesEpisodes = embyEpisodes.Where(x => x.Series.TvDbId == child.ParentRequest.TvDbId.ToString());
                }
                else if (child.ParentRequest.ImdbId.HasValue())
                {
                    seriesEpisodes = embyEpisodes.Where(x => x.Series.ImdbId == child.ParentRequest.ImdbId);
                }
                else
                {
                    continue;
                }

                foreach (var season in child.SeasonRequests)
                {
                    foreach (var episode in season.Episodes)
                    {
                        var foundEp = await seriesEpisodes.FirstOrDefaultAsync(
                            x => x.EpisodeNumber == episode.EpisodeNumber &&
                            x.SeasonNumber == episode.Season.SeasonNumber);

                        if (foundEp != null)
                        {
                            episode.Available = true;
                        }
                    }
                }

                // Check to see if all of the episodes in all seasons are available for this request
                var allAvailable = child.SeasonRequests.All(x => x.Episodes.All(c => c.Available));
                if (allAvailable)
                {
                    // We have fulfulled this request!
                    child.Available = true;
                    BackgroundJob.Enqueue(() => _notificationService.Publish(new NotificationOptions
                    {
                        DateTime         = DateTime.Now,
                        NotificationType = NotificationType.RequestAvailable,
                        RequestId        = child.ParentRequestId,
                        RequestType      = RequestType.TvShow,
                        Recipient        = child.RequestedUser.Email,
                    }));
                }
            }

            await _tvRepo.Save();
        }
Beispiel #2
0
        public Task <List <CalendarViewModel> > GetCalendarData()
        {
            var viewModel = new List <CalendarViewModel>();
            var movies    = _movieRepo.GetAll().Where(x =>
                                                      x.ReleaseDate > DaysAgo && x.ReleaseDate < DaysAhead);
            var episodes = _tvRepo.GetChild().SelectMany(x => x.SeasonRequests.SelectMany(e => e.Episodes
                                                                                          .Where(w => w.AirDate > DaysAgo && w.AirDate < DaysAhead)));

            foreach (var e in episodes)
            {
                viewModel.Add(new CalendarViewModel
                {
                    Title           = e.Title,
                    Start           = e.AirDate.Date,
                    Type            = RequestType.TvShow,
                    BackgroundColor = GetBackgroundColor(e),
                    ExtraParams     = new List <ExtraParams>
                    {
                        new ExtraParams
                        {
                            Overview      = e.Season?.ChildRequest?.ParentRequest?.Overview ?? string.Empty,
                            ProviderId    = e.Season?.ChildRequest?.ParentRequest?.ExternalProviderId ?? 0,
                            Type          = RequestType.TvShow,
                            ReleaseDate   = e.AirDate,
                            RequestStatus = e.RequestStatus
                        }
                    }
                });
            }

            foreach (var m in movies)
            {
                viewModel.Add(new CalendarViewModel
                {
                    Title           = m.Title,
                    Start           = m.ReleaseDate.Date,
                    BackgroundColor = GetBackgroundColor(m),
                    Type            = RequestType.Movie,
                    ExtraParams     = new List <ExtraParams>
                    {
                        new ExtraParams
                        {
                            Overview      = m.Overview,
                            ProviderId    = m.TheMovieDbId,
                            Type          = RequestType.Movie,
                            ReleaseDate   = m.ReleaseDate,
                            RequestStatus = m.RequestStatus
                        }
                    }
                });
            }

            return(Task.FromResult(viewModel));
        }
Beispiel #3
0
        private async Task ProcessTvRequests(DateTime date)
        {
            var requestsToDelete = await _tvRequestRepository.GetChild().Where(x => x.Available && x.MarkedAsAvailable.HasValue && x.MarkedAsAvailable.Value < date).ToListAsync();

            _logger.LogInformation($"Deleting {requestsToDelete.Count} episode requests that have now been scheduled for deletion, All available requests before {date::MM/dd/yyyy} will be deleted");

            await _tvRequestRepository.DeleteChildRange(requestsToDelete);

            // Check if we have parent requests without any child requests now
            var parentRequests = await _tvRequestRepository.Get().Where(x => !x.ChildRequests.Any()).ToListAsync();

            await _tvRequestRepository.DeleteRange(parentRequests);
        }
Beispiel #4
0
        public async Task <IEnumerable <FailedRequestViewModel> > GetFailedRequests()
        {
            var failed = await _requestQueueRepository.GetAll().Where(x => !x.Completed.HasValue).ToListAsync();

            var vm = new List <FailedRequestViewModel>();

            foreach (var f in failed)
            {
                var vmModel = new FailedRequestViewModel
                {
                    RequestId  = f.RequestId,
                    RetryCount = f.RetryCount,
                    Dts        = f.Dts,
                    Error      = f.Error,
                    FailedId   = f.Id,
                    Type       = f.Type
                };

                if (f.Type == RequestType.Movie)
                {
                    var request = await _movieRequestRepository.Find(f.RequestId);

                    vmModel.Title       = request.Title;
                    vmModel.ReleaseYear = request.ReleaseDate;
                }

                if (f.Type == RequestType.Album)
                {
                    var request = await _musicRequestRepository.Find(f.RequestId);

                    vmModel.Title       = request.Title;
                    vmModel.ReleaseYear = request.ReleaseDate;
                }

                if (f.Type == RequestType.TvShow)
                {
                    var request = await _tvRequestRepository.GetChild().Include(x => x.ParentRequest).FirstOrDefaultAsync(x => x.Id == f.RequestId);

                    vmModel.Title       = request.Title;
                    vmModel.ReleaseYear = request.ParentRequest.ReleaseDate;
                }
                vm.Add(vmModel);
            }

            return(vm);
        }
Beispiel #5
0
        public async Task Start()
        {
            // Get all the failed ones!
            var failedRequests = _requestQueue.GetAll().Where(x => !x.Completed.HasValue);

            foreach (var request in failedRequests)
            {
                if (request.Type == RequestType.Movie)
                {
                    var movieRequest = await _movieRequestRepository.GetAll().FirstOrDefaultAsync(x => x.Id == request.RequestId);

                    var result = await _movieSender.Send(movieRequest);

                    if (result.Success)
                    {
                        request.Completed = DateTime.UtcNow;
                        await _requestQueue.SaveChangesAsync();
                    }
                }
                if (request.Type == RequestType.TvShow)
                {
                    var tvRequest = await _tvRequestRepository.GetChild().FirstOrDefaultAsync(x => x.Id == request.RequestId);

                    var result = await _tvSender.Send(tvRequest);

                    if (result.Success)
                    {
                        request.Completed = DateTime.UtcNow;
                        await _requestQueue.SaveChangesAsync();
                    }
                }
                if (request.Type == RequestType.Album)
                {
                    var musicRequest = await _musicRequestRepository.GetAll().FirstOrDefaultAsync(x => x.Id == request.RequestId);

                    var result = await _musicSender.Send(musicRequest);

                    if (result.Success)
                    {
                        request.Completed = DateTime.UtcNow;
                        await _requestQueue.SaveChangesAsync();
                    }
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// TODO This is EXCATLY the same as the PlexAvailabilityChecker. Refactor Please future Jamie
        /// </summary>
        /// <returns></returns>
        private async Task ProcessTv()
        {
            var tv           = _tvRepo.GetChild().Where(x => !x.Available);
            var embyEpisodes = _repo.GetAllEpisodes().Include(x => x.Series);

            foreach (var child in tv)
            {
                var useImdb = false;
                var useTvDb = false;
                if (child.ParentRequest.ImdbId.HasValue())
                {
                    useImdb = true;
                }

                if (child.ParentRequest.TvDbId.ToString().HasValue())
                {
                    useTvDb = true;
                }

                var tvDbId = child.ParentRequest.TvDbId;
                var imdbId = child.ParentRequest.ImdbId;
                IQueryable <EmbyEpisode> seriesEpisodes = null;
                if (useImdb)
                {
                    seriesEpisodes = embyEpisodes.Where(x => x.Series.ImdbId == imdbId.ToString());
                }

                if (useTvDb && (seriesEpisodes == null || !seriesEpisodes.Any()))
                {
                    seriesEpisodes = embyEpisodes.Where(x => x.Series.TvDbId == tvDbId.ToString());
                }

                if (seriesEpisodes == null)
                {
                    continue;
                }

                if (!seriesEpisodes.Any())
                {
                    // Let's try and match the series by name
                    seriesEpisodes = embyEpisodes.Where(x =>
                                                        x.Series.Title.Equals(child.Title, StringComparison.CurrentCultureIgnoreCase));
                }

                foreach (var season in child.SeasonRequests)
                {
                    foreach (var episode in season.Episodes)
                    {
                        if (episode.Available)
                        {
                            continue;
                        }

                        var foundEp = await seriesEpisodes.FirstOrDefaultAsync(
                            x => x.EpisodeNumber == episode.EpisodeNumber &&
                            x.SeasonNumber == episode.Season.SeasonNumber);

                        if (foundEp != null)
                        {
                            episode.Available = true;
                        }
                    }
                }

                // Check to see if all of the episodes in all seasons are available for this request
                var allAvailable = child.SeasonRequests.All(x => x.Episodes.All(c => c.Available));
                if (allAvailable)
                {
                    // We have fulfulled this request!
                    child.Available = true;
                    BackgroundJob.Enqueue(() => _notificationService.Publish(new NotificationOptions
                    {
                        DateTime         = DateTime.Now,
                        NotificationType = NotificationType.RequestAvailable,
                        RequestId        = child.Id,
                        RequestType      = RequestType.TvShow,
                        Recipient        = child.RequestedUser.Email
                    }));
                }
            }

            await _tvRepo.Save();
        }
        public async Task ProcessTvShows()
        {
            var tv = await _tvRequest.GetChild().Where(x => !x.Available).ToListAsync();

            var sonarrEpisodes = _sonarrEpisodeRepo.GetAll().Where(x => x.HasFile);

            foreach (var child in tv)
            {
                var tvDbId = child.ParentRequest.TvDbId;
                IQueryable <SonarrEpisodeCache> seriesEpisodes = sonarrEpisodes.Where(x => x.TvDbId == tvDbId);

                if (seriesEpisodes == null || !seriesEpisodes.Any())
                {
                    continue;
                }

                //if (!seriesEpisodes.Any())
                //{
                //    // Let's try and match the series by name
                //    seriesEpisodes = sonarrEpisodes.Where(x =>
                //        x.EpisodeNumber == child.Title &&
                //        x.Series.ReleaseYear == child.ParentRequest.ReleaseDate.Year.ToString());

                //}

                var availableEpisode = new List <AvailabilityModel>();
                foreach (var season in child.SeasonRequests)
                {
                    foreach (var episode in season.Episodes)
                    {
                        if (episode.Available)
                        {
                            continue;
                        }
                        var foundEp = await seriesEpisodes.AnyAsync(
                            x => x.EpisodeNumber == episode.EpisodeNumber &&
                            x.SeasonNumber == episode.Season.SeasonNumber);

                        if (foundEp)
                        {
                            availableEpisode.Add(new AvailabilityModel
                            {
                                Id = episode.Id
                            });
                            episode.Available = true;
                        }
                    }
                }

                //TODO Partial avilability notifications here
                if (availableEpisode.Any())
                {
                    //await _hub.Clients.Clients(NotificationHub.AdminConnectionIds)
                    //    .SendAsync(NotificationHub.NotificationEvent, "Sonarr Availability Checker found some new available episodes!");
                    await _tvRequest.Save();
                }
                //foreach(var c in availableEpisode)
                //{
                //    await _tvRepo.MarkEpisodeAsAvailable(c.Id);
                //}

                // Check to see if all of the episodes in all seasons are available for this request
                var allAvailable = child.SeasonRequests.All(x => x.Episodes.All(c => c.Available));
                if (allAvailable)
                {
                    await _hub.Clients.Clients(NotificationHub.AdminConnectionIds)
                    .SendAsync(NotificationHub.NotificationEvent, "Sonarr Availability Checker found some new available Shows!");

                    child.Available         = true;
                    child.MarkedAsAvailable = DateTime.UtcNow;
                    _logger.LogInformation("[ARR_AC] - Child request {0} is now available, sending notification", $"{child.Title} - {child.Id}");
                    // We have ful-fulled this request!
                    await _tvRequest.Save();

                    await _notification.Notify(new NotificationOptions
                    {
                        DateTime         = DateTime.Now,
                        NotificationType = NotificationType.RequestAvailable,
                        RequestId        = child.Id,
                        RequestType      = RequestType.TvShow,
                        Recipient        = child.RequestedUser.Email
                    });
                }
            }

            await _tvRequest.Save();
        }
Beispiel #8
0
        public async Task Execute(IJobExecutionContext job)
        {
            // Get all the failed ones!
            var failedRequests = _requestQueue.GetAll().Where(x => x.Completed == null);

            foreach (var request in failedRequests)
            {
                if (request.Type == RequestType.Movie)
                {
                    var movieRequest = await _movieRequestRepository.GetAll().FirstOrDefaultAsync(x => x.Id == request.RequestId);

                    if (movieRequest == null)
                    {
                        await _requestQueue.Delete(request);

                        await _requestQueue.SaveChangesAsync();

                        continue;
                    }
                    var result = await _movieSender.Send(movieRequest);

                    if (result.Success)
                    {
                        request.Completed = DateTime.UtcNow;
                        await _requestQueue.SaveChangesAsync();
                    }
                }
                if (request.Type == RequestType.TvShow)
                {
                    var tvRequest = await _tvRequestRepository.GetChild().FirstOrDefaultAsync(x => x.Id == request.RequestId);

                    if (tvRequest == null)
                    {
                        await _requestQueue.Delete(request);

                        await _requestQueue.SaveChangesAsync();

                        continue;
                    }
                    var result = await _tvSender.Send(tvRequest);

                    if (result.Success)
                    {
                        request.Completed = DateTime.UtcNow;
                        await _requestQueue.SaveChangesAsync();
                    }
                }
                if (request.Type == RequestType.Album)
                {
                    var musicRequest = await _musicRequestRepository.GetAll().FirstOrDefaultAsync(x => x.Id == request.RequestId);

                    if (musicRequest == null)
                    {
                        await _requestQueue.Delete(request);

                        await _requestQueue.SaveChangesAsync();

                        continue;
                    }
                    var result = await _musicSender.Send(musicRequest);

                    if (result.Success)
                    {
                        request.Completed = DateTime.UtcNow;
                        await _requestQueue.SaveChangesAsync();
                    }
                }
            }
        }
Beispiel #9
0
        private Task ProcessTv()
        {
            var tv = _tvRepo.GetChild().Where(x => !x.Available);

            return(ProcessTv(tv));
        }
Beispiel #10
0
        /// <summary>
        /// TODO This is EXCATLY the same as the PlexAvailabilityChecker. Refactor Please future Jamie
        /// </summary>
        /// <returns></returns>
        private async Task ProcessTv()
        {
            var tv = _tvRepo.GetChild().Where(x => !x.Available);
            var jellyfinEpisodes = _repo.GetAllEpisodes().Include(x => x.Series);

            foreach (var child in tv)
            {
                var useImdb = false;
                var useTvDb = false;
                if (child.ParentRequest.ImdbId.HasValue())
                {
                    useImdb = true;
                }

                if (child.ParentRequest.TvDbId.ToString().HasValue())
                {
                    useTvDb = true;
                }

                var tvDbId = child.ParentRequest.TvDbId;
                var imdbId = child.ParentRequest.ImdbId;
                IQueryable <IMediaServerEpisode> seriesEpisodes = null;
                if (useImdb)
                {
                    seriesEpisodes = jellyfinEpisodes.Where(x => x.Series.ImdbId == imdbId.ToString());
                }

                if (useTvDb && (seriesEpisodes == null || !seriesEpisodes.Any()))
                {
                    seriesEpisodes = jellyfinEpisodes.Where(x => x.Series.TvDbId == tvDbId.ToString());
                }

                if (seriesEpisodes == null)
                {
                    continue;
                }

                if (!seriesEpisodes.Any())
                {
                    // Let's try and match the series by name
                    seriesEpisodes = jellyfinEpisodes.Where(x =>
                                                            x.Series.Title == child.Title);
                }

                var availableEpisode = new List <AvailabilityModel>();
                foreach (var season in child.SeasonRequests)
                {
                    foreach (var episode in season.Episodes)
                    {
                        if (episode.Available)
                        {
                            continue;
                        }

                        var foundEp = await seriesEpisodes.FirstOrDefaultAsync(
                            x => x.EpisodeNumber == episode.EpisodeNumber &&
                            x.SeasonNumber == episode.Season.SeasonNumber);

                        if (foundEp != null)
                        {
                            availableEpisode.Add(new AvailabilityModel
                            {
                                Id            = episode.Id,
                                EpisodeNumber = episode.EpisodeNumber,
                                SeasonNumber  = episode.Season.SeasonNumber
                            });
                            episode.Available = true;
                        }
                    }
                }

                if (availableEpisode.Any())
                {
                    await _tvRepo.Save();
                }

                // Check to see if all of the episodes in all seasons are available for this request
                var allAvailable = child.SeasonRequests.All(x => x.Episodes.All(c => c.Available));
                if (allAvailable)
                {
                    // We have fulfulled this request!
                    child.Available         = true;
                    child.MarkedAsAvailable = DateTime.Now;
                    await _notificationService.Notify(new NotificationOptions
                    {
                        DateTime         = DateTime.Now,
                        NotificationType = NotificationType.RequestAvailable,
                        RequestId        = child.Id,
                        RequestType      = RequestType.TvShow,
                        Recipient        = child.RequestedUser.Email
                    });
                }
                else if (availableEpisode.Any())
                {
                    var notification = new NotificationOptions
                    {
                        DateTime         = DateTime.Now,
                        NotificationType = NotificationType.PartiallyAvailable,
                        RequestId        = child.Id,
                        RequestType      = RequestType.TvShow,
                        Recipient        = child.RequestedUser.Email,
                    };
                    notification.Substitutes.Add("Season", availableEpisode.First().SeasonNumber.ToString());
                    notification.Substitutes.Add("Episodes", string.Join(", ", availableEpisode.Select(x => x.EpisodeNumber)));
                    await _notificationService.Notify(notification);
                }
            }

            await _tvRepo.Save();
        }
Beispiel #11
0
        private async Task ProcessTv()
        {
            var tv = await _tvRepo.GetChild().Where(x => !x.Available).ToListAsync();

            await ProcessTv(tv);
        }
Beispiel #12
0
        public async Task <IdentityResult> DeleteUser(OmbiUser userToDelete)
        {
            var userId = userToDelete.Id;
            // We need to delete all the requests first
            var moviesUserRequested     = _movieRepository.GetAll().Where(x => x.RequestedUserId == userId);
            var tvUserRequested         = _tvRepository.GetChild().Where(x => x.RequestedUserId == userId);
            var musicRequested          = _musicRepository.GetAll().Where(x => x.RequestedUserId == userId);
            var notificationPreferences = _userNotificationPreferences.GetAll().Where(x => x.UserId == userId);
            var userQuality             = await _userQualityProfiles.GetAll().FirstOrDefaultAsync(x => x.UserId == userId);

            if (moviesUserRequested.Any())
            {
                await _movieRepository.DeleteRange(moviesUserRequested);
            }
            if (tvUserRequested.Any())
            {
                await _tvRepository.DeleteChildRange(tvUserRequested);
            }
            if (musicRequested.Any())
            {
                await _musicRepository.DeleteRange(musicRequested);
            }
            if (notificationPreferences.Any())
            {
                await _userNotificationPreferences.DeleteRange(notificationPreferences);
            }
            if (userQuality != null)
            {
                await _userQualityProfiles.Delete(userQuality);
            }

            // Delete any issues and request logs
            var issues        = _issuesRepository.GetAll().Where(x => x.UserReportedId == userId);
            var issueComments = _issueCommentsRepository.GetAll().Where(x => x.UserId == userId);
            var requestLog    = _requestLogRepository.GetAll().Where(x => x.UserId == userId);

            if (issueComments.Any())
            {
                await _issueCommentsRepository.DeleteRange(issueComments);
            }
            if (issues.Any())
            {
                var extraComments = new List <IssueComments>();
                var issueIds      = issues.Select(x => x.Id).Distinct();
                foreach (var issue in issueIds)
                {
                    // Get all the comments for this issue and delete them, since the issue will be deleted
                    var extra = _issueCommentsRepository.GetAll().Where(x => x.IssuesId == issue);
                    extraComments.AddRange(extra.ToList());
                }
                await _issuesRepository.DeleteRange(issues);
            }
            if (requestLog.Any())
            {
                await _requestLogRepository.DeleteRange(requestLog);
            }

            // Delete the Subscriptions and mobile notification ids
            var subs       = _requestSubscriptionRepository.GetAll().Where(x => x.UserId == userId);
            var mobileIds  = _notificationRepository.GetAll().Where(x => x.UserId == userId);
            var votes      = _voteRepository.GetAll().Where(x => x.UserId == userId);
            var newMobiles = _mobileDevicesRepository.GetAll().Where(x => x.UserId == userId);

            if (subs.Any())
            {
                await _requestSubscriptionRepository.DeleteRange(subs);
            }
            if (mobileIds.Any())
            {
                await _notificationRepository.DeleteRange(mobileIds);
            }
            if (votes.Any())
            {
                await _voteRepository.DeleteRange(votes);
            }
            if (newMobiles.Any())
            {
                await _mobileDevicesRepository.DeleteRange(newMobiles);
            }

            var result = await _userManager.DeleteAsync(userToDelete);

            return(result);
        }