Made a massive improvement on the Smaller more frequent Plex Job. This should pick up content a lot quicker now and also get their metadata a lot quicker.

pull/2225/head
Jamie Rees 6 years ago
parent f913719fdd
commit 5529a049d6

@ -11,15 +11,15 @@ namespace Ombi.Api.Plex.Models
public string summary { get; set; }
public int index { get; set; }
public float rating { get; set; }
public int viewCount { get; set; }
public int lastViewedAt { get; set; }
//public int viewCount { get; set; }
//public int lastViewedAt { get; set; }
public int year { get; set; }
public string thumb { get; set; }
public string art { get; set; }
public string banner { get; set; }
public string theme { get; set; }
public string duration { get; set; }
public string originallyAvailableAt { get; set; }
//public string duration { get; set; }
//public string originallyAvailableAt { get; set; }
public int leafCount { get; set; }
public int viewedLeafCount { get; set; }
public int childCount { get; set; }

@ -119,11 +119,117 @@ namespace Ombi.Schedule.Jobs.Plex
var contentToAdd = new HashSet<PlexServerContent>();
foreach (var content in allContent)
{
if (content.viewGroup.Equals(PlexMediaType.Episode.ToString(), StringComparison.CurrentCultureIgnoreCase))
{
Logger.LogInformation("Found some episodes, this must be a recently added sync");
foreach (var epInfo in content.Metadata)
{
var grandParentKey = epInfo.grandparentRatingKey;
// Lookup the rating key
var showMetadata = await PlexApi.GetMetadata(servers.PlexAuthToken, servers.FullUri, grandParentKey);
var show = showMetadata.MediaContainer.Metadata.FirstOrDefault();
if(show == null)
{
continue;
}
await ProcessTvShow(servers, show, contentToAdd, recentlyAddedSearch);
}
}
if (content.viewGroup.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase))
{
// Process Shows
Logger.LogInformation("Processing TV Shows");
foreach (var show in content.Metadata ?? new Metadata[] { })
{
await ProcessTvShow(servers, show, contentToAdd, recentlyAddedSearch);
}
}
if (content.viewGroup.Equals(PlexMediaType.Movie.ToString(), StringComparison.CurrentCultureIgnoreCase))
{
Logger.LogInformation("Processing Movies");
foreach (var movie in content?.Metadata ?? new Metadata[] { })
{
// Let's check if we have this movie
try
{
var existing = await Repo.GetFirstContentByCustom(x => x.Title == movie.title
&& x.ReleaseYear == movie.year.ToString()
&& x.Type == PlexMediaTypeEntity.Movie);
// The rating key keeps changing
//var existing = await Repo.GetByKey(movie.ratingKey);
if (existing != null)
{
Logger.LogInformation("We already have movie {0}", movie.title);
continue;
}
var hasSameKey = await Repo.GetByKey(movie.ratingKey);
if (hasSameKey != null)
{
await Repo.Delete(hasSameKey);
}
Logger.LogInformation("Adding movie {0}", movie.title);
var metaData = await PlexApi.GetMetadata(servers.PlexAuthToken, servers.FullUri,
movie.ratingKey);
var providerIds = PlexHelper.GetProviderIdFromPlexGuid(metaData.MediaContainer.Metadata
.FirstOrDefault()
.guid);
var item = new PlexServerContent
{
AddedAt = DateTime.Now,
Key = movie.ratingKey,
ReleaseYear = movie.year.ToString(),
Type = PlexMediaTypeEntity.Movie,
Title = movie.title,
Url = PlexHelper.GetPlexMediaUrl(servers.MachineIdentifier, movie.ratingKey),
Seasons = new List<PlexSeasonsContent>(),
Quality = movie.Media?.FirstOrDefault()?.videoResolution ?? string.Empty
};
if (providerIds.Type == ProviderType.ImdbId)
{
item.ImdbId = providerIds.ImdbId;
}
if (providerIds.Type == ProviderType.TheMovieDbId)
{
item.TheMovieDbId = providerIds.TheMovieDb;
}
if (providerIds.Type == ProviderType.TvDbId)
{
item.TvDbId = providerIds.TheTvDb;
}
contentToAdd.Add(item);
}
catch (Exception e)
{
Logger.LogError(LoggingEvents.PlexContentCacher, e, "Exception when adding new Movie {0}",
movie.title);
}
if (contentToAdd.Count > 500)
{
await Repo.AddRange(contentToAdd);
contentToAdd.Clear();
}
}
}
if (contentToAdd.Count > 500)
{
await Repo.AddRange(contentToAdd);
contentToAdd.Clear();
}
}
if (contentToAdd.Any())
{
await Repo.AddRange(contentToAdd);
}
}
private async Task ProcessTvShow(PlexServers servers, Metadata show, HashSet<PlexServerContent> contentToAdd, bool recentlyAdded)
{
var seasonList = await PlexApi.GetSeasons(servers.PlexAuthToken, servers.FullUri,
show.ratingKey);
@ -186,10 +292,12 @@ namespace Ombi.Schedule.Jobs.Plex
Repo.DeleteWithoutSave(e);
}
}
await Repo.SaveChangesAsync();
existingContent = null;
}
}
// The ratingKey keeps changing...
//var existingContent = await Repo.GetByKey(show.ratingKey);
if (existingContent != null)
@ -251,10 +359,12 @@ namespace Ombi.Schedule.Jobs.Plex
{
item.ImdbId = providerIds.ImdbId;
}
if (providerIds.Type == ProviderType.TheMovieDbId)
{
item.TheMovieDbId = providerIds.TheMovieDb;
}
if (providerIds.Type == ProviderType.TvDbId)
{
item.TvDbId = providerIds.TheTvDb;
@ -269,20 +379,23 @@ namespace Ombi.Schedule.Jobs.Plex
existingImdb = await Repo.GetAll().AnyAsync(x =>
x.ImdbId == item.ImdbId && x.Type == PlexMediaTypeEntity.Show);
}
if (item.TheMovieDbId.HasValue())
{
existingMovieDbId = await Repo.GetAll().AnyAsync(x =>
x.TheMovieDbId == item.TheMovieDbId && x.Type == PlexMediaTypeEntity.Show);
}
if (item.TvDbId.HasValue())
{
existingTvDbId = await Repo.GetAll().AnyAsync(x =>
x.TvDbId == item.TvDbId && x.Type == PlexMediaTypeEntity.Show);
}
if (existingImdb || existingTvDbId || existingMovieDbId)
{
// We already have it!
continue;
return;
}
item.Seasons.ToList().AddRange(seasonsContent);
@ -295,96 +408,13 @@ namespace Ombi.Schedule.Jobs.Plex
show.title);
}
}
if (contentToAdd.Count > 500)
{
await Repo.AddRange(contentToAdd);
contentToAdd.Clear();
}
}
}
if (content.viewGroup.Equals(PlexMediaType.Movie.ToString(), StringComparison.CurrentCultureIgnoreCase))
{
Logger.LogInformation("Processing Movies");
foreach (var movie in content?.Metadata ?? new Metadata[] { })
{
// Let's check if we have this movie
try
{
var existing = await Repo.GetFirstContentByCustom(x => x.Title == movie.title
&& x.ReleaseYear == movie.year.ToString()
&& x.Type == PlexMediaTypeEntity.Movie);
// The rating key keeps changing
//var existing = await Repo.GetByKey(movie.ratingKey);
if (existing != null)
{
Logger.LogInformation("We already have movie {0}", movie.title);
continue;
}
var hasSameKey = await Repo.GetByKey(movie.ratingKey);
if (hasSameKey != null)
{
await Repo.Delete(hasSameKey);
}
Logger.LogInformation("Adding movie {0}", movie.title);
var metaData = await PlexApi.GetMetadata(servers.PlexAuthToken, servers.FullUri,
movie.ratingKey);
var providerIds = PlexHelper.GetProviderIdFromPlexGuid(metaData.MediaContainer.Metadata
.FirstOrDefault()
.guid);
var item = new PlexServerContent
{
AddedAt = DateTime.Now,
Key = movie.ratingKey,
ReleaseYear = movie.year.ToString(),
Type = PlexMediaTypeEntity.Movie,
Title = movie.title,
Url = PlexHelper.GetPlexMediaUrl(servers.MachineIdentifier, movie.ratingKey),
Seasons = new List<PlexSeasonsContent>(),
Quality = movie.Media?.FirstOrDefault()?.videoResolution ?? string.Empty
};
if (providerIds.Type == ProviderType.ImdbId)
{
item.ImdbId = providerIds.ImdbId;
}
if (providerIds.Type == ProviderType.TheMovieDbId)
{
item.TheMovieDbId = providerIds.TheMovieDb;
}
if (providerIds.Type == ProviderType.TvDbId)
{
item.TvDbId = providerIds.TheTvDb;
}
contentToAdd.Add(item);
}
catch (Exception e)
{
Logger.LogError(LoggingEvents.PlexContentCacher, e, "Exception when adding new Movie {0}",
movie.title);
}
if (contentToAdd.Count > 500)
if (contentToAdd.Count > 500 || recentlyAdded)
{
await Repo.AddRange(contentToAdd);
contentToAdd.Clear();
}
}
}
if (contentToAdd.Count > 500)
{
await Repo.AddRange(contentToAdd);
contentToAdd.Clear();
}
}
if (contentToAdd.Any())
{
await Repo.AddRange(contentToAdd);
}
}
/// <summary>
/// Gets all the library sections.

