Small changes

This commit is contained in:
Jamie.Rees 2017-04-21 16:31:11 +01:00
commit 3935dcb811
10 changed files with 157 additions and 103 deletions

View file

@ -0,0 +1,16 @@
using System;
using NUnit.Framework;
namespace Ombi.Core.Tests
{
[TestFixture]
public class MovieEngineTests
{
[Test]
public void Test()
{
Assert.IsTrue(true);
}
}
}

View file

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputType>Library</OutputType>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
<ApplicationIcon />
<OutputType>Exe</OutputType>
<OutputTypeEx>library</OutputTypeEx>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.TestPlatform.TestHost" Version="15.0.0" />
<PackageReference Include="NUnit" Version="3.6.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.7.0" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Ombi.Core.Engine.Interfaces;
using Ombi.Core.IdentityResolver;
using Ombi.Core.Models.Requests;
using Ombi.Core.Requests.Models;
using Ombi.Helpers;
using Ombi.Store.Entities;
namespace Ombi.Core.Engine
{
public abstract class BaseMediaEngine : BaseEngine
{
protected BaseMediaEngine(IUserIdentityManager identity, IRequestService service) : base(identity)
{
RequestService = service;
}
protected IRequestService RequestService { get; }
private long _dbMovieCacheTime = 0;
private Dictionary<int, RequestModel> _dbMovies;
protected async Task<Dictionary<int, RequestModel>> GetRequests(RequestType type)
{
long now = DateTime.Now.Ticks;
if (_dbMovies == null || (now - _dbMovieCacheTime) > 10000)
{
var allResults = await RequestService.GetAllAsync();
allResults = allResults.Where(x => x.Type == type);
var distinctResults = allResults.DistinctBy(x => x.ProviderId);
_dbMovies = distinctResults.ToDictionary(x => x.ProviderId);
_dbMovieCacheTime = now;
}
return _dbMovies;
}
}
}

View file

@ -0,0 +1,22 @@
using System.Threading.Tasks;
using Ombi.Core.IdentityResolver;
using Ombi.Core.Models;
namespace Ombi.Core.Engine.Interfaces
{
public abstract class BaseEngine
{
protected BaseEngine(IUserIdentityManager identity)
{
UserIdentity = identity;
}
protected IUserIdentityManager UserIdentity { get; }
protected async Task<UserDto> GetUser(string username)
{
}
}
}

View file

