Added the Emby Cacher, we now cache the Emby data!

pull/1510/head
tidusjar 7 years ago
parent 6d276a3c3d
commit 012a82ca2d

@ -66,6 +66,14 @@ namespace Ombi.Api.Emby
return obj;
}
public async Task<EmbyItemContainer<MovieInformation>> GetCollection(string mediaId, string apiKey, string userId, string baseUrl)
{
var request = new Request($"emby/users/{userId}/items?parentId={mediaId}", baseUrl, HttpMethod.Get);
AddHeaders(request, apiKey);
return await Api.Request<EmbyItemContainer<MovieInformation>>(request);
}
public async Task<EmbyItemContainer<EmbyMovie>> GetAllMovies(string apiKey, string userId, string baseUri)
{
return await GetAll<EmbyMovie>("Movie", apiKey, userId, baseUri);

@ -17,6 +17,9 @@ namespace Ombi.Api.Emby
Task<EmbyItemContainer<EmbyEpisodes>> GetAllEpisodes(string apiKey, string userId, string baseUri);
Task<EmbyItemContainer<EmbySeries>> GetAllShows(string apiKey, string userId, string baseUri);
Task<EmbyItemContainer<MovieInformation>> GetCollection(string mediaId, string apiKey, string userId,
string baseUrl);
Task<SeriesInformation> GetSeriesInformation(string mediaId, string apiKey, string userId, string baseUrl);
Task<MovieInformation> GetMovieInformation(string mediaId, string apiKey, string userId, string baseUrl);
Task<EpisodeInformation> GetEpisodeInformation(string mediaId, string apiKey, string userId, string baseUrl);

@ -13,7 +13,7 @@ namespace Ombi.Core.Models.Search
/// <summary>
/// This is used for the PlexAvailabilityCheck rule
/// This is used for the PlexAvailabilityCheck/EmbyAvailabilityRule rule
/// </summary>
/// <value>
/// The custom identifier.

@ -38,4 +38,8 @@
<Folder Include="Models\Requests\Tv\" />
</ItemGroup>
<ItemGroup>
<None Include="Rule\Rules\Search\EmbyAvailabilityRule.cs" />
</ItemGroup>
</Project>

@ -0,0 +1,27 @@
using System.Threading.Tasks;
using Ombi.Core.Models.Search;
using Ombi.Core.Rule.Interfaces;
using Ombi.Store.Repository;
namespace Ombi.Core.Rule.Rules.Search
{
public class EmbyAvailabilityRule : BaseSearchRule, IRules<SearchViewModel>
{
public EmbyAvailabilityRule(IEmbyContentRepository repo)
{
EmbyContentRepository = repo;
}
private IEmbyContentRepository EmbyContentRepository { get; }
public async Task<RuleResult> Execute(SearchViewModel obj)
{
var item = await EmbyContentRepository.Get(obj.CustomId);
if (item != null)
{
obj.Available = true;
}
return Success();
}
}
}

@ -36,6 +36,7 @@ using Ombi.Api.Service;
using Ombi.Api.Slack;
using Ombi.Core.Rule.Interfaces;
using Ombi.Core.Senders;
using Ombi.Schedule.Jobs.Emby;
using Ombi.Schedule.Jobs.Ombi;
using Ombi.Schedule.Jobs.Plex;
using Ombi.Schedule.Ombi;
@ -63,7 +64,7 @@ namespace Ombi.DependencyInjection
services.AddTransient<IMovieRequestEngine, MovieRequestEngine>();
services.AddTransient<ITvRequestEngine, TvRequestEngine>();
services.AddTransient<ITvSearchEngine, TvSearchEngine>();
services.AddSingleton<IRuleEvaluator, RuleEvaluator>();
services.AddTransient<IRuleEvaluator, RuleEvaluator>();
services.AddTransient<IMovieSender, MovieSender>();
services.AddTransient<ITvSender, TvSender>();
}
@ -95,6 +96,7 @@ namespace Ombi.DependencyInjection
services.AddTransient<ISettingsRepository, SettingsJsonRepository>();
services.AddTransient<ISettingsResolver, SettingsResolver>();
services.AddTransient<IPlexContentRepository, PlexContentRepository>();
services.AddTransient<IEmbyContentRepository, EmbyContentRepository>();
services.AddTransient<INotificationTemplatesRepository, NotificationTemplatesRepository>();
services.AddTransient<ITvRequestRepository, TvRequestRepository>();
@ -124,6 +126,7 @@ namespace Ombi.DependencyInjection
public static void RegisterJobs(this IServiceCollection services)
{
services.AddTransient<IPlexContentCacher, PlexContentCacher>();
services.AddTransient<IEmbyContentCacher, EmbyContentCacher>();
services.AddTransient<IPlexEpisodeCacher, PlexEpisodeCacher>();
services.AddTransient<IPlexAvailabilityChecker, PlexAvailabilityChecker>();
services.AddTransient<IJobSetup, JobSetup>();

@ -12,6 +12,7 @@ namespace Ombi.Helpers
public static EventId Cacher => new EventId(2000);
public static EventId RadarrCacher => new EventId(2001);
public static EventId PlexEpisodeCacher => new EventId(2001);
public static EventId EmbyContentCacher => new EventId(2002);
public static EventId MovieSender => new EventId(3000);

@ -1,5 +1,6 @@
using Hangfire;
using Ombi.Schedule.Jobs;
using Ombi.Schedule.Jobs.Emby;
using Ombi.Schedule.Jobs.Radarr;
using Ombi.Schedule.Ombi;
@ -7,19 +8,24 @@ namespace Ombi.Schedule
{
public class JobSetup : IJobSetup
{
public JobSetup(IPlexContentCacher cacher, IRadarrCacher radarrCacher, IOmbiAutomaticUpdater updater)
public JobSetup(IPlexContentCacher plexContentCacher, IRadarrCacher radarrCacher,
IOmbiAutomaticUpdater updater, IEmbyContentCacher embyCacher)
{
Cacher = cacher;
PlexContentCacher = plexContentCacher;
RadarrCacher = radarrCacher;
Updater = updater;
EmbyContentCacher = embyCacher;
}
private IPlexContentCacher Cacher { get; }
private IPlexContentCacher PlexContentCacher { get; }
private IRadarrCacher RadarrCacher { get; }
private IOmbiAutomaticUpdater Updater { get; }
private IEmbyContentCacher EmbyContentCacher { get; }
public void Setup()
{
RecurringJob.AddOrUpdate(() => Cacher.CacheContent(), Cron.Hourly);
RecurringJob.AddOrUpdate(() => PlexContentCacher.CacheContent(), Cron.Hourly);
RecurringJob.AddOrUpdate(() => EmbyContentCacher.Start(), Cron.Hourly);
RecurringJob.AddOrUpdate(() => RadarrCacher.CacheContent(), Cron.Hourly);
//RecurringJob.AddOrUpdate(() => Updater.Update(), Cron.Daily);

@ -1,32 +1,140 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Ombi.Api.Emby;
using Ombi.Api.Emby.Models.Movie;
using Ombi.Core.Settings;
using Ombi.Core.Settings.Models.External;
using Ombi.Helpers;
using Ombi.Store.Entities;
using Ombi.Store.Repository;
using Serilog;
using EmbyMediaType = Ombi.Store.Entities.EmbyMediaType;
namespace Ombi.Schedule.Jobs.Emby
{
public class EmbyContentCacher
public class EmbyContentCacher : IEmbyContentCacher
{
public EmbyContentCacher(ISettingsService<EmbySettings> settings, IEmbyApi api, ILogger<EmbyContentCacher> logger)
public EmbyContentCacher(ISettingsService<EmbySettings> settings, IEmbyApi api, ILogger<EmbyContentCacher> logger,
IEmbyContentRepository repo)
{
_logger = logger;
_settings = settings;
_api = api;
_repo = repo;
}
private readonly ILogger<EmbyContentCacher> _logger;
private readonly ISettingsService<EmbySettings> _settings;
private readonly IEmbyApi _api;
private readonly IEmbyContentRepository _repo;
public async Task Start()
{
var embySettings = await _settings.GetSettingsAsync();
if (!embySettings.Enable)
return;
foreach (var server in embySettings.Servers)
await StartServerCache(server);
// Episodes
//BackgroundJob.Enqueue(() => );
}
private bool ValidateSettings(EmbySettings emby)
private async Task StartServerCache(EmbyServers server)
{
if (emby.Enable)
if (!ValidateSettings(server))
return;
var movies = await _api.GetAllMovies(server.ApiKey, server.AdministratorId, server.FullUri);
var mediaToAdd = new List<EmbyContent>();
foreach (var movie in movies.Items)
{
foreach (var server in emby.Servers)
if (movie.Type.Equals("boxset", StringComparison.CurrentCultureIgnoreCase))
{
if (server?.Ip == null || string.IsNullOrEmpty(server?.ApiKey))
var movieInfo =
await _api.GetCollection(movie.Id, server.ApiKey, server.AdministratorId, server.FullUri);
foreach (var item in movieInfo.Items)
{
//Log.Warn("A setting is null, Ensure Emby is configured correctly, and we have a Emby Auth token.");
return false;
var info = await _api.GetMovieInformation(item.Id, server.ApiKey,
server.AdministratorId, server.FullUri);
await ProcessMovies(info, mediaToAdd);
}
}
else
{
// Regular movie
var movieInfo = await _api.GetMovieInformation(movie.Id, server.ApiKey,
server.AdministratorId, server.FullUri);
await ProcessMovies(movieInfo, mediaToAdd);
}
}
return emby.Enable;
// TV Time
var tv = await _api.GetAllShows(server.ApiKey, server.AdministratorId, server.FullUri);
foreach (var tvShow in tv.Items)
{
var tvInfo = await _api.GetSeriesInformation(tvShow.Id, server.ApiKey, server.AdministratorId,
server.FullUri);
if (string.IsNullOrEmpty(tvInfo.ProviderIds?.Tvdb))
{
Log.Error("Provider Id on tv {0} is null", tvShow.Name);
continue;
}
var existingTv = await _repo.GetByEmbyId(tvShow.Id);
if (existingTv == null)
mediaToAdd.Add(new EmbyContent
{
ProviderId = tvInfo.ProviderIds.Tvdb,
Title = tvInfo.Name,
Type = EmbyMediaType.Series,
EmbyId = tvShow.Id,
AddedAt = DateTime.UtcNow
});
}
if (mediaToAdd.Any())
await _repo.AddRange(mediaToAdd);
}
private async Task ProcessMovies(MovieInformation movieInfo, ICollection<EmbyContent> content)
{
if (string.IsNullOrEmpty(movieInfo.ProviderIds.Imdb))
{
Log.Error("Provider Id on movie {0} is null", movieInfo.Name);
return;
}
// Check if it exists
var existingMovie = await _repo.GetByEmbyId(movieInfo.Id);
if (existingMovie == null)
content.Add(new EmbyContent
{
ProviderId = movieInfo.ProviderIds.Imdb,
Title = movieInfo.Name,
Type = EmbyMediaType.Movie,
EmbyId = movieInfo.Id,
AddedAt = DateTime.UtcNow,
});
}
private bool ValidateSettings(EmbyServers server)
{
if (server?.Ip == null || string.IsNullOrEmpty(server?.ApiKey))
{
_logger.LogInformation(LoggingEvents.EmbyContentCacher, $"Server {server?.Name} is not configured correctly");
return false;
}
return true;
}
}
}

@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace Ombi.Schedule.Jobs.Emby
{
public interface IEmbyContentCacher
{
Task Start();
}
}

@ -17,6 +17,7 @@ namespace Ombi.Store.Context
DbSet<PlexContent> PlexContent { get; set; }
DbSet<PlexEpisode> PlexEpisode { get; set; }
DbSet<RadarrCache> RadarrCache { get; set; }
DbSet<EmbyContent> EmbyContent { get; set; }
DatabaseFacade Database { get; }
EntityEntry<T> Entry<T>(T entry) where T : class;
EntityEntry<TEntity> Attach<TEntity>(TEntity entity) where TEntity : class;

@ -28,7 +28,8 @@ namespace Ombi.Store.Context
public DbSet<PlexContent> PlexContent { get; set; }
public DbSet<PlexEpisode> PlexEpisode { get; set; }
public DbSet<RadarrCache> RadarrCache { get; set; }
public DbSet<EmbyContent> EmbyContent { get; set; }
public DbSet<MovieRequests> MovieRequests { get; set; }
public DbSet<TvRequests> TvRequests { get; set; }
public DbSet<ChildRequests> ChildRequests { get; set; }

@ -0,0 +1,49 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2017 Jamie Rees
// File: EmbyContent.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System;
using System.ComponentModel.DataAnnotations.Schema;
namespace Ombi.Store.Entities
{
[Table("EmbyContent")]
public class EmbyContent : Entity
{
public string Title { get; set; }
public string ProviderId { get; set; }
public string EmbyId { get; set; }
public EmbyMediaType Type { get; set; }
public DateTime AddedAt { get; set; }
}
public enum EmbyMediaType
{
Movie = 0,
Series = 1,
Music = 2
}
}

@ -10,7 +10,7 @@ using Ombi.Helpers;
namespace Ombi.Store.Migrations
{
[DbContext(typeof(OmbiContext))]
[Migration("20170825114646_Inital")]
[Migration("20170901230032_Inital")]
partial class Inital
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -159,6 +159,26 @@ namespace Ombi.Store.Migrations
b.ToTable("Audit");
});
modelBuilder.Entity("Ombi.Store.Entities.EmbyContent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<DateTime>("AddedAt");
b.Property<string>("EmbyId");
b.Property<string>("ProviderId");
b.Property<string>("Title");
b.Property<int>("Type");
b.HasKey("Id");
b.ToTable("EmbyContent");
});
modelBuilder.Entity("Ombi.Store.Entities.GlobalSettings", b =>
{
b.Property<int>("Id")

@ -67,6 +67,23 @@ namespace Ombi.Store.Migrations
table.PrimaryKey("PK_Audit", x => x.Id);
});
migrationBuilder.CreateTable(
name: "EmbyContent",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
AddedAt = table.Column<DateTime>(nullable: false),
EmbyId = table.Column<string>(nullable: true),
ProviderId = table.Column<string>(nullable: true),
Title = table.Column<string>(nullable: true),
Type = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_EmbyContent", x => x.Id);
});
migrationBuilder.CreateTable(
name: "GlobalSettings",
columns: table => new
@ -621,6 +638,9 @@ namespace Ombi.Store.Migrations
migrationBuilder.DropTable(
name: "Audit");
migrationBuilder.DropTable(
name: "EmbyContent");
migrationBuilder.DropTable(
name: "GlobalSettings");

@ -158,6 +158,26 @@ namespace Ombi.Store.Migrations
b.ToTable("Audit");
});
modelBuilder.Entity("Ombi.Store.Entities.EmbyContent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<DateTime>("AddedAt");
b.Property<string>("EmbyId");
b.Property<string>("ProviderId");
b.Property<string>("Title");
b.Property<int>("Type");
b.HasKey("Id");
b.ToTable("EmbyContent");
});
modelBuilder.Entity("Ombi.Store.Entities.GlobalSettings", b =>
{
b.Property<int>("Id")

@ -0,0 +1,112 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2017 Jamie Rees
// File: PlexContentRepository.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Ombi.Store.Context;
using Ombi.Store.Entities;
namespace Ombi.Store.Repository
{
public class EmbyContentRepository : IEmbyContentRepository
{
public EmbyContentRepository(IOmbiContext db)
{
Db = db;
}
private IOmbiContext Db { get; }
public async Task<IEnumerable<EmbyContent>> GetAll()
{
return await Db.EmbyContent.ToListAsync();
}
public async Task AddRange(IEnumerable<EmbyContent> content)
{
Db.EmbyContent.AddRange(content);
await Db.SaveChangesAsync();
}
public async Task<bool> ContentExists(string providerId)
{
return await Db.EmbyContent.AnyAsync(x => x.ProviderId == providerId);
}
public async Task<EmbyContent> Add(EmbyContent content)
{
await Db.EmbyContent.AddAsync(content);
await Db.SaveChangesAsync();
return content;
}
public async Task<EmbyContent> Get(string providerId)
{
return await Db.EmbyContent.FirstOrDefaultAsync(x => x.ProviderId == providerId);
}
public IQueryable<EmbyContent> Get()
{
return Db.EmbyContent.AsQueryable();
}
public async Task<EmbyContent> GetByEmbyId(string embyId)
{
return await Db.EmbyContent./*Include(x => x.Seasons).*/FirstOrDefaultAsync(x => x.EmbyId == embyId);
}
public async Task Update(EmbyContent existingContent)
{
Db.EmbyContent.Update(existingContent);
await Db.SaveChangesAsync();
}
//public IQueryable<PlexEpisode> GetAllEpisodes()
//{
// return Db.PlexEpisode.AsQueryable();
//}
//public async Task<PlexEpisode> Add(PlexEpisode content)
//{
// await Db.PlexEpisode.AddAsync(content);
// await Db.SaveChangesAsync();
// return content;
//}
//public async Task<PlexEpisode> GetEpisodeByKey(int key)
//{
// return await Db.PlexEpisode.FirstOrDefaultAsync(x => x.Key == key);
//}
//public async Task AddRange(IEnumerable<PlexEpisode> content)
//{
// Db.PlexEpisode.AddRange(content);
// await Db.SaveChangesAsync();
//}
}
}

@ -0,0 +1,19 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Ombi.Store.Entities;
namespace Ombi.Store.Repository
{
public interface IEmbyContentRepository
{
Task<EmbyContent> Add(EmbyContent content);
Task AddRange(IEnumerable<EmbyContent> content);
Task<bool> ContentExists(string providerId);
IQueryable<EmbyContent> Get();
Task<EmbyContent> Get(string providerId);
Task<IEnumerable<EmbyContent>> GetAll();
Task<EmbyContent> GetByEmbyId(string embyId);
Task Update(EmbyContent existingContent);
}
}

@ -5,6 +5,7 @@ using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Ombi.Api.Emby;
using Ombi.Attributes;
using Ombi.Core.Models.UI;
using Ombi.Core.Settings;
@ -33,16 +34,19 @@ namespace Ombi.Controllers
/// <param name="resolver">The resolver.</param>
/// <param name="mapper">The mapper.</param>
/// <param name="templateRepo">The templateRepo.</param>
public SettingsController(ISettingsResolver resolver, IMapper mapper, INotificationTemplatesRepository templateRepo)
public SettingsController(ISettingsResolver resolver, IMapper mapper, INotificationTemplatesRepository templateRepo,
IEmbyApi embyApi)
{
SettingsResolver = resolver;
Mapper = mapper;
TemplateRepository = templateRepo;
_embyApi = embyApi;
}
private ISettingsResolver SettingsResolver { get; }
private IMapper Mapper { get; }
private INotificationTemplatesRepository TemplateRepository { get; }
private readonly IEmbyApi _embyApi;
/// <summary>
/// Gets the Ombi settings.
@ -114,6 +118,12 @@ namespace Ombi.Controllers
[HttpPost("emby")]
public async Task<bool> EmbySettings([FromBody]EmbySettings emby)
{
foreach (var server in emby.Servers)
{
var users = await _embyApi.GetUsers(server.FullUri, server.ApiKey);
var admin = users.FirstOrDefault(x => x.Policy.IsAdministrator);
server.AdministratorId = admin?.Id;
}
return await Save(emby);
}

Loading…
Cancel
Save