@ -31,6 +31,7 @@ namespace Ombi.Schedule.Jobs
public enum PlexMediaType
{
Movie = 0,
Show = 1
Show = 1,
Episode = 2,
}
}

@ -1,132 +1,134 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features.Authentication;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using Ombi.Api.Emby;
using Ombi.Api.Plex;
using Ombi.Config;
using Ombi.Controllers;
using Ombi.Core.Authentication;
using Ombi.Core.Settings;
using Ombi.Core.Settings.Models.External;
using Ombi.Models;
using Ombi.Notifications;
using Ombi.Schedule.Jobs.Ombi;
using Ombi.Settings.Settings.Models;
using Ombi.Settings.Settings.Models.Notifications;
using Ombi.Store.Context;
using Ombi.Store.Entities;
//using System.Collections.Generic;
//using System.Linq;
//using System.Threading;
//using System.Threading.Tasks;
//using AutoMapper;
//using Microsoft.AspNetCore.Http;
//using Microsoft.AspNetCore.Http.Features.Authentication;
//using Microsoft.AspNetCore.Identity;
//using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
//using Microsoft.EntityFrameworkCore;
//using Microsoft.EntityFrameworkCore.Infrastructure;
//using Microsoft.Extensions.DependencyInjection;
//using Microsoft.Extensions.Options;
//using Moq;
//using NUnit.Framework;
//using Ombi.Api.Emby;
//using Ombi.Api.Plex;
//using Ombi.Config;
//using Ombi.Controllers;
//using Ombi.Core.Authentication;
//using Ombi.Core.Settings;
//using Ombi.Core.Settings.Models.External;
//using Ombi.Models;
//using Ombi.Notifications;
//using Ombi.Schedule.Jobs.Ombi;
//using Ombi.Settings.Settings.Models;
//using Ombi.Settings.Settings.Models.Notifications;
//using Ombi.Store.Context;
//using Ombi.Store.Entities;
namespace Ombi.Tests
{
[TestFixture]
[Ignore("Need to sort out the DB, looks like it's using the real one...")]
public class IdentityControllerTests
{
[SetUp]
public void Setup()
{
_plexApi = new Mock<IPlexApi>();
_embyApi = new Mock<IEmbyApi>();
_mapper = new Mock<IMapper>();
_emailProvider = new Mock<IEmailProvider>();
_emailSettings = new Mock<ISettingsService<EmailNotificationSettings>>();
_customizationSettings = new Mock<ISettingsService<CustomizationSettings>>();
_welcomeEmail = new Mock<IWelcomeEmail>();
_embySettings = new Mock<ISettingsService<EmbySettings>>();
_plexSettings = new Mock<ISettingsService<PlexSettings>>();
//namespace Ombi.Tests
//{
// [TestFixture]
// [Ignore("Need to sort out the DB, looks like it's using the real one...")]
// public class IdentityControllerTests
// {
// [SetUp]
// public void Setup()
// {
// _plexApi = new Mock<IPlexApi>();
// _embyApi = new Mock<IEmbyApi>();
// _mapper = new Mock<IMapper>();
// _emailProvider = new Mock<IEmailProvider>();
// _emailSettings = new Mock<ISettingsService<EmailNotificationSettings>>();
// _customizationSettings = new Mock<ISettingsService<CustomizationSettings>>();
// _welcomeEmail = new Mock<IWelcomeEmail>();
// _embySettings = new Mock<ISettingsService<EmbySettings>>();
// _plexSettings = new Mock<ISettingsService<PlexSettings>>();
var services = new ServiceCollection();
services.AddEntityFrameworkInMemoryDatabase()
.AddDbContext<OmbiContext>();
services.AddIdentity<OmbiUser, IdentityRole>()
.AddEntityFrameworkStores<OmbiContext>().AddUserManager<OmbiUserManager>();
// var services = new ServiceCollection();
// services.AddEntityFrameworkInMemoryDatabase()
// .AddDbContext<OmbiContext>();
// services.AddIdentity<OmbiUser, IdentityRole>()
// .AddEntityFrameworkStores<OmbiContext>().AddUserManager<OmbiUserManager>();
services.AddTransient(x => _plexApi.Object);
services.AddTransient(x => _embyApi.Object);
services.AddTransient(x => _customizationSettings.Object);
services.AddTransient(x => _welcomeEmail.Object);
services.AddTransient(x => _emailSettings.Object);
services.AddTransient(x => _emailProvider.Object);
services.AddTransient(x => _mapper.Object);
services.AddTransient(x => _embySettings.Object);
services.AddTransient(x => _plexSettings.Object);
// services.AddTransient(x => _plexApi.Object);
// services.AddTransient(x => _embyApi.Object);
// services.AddTransient(x => _customizationSettings.Object);
// services.AddTransient(x => _welcomeEmail.Object);
// services.AddTransient(x => _emailSettings.Object);
// services.AddTransient(x => _emailProvider.Object);
// services.AddTransient(x => _mapper.Object);
// services.AddTransient(x => _embySettings.Object);
// services.AddTransient(x => _plexSettings.Object);
services.Configure<IdentityOptions>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequiredLength = 1;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.User.AllowedUserNameCharacters = string.Empty;
});
// services.Configure<IdentityOptions>(options =>
// {
// options.Password.RequireDigit = false;
// options.Password.RequiredLength = 1;
// options.Password.RequireLowercase = false;
// options.Password.RequireNonAlphanumeric = false;
// options.Password.RequireUppercase = false;
// options.User.AllowedUserNameCharacters = string.Empty;
// });
// Taken from https://github.com/aspnet/MusicStore/blob/dev/test/MusicStore.Test/ManageControllerTest.cs (and modified)
var context = new DefaultHttpContext();
context.Features.Set<IHttpAuthenticationFeature>(new HttpAuthenticationFeature());
services.AddSingleton<IHttpContextAccessor>(h => new HttpContextAccessor { HttpContext = context });
_serviceProvider = services.BuildServiceProvider();
_userManager = _serviceProvider.GetRequiredService<OmbiUserManager>();
// // Taken from https://github.com/aspnet/MusicStore/blob/dev/test/MusicStore.Test/ManageControllerTest.cs (and modified)
// var context = new DefaultHttpContext();
// context.Features.Set<IHttpAuthenticationFeature>(new HttpAuthenticationFeature());
// services.AddSingleton<IHttpContextAccessor>(h => new HttpContextAccessor { HttpContext = context });
// _serviceProvider = services.BuildServiceProvider();
// _userManager = _serviceProvider.GetRequiredService<OmbiUserManager>();
Controller = new IdentityController(_userManager, _mapper.Object, _serviceProvider.GetService<RoleManager<IdentityRole>>(), _emailProvider.Object,
_emailSettings.Object, _customizationSettings.Object,_welcomeEmail.Object, null, null, null, null, null, null, null, null);
}
// Controller = new IdentityController(_userManager, _mapper.Object,
// _serviceProvider.GetService<RoleManager<IdentityRole>>(), _emailProvider.Object,
// _emailSettings.Object, _customizationSettings.Object, _welcomeEmail.Object, null, null, null, null,
// null, null, null, null, null);
// }
private OmbiUserManager _userManager;
private Mock<IEmailProvider> _emailProvider;
private Mock<ISettingsService<EmailNotificationSettings>> _emailSettings;
private Mock<ISettingsService<CustomizationSettings>> _customizationSettings;
private Mock<ISettingsService<EmbySettings>> _embySettings;
private Mock<ISettingsService<PlexSettings>> _plexSettings;
private Mock<IWelcomeEmail> _welcomeEmail;
private Mock<IMapper> _mapper;
private Mock<IPlexApi> _plexApi;
private Mock<IEmbyApi> _embyApi;
private ServiceProvider _serviceProvider;
// private OmbiUserManager _userManager;
// private Mock<IEmailProvider> _emailProvider;
// private Mock<ISettingsService<EmailNotificationSettings>> _emailSettings;
// private Mock<ISettingsService<CustomizationSettings>> _customizationSettings;
// private Mock<ISettingsService<EmbySettings>> _embySettings;
// private Mock<ISettingsService<PlexSettings>> _plexSettings;
// private Mock<IWelcomeEmail> _welcomeEmail;
// private Mock<IMapper> _mapper;
// private Mock<IPlexApi> _plexApi;
// private Mock<IEmbyApi> _embyApi;
// private ServiceProvider _serviceProvider;
private IdentityController Controller { get; set; }
// private IdentityController Controller { get; set; }
[Test]
public async Task CreateWizardUser_Should_CreateUser_WhenThereAreNoOtherUsers()
{
var model = new CreateUserWizardModel()
{
Password = "a",
Username = "b"
};
// [Test]
// public async Task CreateWizardUser_Should_CreateUser_WhenThereAreNoOtherUsers()
// {
// var model = new CreateUserWizardModel()
// {
// Password = "a",
// Username = "b"
// };
var result = await Controller.CreateWizardUser(model);
// var result = await Controller.CreateWizardUser(model);
Assert.That(result, Is.True);
}
// Assert.That(result, Is.True);
// }
[Test]
public async Task CreateWizardUser_ShouldNot_CreateUser_WhenThereAreOtherUsers()
{
var um = _serviceProvider.GetService<OmbiUserManager>();
var r = await um.CreateAsync(new OmbiUser { UserName = "aaaa",UserType = UserType.LocalUser}, "bbb");
var model = new CreateUserWizardModel
{
Password = "a",
Username = "b"
};
// [Test]
// public async Task CreateWizardUser_ShouldNot_CreateUser_WhenThereAreOtherUsers()
// {
// var um = _serviceProvider.GetService<OmbiUserManager>();
// var r = await um.CreateAsync(new OmbiUser { UserName = "aaaa",UserType = UserType.LocalUser}, "bbb");
// var model = new CreateUserWizardModel
// {
// Password = "a",
// Username = "b"
// };
var result = await Controller.CreateWizardUser(model);
// var result = await Controller.CreateWizardUser(model);
Assert.That(result, Is.False);
}
}
}
// Assert.That(result, Is.False);
// }
// }
//}

Loading…
Cancel
Save