@ -1,61 +1,45 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using AutoMapper; using AutoMapper;
using Ombi.Api.TheMovieDb; using Ombi.Api.TheMovieDb;
using Ombi.Api.TheMovieDb.Models; using Ombi.Api.TheMovieDb.Models;
using Ombi.Core.IdentityResolver;
using Ombi.Core.Models.Requests; using Ombi.Core.Models.Requests;
using Ombi.Core.Models.Search; using Ombi.Core.Models.Search;
using Ombi.Core.Requests.Models; using Ombi.Core.Requests.Models;
using Ombi.Helpers; using Ombi.Core.Settings;
using Ombi.Core.Settings.Models.External;
using Ombi.Store.Entities; using Ombi.Store.Entities;
using Ombi.TheMovieDbApi;
using Ombi.TheMovieDbApi.Models;
namespace Ombi.Core.Engine namespace Ombi.Core.Engine
{ {
public class MovieEngine : IMovieEngine public class MovieEngine : BaseMediaEngine, IMovieEngine
{ {
public MovieEngine(IRequestService service, IMovieDbApi movApi, IMapper mapper) public MovieEngine(IUserIdentityManager identity, IRequestService service, IMovieDbApi movApi, IMapper mapper, ISettingsService<PlexSettings> plexSettings, ISettingsService<EmbySettings> embySettings)
: base(identity, service)
{ {
RequestService = service;
MovieApi = movApi; MovieApi = movApi;
Mapper = mapper; Mapper = mapper;
PlexSettings = plexSettings;
EmbySettings = embySettings;
} }
private IRequestService RequestService { get; }
private IMovieDbApi MovieApi { get; } private IMovieDbApi MovieApi { get; }
private IMapper Mapper { get; } private IMapper Mapper { get; }
private ISettingsService<PlexSettings> PlexSettings { get; }
private ISettingsService<EmbySettings> EmbySettings { get; }
public async Task<IEnumerable<SearchMovieViewModel>> LookupImdbInformation(IEnumerable<SearchMovieViewModel> movies) public async Task<IEnumerable<SearchMovieViewModel>> LookupImdbInformation(IEnumerable<SearchMovieViewModel> movies)
{ {
var retVal = new List<SearchMovieViewModel>(); var retVal = new List<SearchMovieViewModel>();
Dictionary<int, RequestModel> dbMovies = await RequestedMovies(); Dictionary<int, RequestModel> dbMovies = await GetRequests(RequestType.Movie);
foreach (var m in movies) foreach (var m in movies)
{ {
var movieInfo = await MovieApi.GetMovieInformationWithVideo(m.Id); var movieInfo = await MovieApi.GetMovieInformationWithVideo(m.Id);
var viewMovie = Mapper.Map<SearchMovieViewModel>(movieInfo); var viewMovie = Mapper.Map<SearchMovieViewModel>(movieInfo);
retVal.Add(viewMovie);
// TODO needs to be careful about this, it's adding extra time to search...
// https://www.themoviedb.org/talk/5807f4cdc3a36812160041f2
//var videoId = movieInfo?.video ?? false
// ? movieInfo?.videos?.results?.FirstOrDefault()?.key
// : string.Empty;
//viewMovie.Trailer = string.IsNullOrEmpty(videoId) retVal.Add(await ProcessSingleMovie(viewMovie, dbMovies));
// ? string.Empty
// : $"https://www.youtube.com/watch?v={videoId}";
if (dbMovies.ContainsKey(movieInfo.Id) /*&& canSee*/) // compare to the requests db
{
var dbm = dbMovies[movieInfo.Id];
viewMovie.Requested = true;
viewMovie.Approved = dbm.Approved;
viewMovie.Available = dbm.Available;
}
} }
return retVal; return retVal;
} }
@ -111,24 +95,25 @@ namespace Ombi.Core.Engine
private async Task<List<SearchMovieViewModel>> TransformMovieResultsToResponse(IEnumerable<MovieSearchResult> movies) private async Task<List<SearchMovieViewModel>> TransformMovieResultsToResponse(IEnumerable<MovieSearchResult> movies)
{ {
await Task.Yield();
var viewMovies = new List<SearchMovieViewModel>(); var viewMovies = new List<SearchMovieViewModel>();
//var counter = 0; Dictionary<int, RequestModel> dbMovies = await GetRequests(RequestType.Movie);
Dictionary<int, RequestModel> dbMovies = await RequestedMovies();
foreach (var movie in movies) foreach (var movie in movies)
{ {
var viewMovie = Mapper.Map<SearchMovieViewModel>(movie);
viewMovies.Add(viewMovie);
viewMovies.Add(await ProcessSingleMovie(movie, dbMovies));
}
return viewMovies;
}
// var canSee = CanUserSeeThisRequest(viewMovie.Id, Security.HasPermissions(User, Permissions.UsersCanViewOnlyOwnRequests), dbMovies); private async Task<SearchMovieViewModel> ProcessSingleMovie(SearchMovieViewModel viewMovie,
Dictionary<int, RequestModel> existingRequests)
{
// var plexSettings = await PlexService.GetSettingsAsync(); var plexSettings = await PlexSettings.GetSettingsAsync();
// var embySettings = await EmbySettings.GetSettingsAsync(); var embySettings = await EmbySettings.GetSettingsAsync();
// if (plexSettings.Enable) if (plexSettings.Enable)
// { {
// var content = PlexContentRepository.GetAll(); // var content = PlexContentRepository.GetAll();
// var plexMovies = PlexChecker.GetPlexMovies(content); // var plexMovies = PlexChecker.GetPlexMovies(content);
@ -140,9 +125,9 @@ namespace Ombi.Core.Engine
// viewMovie.Available = true; // viewMovie.Available = true;
// viewMovie.PlexUrl = plexMovie.Url; // viewMovie.PlexUrl = plexMovie.Url;
// } // }
// } }
// if (embySettings.Enable) if (embySettings.Enable)
// { {
// var embyContent = EmbyContentRepository.GetAll(); // var embyContent = EmbyContentRepository.GetAll();
// var embyMovies = EmbyChecker.GetEmbyMovies(embyContent); // var embyMovies = EmbyChecker.GetEmbyMovies(embyContent);
@ -152,45 +137,25 @@ namespace Ombi.Core.Engine
// { // {
// viewMovie.Available = true; // viewMovie.Available = true;
// } // }
// }
if (dbMovies.ContainsKey(movie.Id) /*&& canSee*/) // compare to the requests db
{
var dbm = dbMovies[movie.Id];
viewMovie.Requested = true;
viewMovie.Approved = dbm.Approved;
viewMovie.Available = dbm.Available;
}
// else if (canSee)
// {
// bool exists = IsMovieInCache(movie, viewMovie.ImdbId);
// viewMovie.Approved = exists;
// viewMovie.Requested = exists;
// }
// viewMovies.Add(viewMovie);
//}
} }
return viewMovies;
if (existingRequests.ContainsKey(viewMovie.Id)) // Do we already have a request for this?
{
var requestedMovie = existingRequests[viewMovie.Id];
viewMovie.Requested = true;
viewMovie.Approved = requestedMovie.Approved;
viewMovie.Available = requestedMovie.Available;
}
return viewMovie;
} }
private async Task<SearchMovieViewModel> ProcessSingleMovie(MovieSearchResult movie, Dictionary<int, RequestModel> existingRequests)
private long _dbMovieCacheTime = 0;
private Dictionary<int, RequestModel> _dbMovies;
private async Task<Dictionary<int, RequestModel>> RequestedMovies()
{ {
long now = DateTime.Now.Ticks; var viewMovie = Mapper.Map<SearchMovieViewModel>(movie);
if (_dbMovies == null || (now - _dbMovieCacheTime) > 10000) return await ProcessSingleMovie(viewMovie, existingRequests);
{
var allResults = await RequestService.GetAllAsync();
allResults = allResults.Where(x => x.Type == RequestType.Movie);
var distinctResults = allResults.DistinctBy(x => x.ProviderId);
_dbMovies = distinctResults.ToDictionary(x => x.ProviderId);
_dbMovieCacheTime = now;
}
return _dbMovies;
} }
} }
} }

