You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ombi/src/Ombi.Schedule/Jobs/Plex/PlexAvailabilityChecker.cs

84 lines
2.7 KiB

using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Ombi.Store.Repository;
using Ombi.Store.Repository.Requests;
namespace Ombi.Schedule.Jobs.Plex
{
public class PlexAvailabilityChecker : IPlexAvailabilityChecker
{
public PlexAvailabilityChecker(IPlexContentRepository repo, ITvRequestRepository tvRequest, IMovieRequestRepository movies)
{
_tvRepo = tvRequest;
_repo = repo;
_movieRepo = movies;
}
private readonly ITvRequestRepository _tvRepo;
private readonly IMovieRequestRepository _movieRepo;
private readonly IPlexContentRepository _repo;
public async Task Start()
{
await ProcessMovies();
await ProcessTv();
}
private async Task ProcessTv()
{
var tv = _tvRepo.GetChild().Where(x => !x.Available);
var plexEpisodes = _repo.GetAllEpisodes().Include(x => x.Series);
foreach (var child in tv)
{
var tvDbId = child.ParentRequest.TvDbId;
var seriesEpisodes = plexEpisodes.Where(x => x.Series.ProviderId == tvDbId.ToString());
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;
}
}
await _tvRepo.Save();
}
private async Task ProcessMovies()
{
// Get all non available
var movies = _movieRepo.Get().Where(x => !x.Available);
foreach (var movie in movies)
{
var plexContent = await _repo.Get(movie.ImdbId);
if (plexContent == null)
{
// We don't yet have this
continue;
}
movie.Available = true;
}
await _movieRepo.Save();
}
}
}