From 63fd43b1d8dd28d39d0c865f7ed4e7163f05c9d0 Mon Sep 17 00:00:00 2001 From: Jamie Rees Date: Thu, 30 Jul 2020 09:03:36 +0100 Subject: [PATCH] wip --- .../Jobs/ArrAvailabilityChecker.cs | 103 ++++++++++++++++++ src/Ombi.Schedule/Jobs/Radarr/RadarrSync.cs | 26 ++--- src/Ombi.Schedule/Jobs/Sonarr/SonarrSync.cs | 53 +++++++-- 3 files changed, 159 insertions(+), 23 deletions(-) create mode 100644 src/Ombi.Schedule/Jobs/ArrAvailabilityChecker.cs diff --git a/src/Ombi.Schedule/Jobs/ArrAvailabilityChecker.cs b/src/Ombi.Schedule/Jobs/ArrAvailabilityChecker.cs new file mode 100644 index 000000000..2294fde9e --- /dev/null +++ b/src/Ombi.Schedule/Jobs/ArrAvailabilityChecker.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Logging; +using Ombi.Core; +using Ombi.Helpers; +using Ombi.Hubs; +using Ombi.Notifications.Models; +using Ombi.Schedule.Jobs.Plex.Models; +using Ombi.Store.Entities; +using Ombi.Store.Repository; +using Ombi.Store.Repository.Requests; +using Quartz; + +namespace Ombi.Schedule.Jobs.Radarr +{ + public class ArrAvailabilityChecker + { + public ArrAvailabilityChecker( + IExternalRepository radarrRepo, + IExternalRepository sonarrRepo, + IExternalRepository sonarrEpisodeRepo, + INotificationHelper notification, IHubContext hub, + ITvRequestRepository tvRequest, IMovieRequestRepository movies, + ILogger log) + { + _radarrRepo = radarrRepo; + _sonarrRepo = sonarrRepo; + _sonarrEpisodeRepo = sonarrEpisodeRepo; + _notification = notification; + _hub = hub; + _tvRequest = tvRequest; + _movies = movies; + _logger = log; + } + + private readonly IExternalRepository _radarrRepo; + private readonly IExternalRepository _sonarrRepo; + private readonly ILogger _logger; + private readonly IExternalRepository _sonarrEpisodeRepo; + private readonly INotificationHelper _notification; + private readonly IHubContext _hub; + private readonly ITvRequestRepository _tvRequest; + private readonly IMovieRequestRepository _movies; + + public async Task Execute(IJobExecutionContext job) + { + + await ProcessMovies(); + await ProcessTvShows(); + + } + + private async Task ProcessMovies() + { + var availableRadarrMovies = _radarrRepo.GetAll().Where(x => x.HasFile).ToImmutableHashSet(); + var unavailableMovieRequests = _movies.GetAll().Where(x => !x.Available).ToImmutableHashSet(); + + var itemsForAvailability = new List(); + foreach (var movieRequest in unavailableMovieRequests) + { + // Do we have an item in the radarr list + var available = availableRadarrMovies.Any(x => x.TheMovieDbId == movieRequest.TheMovieDbId); + if (available) + { + movieRequest.Available = true; + movieRequest.MarkedAsAvailable = DateTime.UtcNow; + itemsForAvailability.Add(new AvailabilityModel + { + Id = movieRequest.Id, + RequestedUser = movieRequest.RequestedUser != null ? movieRequest.RequestedUser.Email : string.Empty + }); + } + } + + if (itemsForAvailability.Any()) + { + await _movies.SaveChangesAsync(); + } + foreach (var item in itemsForAvailability) + { + await _notification.Notify(new NotificationOptions + { + DateTime = DateTime.Now, + NotificationType = NotificationType.RequestAvailable, + RequestId = item.Id, + RequestType = RequestType.Movie, + Recipient = item.RequestedUser + }); + } + + } + + public async Task ProcessTvShows() + { + + } + + } +} diff --git a/src/Ombi.Schedule/Jobs/Radarr/RadarrSync.cs b/src/Ombi.Schedule/Jobs/Radarr/RadarrSync.cs index 3a8aca3d7..54f77e253 100644 --- a/src/Ombi.Schedule/Jobs/Radarr/RadarrSync.cs +++ b/src/Ombi.Schedule/Jobs/Radarr/RadarrSync.cs @@ -49,29 +49,29 @@ namespace Ombi.Schedule.Jobs.Radarr // Let's remove the old cached data using (var tran = await _ctx.Database.BeginTransactionAsync()) { - await _ctx.Database.ExecuteSqlCommandAsync("DELETE FROM RadarrCache"); + await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM RadarrCache"); tran.Commit(); } var movieIds = new List(); foreach (var m in movies) { - if(m.monitored) - { - if (m.tmdbId > 0) + if (m.monitored || m.hasFile) { - movieIds.Add(new RadarrCache + if (m.tmdbId > 0) { - TheMovieDbId = m.tmdbId, - HasFile = m.hasFile - }); - } - else - { - Logger.LogError("TMDBId is not > 0 for movie {0}", m.title); + movieIds.Add(new RadarrCache + { + TheMovieDbId = m.tmdbId, + HasFile = m.hasFile + }); + } + else + { + Logger.LogError("TMDBId is not > 0 for movie {0}", m.title); + } } } - } using (var tran = await _ctx.Database.BeginTransactionAsync()) { diff --git a/src/Ombi.Schedule/Jobs/Sonarr/SonarrSync.cs b/src/Ombi.Schedule/Jobs/Sonarr/SonarrSync.cs index cb431d1d5..2668c283f 100644 --- a/src/Ombi.Schedule/Jobs/Sonarr/SonarrSync.cs +++ b/src/Ombi.Schedule/Jobs/Sonarr/SonarrSync.cs @@ -33,7 +33,7 @@ namespace Ombi.Schedule.Jobs.Sonarr private readonly ISonarrApi _api; private readonly ILogger _log; private readonly ExternalContext _ctx; - + public async Task Execute(IJobExecutionContext job) { try @@ -50,48 +50,81 @@ namespace Ombi.Schedule.Jobs.Sonarr var ids = sonarrSeries.Select(x => x.tvdbId); using (var tran = await _ctx.Database.BeginTransactionAsync()) { - await _ctx.Database.ExecuteSqlCommandAsync("DELETE FROM SonarrCache"); + await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM SonarrCache"); tran.Commit(); } + var existingSeries = await _ctx.SonarrCache.Select(x => x.TvDbId).ToListAsync(); + + //var entites = ids.Except(existingSeries).Select(id => new SonarrCache { TvDbId = id }).ToImmutableHashSet(); var entites = ids.Select(id => new SonarrCache { TvDbId = id }).ToImmutableHashSet(); await _ctx.SonarrCache.AddRangeAsync(entites); entites.Clear(); using (var tran = await _ctx.Database.BeginTransactionAsync()) { - await _ctx.Database.ExecuteSqlCommandAsync("DELETE FROM SonarrEpisodeCache"); + await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM SonarrEpisodeCache"); tran.Commit(); } foreach (var s in sonarrSeries) { - if (!s.monitored) + if (!s.monitored || s.episodeFileCount > 0) // We have files { continue; } + _log.LogDebug("Syncing series: {0}", s.title); var episodes = await _api.GetEpisodes(s.id, settings.ApiKey, settings.FullUri); var monitoredEpisodes = episodes.Where(x => x.monitored || x.hasFile); - + + var allExistingEpisodes = await _ctx.SonarrEpisodeCache.Where(x => x.TvDbId == s.tvdbId).ToListAsync(); // Add to DB _log.LogDebug("We have the episodes, adding to db transaction"); - using (var tran = await _ctx.Database.BeginTransactionAsync()) - { - await _ctx.SonarrEpisodeCache.AddRangeAsync(monitoredEpisodes.Select(episode => + var episodesToAdd = monitoredEpisodes.Select(episode => new SonarrEpisodeCache { EpisodeNumber = episode.episodeNumber, SeasonNumber = episode.seasonNumber, TvDbId = s.tvdbId, HasFile = episode.hasFile - })); + }); + //var episodesToAdd = new List(); + + //foreach (var monitored in monitoredEpisodes) + //{ + // var existing = allExistingEpisodes.FirstOrDefault(x => x.SeasonNumber == monitored.seasonNumber && x.EpisodeNumber == monitored.episodeNumber); + // if (existing == null) + // { + // // Just add a new one + // episodesToAdd.Add(new SonarrEpisodeCache + // { + // EpisodeNumber = monitored.episodeNumber, + // SeasonNumber = monitored.seasonNumber, + // TvDbId = s.tvdbId, + // HasFile = monitored.hasFile + // }); + // } + // else + // { + // // Do we need to update the availability? + // if (monitored.hasFile != existing.HasFile) + // { + // existing.HasFile = monitored.hasFile; + // } + // } + + //} + + using (var tran = await _ctx.Database.BeginTransactionAsync()) + { + await _ctx.SonarrEpisodeCache.AddRangeAsync(episodesToAdd); _log.LogDebug("Commiting the transaction"); await _ctx.SaveChangesAsync(); tran.Commit(); } } - + } } catch (Exception e)