View file

@ -32,11 +32,6 @@ namespace Ombi.Core.Engine
RequestAdded = false, RequestAdded = false,
Message = "There was an issue adding this movie!" Message = "There was an issue adding this movie!"
}; };
//Response.AsJson(new JsonResponseModel
//{
// Result = false,
// Message = "There was an issue adding this movie!"
//});
} }
var fullMovieName = var fullMovieName =
$"{movieInfo.Title}{(!string.IsNullOrEmpty(movieInfo.ReleaseDate) ? $" ({DateTime.Parse(movieInfo.ReleaseDate).Year})" : string.Empty)}"; $"{movieInfo.Title}{(!string.IsNullOrEmpty(movieInfo.ReleaseDate) ? $" ({DateTime.Parse(movieInfo.ReleaseDate).Year})" : string.Empty)}";
@ -44,26 +39,11 @@ namespace Ombi.Core.Engine
var existingRequest = await RequestService.CheckRequestAsync(model.Id); var existingRequest = await RequestService.CheckRequestAsync(model.Id);
if (existingRequest != null) if (existingRequest != null)
{ {
// check if the current user is already marked as a requester for this movie, if not, add them
//if (!existingRequest.UserHasRequested(Username))
//{
// existingRequest.RequestedUsers.Add(Username);
// await RequestService.UpdateRequestAsync(existingRequest);
//}
return new RequestEngineResult return new RequestEngineResult
{ {
RequestAdded = true, RequestAdded = false,
Message = "This has already been requested"
}; };
//Response.AsJson(new JsonResponseModel
//{
// Result = true,
// Message =
// Security.HasPermissions(User, Permissions.UsersCanViewOnlyOwnRequests)
// ? $"{fullMovieName} {Ombi.UI.Resources.UI.Search_SuccessfullyAdded}"
// : $"{fullMovieName} {Resources.UI.Search_AlreadyRequested}"
//});
} }

View file

@ -23,7 +23,6 @@ namespace Ombi.Mapping
} }
var config = new AutoMapper.MapperConfiguration(cfg => var config = new AutoMapper.MapperConfiguration(cfg =>
{ {
//cfg.AddProfile(new OmbiProfile());
cfg.AddProfiles(assemblies); cfg.AddProfiles(assemblies);
}); });

View file

@ -12,6 +12,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Ombi.Api\Ombi.Api.csproj" /> <ProjectReference Include="..\Ombi.Api\Ombi.Api.csproj" />
<ProjectReference Include="..\Ombi.Helpers\Ombi.Helpers.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -3,6 +3,7 @@ using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using AutoMapper; using AutoMapper;
using Ombi.Api.TheMovieDb.Models; using Ombi.Api.TheMovieDb.Models;
using Ombi.Helpers;
using Ombi.TheMovieDbApi.Models; using Ombi.TheMovieDbApi.Models;
namespace Ombi.Api.TheMovieDb namespace Ombi.Api.TheMovieDb
@ -16,7 +17,7 @@ namespace Ombi.Api.TheMovieDb
} }
private IMapper Mapper { get; } private IMapper Mapper { get; }
private const string ApiToken = "b8eabaf5608b88d0298aa189dd90bf00"; private readonly string ApiToken = StringCipher.DecryptString("bqCrDAQABAC/mJT6JXNdlQ1boOjsHeQgyk7gcNv7tUFtwxoVEnYvqS+UdgfgoyXnBz2F6LJnKX8xGtXbzLsf6pbxDkxna6zvunivxAcAHewo2zTPjoUB5igeMB8d93fx0WO9IhGtq8oGXv++xfaXfTY3aN5NV7JmF6ziAAAAAD1e5VjRPSLOYTyJ3Hbw9bDsE/4FGxYIrvxVkqDMl1vAosOeTi+0kKPFloF6k2ptTw==", "Ombiv3SettingsEncryptionPassword");
private static readonly string BaseUri ="http://api.themoviedb.org/3/"; private static readonly string BaseUri ="http://api.themoviedb.org/3/";
private Ombi.Api.Api Api { get; } private Ombi.Api.Api Api { get; }

View file

@ -40,6 +40,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ombi.Api.Emby", "Ombi.Api.E
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ombi.Api.Sonarr", "Ombi.Api.Sonarr\Ombi.Api.Sonarr.csproj", "{CFB5E008-D0D0-43C0-AA06-89E49D17F384}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ombi.Api.Sonarr", "Ombi.Api.Sonarr\Ombi.Api.Sonarr.csproj", "{CFB5E008-D0D0-43C0-AA06-89E49D17F384}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{6F42AB98-9196-44C4-B888-D5E409F415A1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ombi.Core.Tests", "Ombi.Core.Tests\Ombi.Core.Tests.csproj", "{2836861C-1185-4E56-A0C2-F4EB541C74AE}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -94,6 +98,10 @@ Global
{CFB5E008-D0D0-43C0-AA06-89E49D17F384}.Debug|Any CPU.Build.0 = Debug|Any CPU {CFB5E008-D0D0-43C0-AA06-89E49D17F384}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CFB5E008-D0D0-43C0-AA06-89E49D17F384}.Release|Any CPU.ActiveCfg = Release|Any CPU {CFB5E008-D0D0-43C0-AA06-89E49D17F384}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CFB5E008-D0D0-43C0-AA06-89E49D17F384}.Release|Any CPU.Build.0 = Release|Any CPU {CFB5E008-D0D0-43C0-AA06-89E49D17F384}.Release|Any CPU.Build.0 = Release|Any CPU
{2836861C-1185-4E56-A0C2-F4EB541C74AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2836861C-1185-4E56-A0C2-F4EB541C74AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2836861C-1185-4E56-A0C2-F4EB541C74AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2836861C-1185-4E56-A0C2-F4EB541C74AE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -106,5 +114,6 @@ Global
{2E1A7B91-F29B-42BC-8F1E-1CF2DCC389BA} = {9293CA11-360A-4C20-A674-B9E794431BF5} {2E1A7B91-F29B-42BC-8F1E-1CF2DCC389BA} = {9293CA11-360A-4C20-A674-B9E794431BF5}
{08FF107D-31E1-470D-AF86-E09B015CEE06} = {9293CA11-360A-4C20-A674-B9E794431BF5} {08FF107D-31E1-470D-AF86-E09B015CEE06} = {9293CA11-360A-4C20-A674-B9E794431BF5}
{CFB5E008-D0D0-43C0-AA06-89E49D17F384} = {9293CA11-360A-4C20-A674-B9E794431BF5} {CFB5E008-D0D0-43C0-AA06-89E49D17F384} = {9293CA11-360A-4C20-A674-B9E794431BF5}
{2836861C-1185-4E56-A0C2-F4EB541C74AE} = {6F42AB98-9196-44C4-B888-D5E409F415A1}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal