Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/tidusjar/Ombi into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar committed Aug 17, 2021
2 parents 45b692c + 485ae09 commit b6b9840
Show file tree
Hide file tree
Showing 39 changed files with 921 additions and 136 deletions.
1 change: 1 addition & 0 deletions src/Ombi.Core/Engine/Interfaces/IMovieEngineV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ public interface IMovieEngineV2
Task<IEnumerable<StreamingData>> GetStreamInformation(int movieDbId, CancellationToken cancellationToken);
Task<IEnumerable<SearchMovieViewModel>> RecentlyRequestedMovies(int currentlyLoaded, int toLoad, CancellationToken cancellationToken);
Task<IEnumerable<SearchMovieViewModel>> SeasonalList(int currentPosition, int amountToLoad, CancellationToken cancellationToken);
Task<IEnumerable<SearchMovieViewModel>> AdvancedSearch(DiscoverModel model, int currentlyLoaded, int toLoad, CancellationToken cancellationToken);
}
}
2 changes: 1 addition & 1 deletion src/Ombi.Core/Engine/MovieRequestEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public async Task<RequestsViewModel<MovieRequests>> GetRequestsByStatus(int coun

// TODO fix this so we execute this on the server
var requests = sortOrder.Equals("asc", StringComparison.InvariantCultureIgnoreCase)
? allRequests.ToList().OrderBy(x => x.RequestedDate).ToList()
? allRequests.ToList().OrderBy(x => prop.GetValue(x)).ToList()
: allRequests.ToList().OrderByDescending(x => prop.GetValue(x)).ToList();
var total = requests.Count();
requests = requests.Skip(position).Take(count).ToList();
Expand Down
17 changes: 16 additions & 1 deletion src/Ombi.Core/Engine/V2/MovieSearchEngineV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,27 @@ public async Task<IEnumerable<SearchMovieViewModel>> PopularMovies(int currently
foreach (var pagesToLoad in pages)
{
var apiResult = await Cache.GetOrAddAsync(nameof(PopularMovies) + pagesToLoad.Page + langCode,
() => MovieApi.PopularMovies(langCode, pagesToLoad.Page, cancellationToken), DateTimeOffset.Now.AddHours(12));
() => MovieApi.PopularMovies(langCode, pagesToLoad.Page, cancellationToken), DateTimeOffset.Now.AddHours(12));
results.AddRange(apiResult.Skip(pagesToLoad.Skip).Take(pagesToLoad.Take));
}
return await TransformMovieResultsToResponse(results);
}

public async Task<IEnumerable<SearchMovieViewModel>> AdvancedSearch(DiscoverModel model, int currentlyLoaded, int toLoad, CancellationToken cancellationToken)
{
var langCode = await DefaultLanguageCode(null);

//var pages = PaginationHelper.GetNextPages(currentlyLoaded, toLoad, _theMovieDbMaxPageItems);

var results = new List<MovieDbSearchResult>();
//foreach (var pagesToLoad in pages)
//{
var apiResult = await MovieApi.AdvancedSearch(model, cancellationToken);
//results.AddRange(apiResult.Skip(pagesToLoad.Skip).Take(pagesToLoad.Take));
//}
return await TransformMovieResultsToResponse(apiResult);
}

/// <summary>
/// Gets top rated movies.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions src/Ombi.Core/Rule/Rules/Search/JellyfinAvailabilityRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public async Task<RuleResult> Execute(SearchViewModel obj)
var useImdb = false;
var useTheMovieDb = false;
var useTvDb = false;
var useId = false;

if (obj.ImdbId.HasValue())
{
Expand All @@ -39,6 +40,14 @@ public async Task<RuleResult> Execute(SearchViewModel obj)
}
if (item == null)
{
if (obj.Id > 0)
{
item = await JellyfinContentRepository.GetByTheMovieDbId(obj.Id.ToString());
if (item != null)
{
useId = true;
}
}
if (obj.TheMovieDbId.HasValue())
{
item = await JellyfinContentRepository.GetByTheMovieDbId(obj.TheMovieDbId);
Expand All @@ -63,6 +72,11 @@ public async Task<RuleResult> Execute(SearchViewModel obj)

if (item != null)
{
if (useId)
{
obj.TheMovieDbId = obj.Id.ToString();
useTheMovieDb = true;
}
obj.Available = true;
var s = await JellyfinSettings.GetSettingsAsync();
if (s.Enable)
Expand Down
17 changes: 13 additions & 4 deletions src/Ombi.Helpers/CacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System.Threading;
using System.Threading.Tasks;
using LazyCache;
using Microsoft.Extensions.Caching.Memory;

namespace Ombi.Helpers
{
public class CacheService : ICacheService
{
protected readonly IAppCache _memoryCache;
public CacheService(IAppCache memoryCache)
protected readonly IMemoryCache _memoryCache;
public CacheService(IMemoryCache memoryCache)
{
_memoryCache = memoryCache;
}
Expand All @@ -20,7 +21,11 @@ public virtual async Task<T> GetOrAddAsync<T>(string cacheKey, Func<Task<T>> fac
absoluteExpiration = DateTimeOffset.Now.AddHours(1);
}

return await _memoryCache.GetOrAddAsync<T>(cacheKey, () => factory(), absoluteExpiration);
return await _memoryCache.GetOrCreateAsync<T>(cacheKey, entry =>
{
entry.AbsoluteExpiration = absoluteExpiration;
return factory();
});
}

public void Remove(string key)
Expand All @@ -31,7 +36,11 @@ public void Remove(string key)
public T GetOrAdd<T>(string cacheKey, Func<T> factory, DateTimeOffset absoluteExpiration)
{
// locks get and set internally
return _memoryCache.GetOrAdd<T>(cacheKey, () => factory(), absoluteExpiration);
return _memoryCache.GetOrCreate<T>(cacheKey, entry =>
{
entry.AbsoluteExpiration = absoluteExpiration;
return factory();
});
}

private static class TypeLock<T>
Expand Down
15 changes: 10 additions & 5 deletions src/Ombi.Helpers/MediaCacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using LazyCache;
using Microsoft.Extensions.Caching.Memory;

namespace Ombi.Helpers
{
Expand All @@ -14,7 +15,7 @@ public class MediaCacheService : CacheService, IMediaCacheService
{
private const string CacheKey = "MediaCacheServiceKeys";

public MediaCacheService(IAppCache memoryCache) : base(memoryCache)
public MediaCacheService(IMemoryCache memoryCache) : base(memoryCache)
{
}

Expand All @@ -33,24 +34,28 @@ public async override Task<T> GetOrAddAsync<T>(string cacheKey, System.Func<Task
// Not in the cache, so add this Key into our MediaServiceCache
await UpdateLocalCache(cacheKey);

return await _memoryCache.GetOrAddAsync<T>(cacheKey, () => factory(), absoluteExpiration);
return await _memoryCache.GetOrCreateAsync<T>(cacheKey, entry =>
{
entry.AbsoluteExpiration = absoluteExpiration;
return factory();
});
}

private async Task UpdateLocalCache(string cacheKey)
{
var mediaServiceCache = await _memoryCache.GetAsync<List<string>>(CacheKey);
var mediaServiceCache = _memoryCache.Get<List<string>>(CacheKey);
if (mediaServiceCache == null)
{
mediaServiceCache = new List<string>();
}
mediaServiceCache.Add(cacheKey);
_memoryCache.Remove(CacheKey);
_memoryCache.Add(CacheKey, mediaServiceCache);
_memoryCache.Set(CacheKey, mediaServiceCache);
}

public async Task Purge()
{
var keys = await _memoryCache.GetAsync<List<string>>(CacheKey);
var keys = _memoryCache.Get<List<string>>(CacheKey);
if (keys == null)
{
return;
Expand Down
10 changes: 6 additions & 4 deletions src/Ombi.TheMovieDbApi/IMovieDbApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface IMovieDbApi
Task<List<MovieDbSearchResult>> UpcomingTv(string languageCode, int? page = null);
Task<List<MovieDbSearchResult>> SimilarMovies(int movieId, string langCode);
Task<FindResult> Find(string externalId, ExternalSource source);
Task<TvExternals> GetTvExternals(int theMovieDbId);
Task<TvExternals> GetTvExternals(int theMovieDbId);
Task<SeasonDetails> GetSeasonEpisodes(int theMovieDbId, int seasonNumber, CancellationToken token, string langCode = "en");
Task<TvInfo> GetTVInfo(string themoviedbid, string langCode = "en");
Task<TheMovieDbContainer<ActorResult>> SearchByActor(string searchTerm, string langCode);
Expand All @@ -35,10 +35,12 @@ public interface IMovieDbApi
Task<TheMovieDbContainer<DiscoverMovies>> DiscoverMovies(string langCode, int keywordId);
Task<FullMovieInfo> GetFullMovieInfo(int movieId, CancellationToken cancellationToken, string langCode);
Task<Collections> GetCollection(string langCode, int collectionId, CancellationToken cancellationToken);
Task<List<Keyword>> SearchKeyword(string searchTerm);
Task<Keyword> GetKeyword(int keywordId);
Task<List<TheMovidDbKeyValue>> SearchKeyword(string searchTerm);
Task<TheMovidDbKeyValue> GetKeyword(int keywordId);
Task<WatchProviders> GetMovieWatchProviders(int theMoviedbId, CancellationToken token);
Task<WatchProviders> GetTvWatchProviders(int theMoviedbId, CancellationToken token);
Task<List<Genre>> GetGenres(string media);
Task<List<Genre>> GetGenres(string media, CancellationToken cancellationToken);
Task<List<WatchProvidersResults>> SearchWatchProviders(string media, string searchTerm, CancellationToken cancellationToken);
Task<List<MovieDbSearchResult>> AdvancedSearch(DiscoverModel model, CancellationToken cancellationToken);
}
}
18 changes: 18 additions & 0 deletions src/Ombi.TheMovieDbApi/Models/DiscoverModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ombi.Api.TheMovieDb.Models
{
public class DiscoverModel
{
public string Type { get; set; }
public int? ReleaseYear { get; set; }
public List<int> GenreIds { get; set; } = new List<int>();
public List<int> KeywordIds { get; set; } = new List<int>();
public List<int> WatchProviders { get; set; } = new List<int>();
public List<int> Companies { get; set; } = new List<int>();
}
}
21 changes: 21 additions & 0 deletions src/Ombi.TheMovieDbApi/Models/DiscoverTv.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Ombi.Api.TheMovieDb.Models {

public class DiscoverTv
{
public int vote_count { get; set; }
public int id { get; set; }
public bool video { get; set; }
public float vote_average { get; set; }
public string title { get; set; }
public float popularity { get; set; }
public string poster_path { get; set; }
public string original_language { get; set; }
public string original_title { get; set; }
public int[] genre_ids { get; set; }
public string backdrop_path { get; set; }
public bool adult { get; set; }
public string overview { get; set; }
public string release_date { get; set; }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Ombi.Api.TheMovieDb.Models
{
public sealed class Keyword
public sealed class TheMovidDbKeyValue
{
[DataMember(Name = "id")]
public int Id { get; set; }
Expand Down
16 changes: 16 additions & 0 deletions src/Ombi.TheMovieDbApi/Models/WatchProvidersResults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ombi.Api.TheMovieDb.Models
{
public class WatchProvidersResults
{
public int provider_id { get; set; }
public string logo_path { get; set; }
public string provider_name { get; set; }
public string origin_country { get; set; }
}
}
53 changes: 46 additions & 7 deletions src/Ombi.TheMovieDbApi/TheMovieDbApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,34 @@ public async Task<TheMovieDbContainer<DiscoverMovies>> DiscoverMovies(string lan
return await Api.Request<TheMovieDbContainer<DiscoverMovies>>(request);
}



public async Task<List<MovieDbSearchResult>> AdvancedSearch(DiscoverModel model, CancellationToken cancellationToken)
{
var request = new Request($"discover/{model.Type}", BaseUri, HttpMethod.Get);
request.FullUri = request.FullUri.AddQueryParameter("api_key", ApiToken);
if(model.ReleaseYear.HasValue && model.ReleaseYear.Value > 1900)
{
request.FullUri = request.FullUri.AddQueryParameter("year", model.ReleaseYear.Value.ToString());
}
if (model.KeywordIds.Any())
{
request.FullUri = request.FullUri.AddQueryParameter("with_keyword", string.Join(',', model.KeywordIds));
}
if (model.GenreIds.Any())
{
request.FullUri = request.FullUri.AddQueryParameter("with_genres", string.Join(',', model.GenreIds));
}
if (model.WatchProviders.Any())
{
request.FullUri = request.FullUri.AddQueryParameter("with_watch_providers", string.Join(',', model.WatchProviders));
}
//request.FullUri = request.FullUri.AddQueryParameter("sort_by", "popularity.desc");

var result = await Api.Request<TheMovieDbContainer<SearchResult>>(request, cancellationToken);
return Mapper.Map<List<MovieDbSearchResult>>(result.results);
}

public async Task<Collections> GetCollection(string langCode, int collectionId, CancellationToken cancellationToken)
{
// https://developers.themoviedb.org/3/discover/movie-discover
Expand Down Expand Up @@ -357,34 +385,45 @@ public async Task<List<MovieDbSearchResult>> GetMoviesViaKeywords(string keyword
return Mapper.Map<List<MovieDbSearchResult>>(result.results);
}

public async Task<List<Keyword>> SearchKeyword(string searchTerm)
public async Task<List<TheMovidDbKeyValue>> SearchKeyword(string searchTerm)
{
var request = new Request("search/keyword", BaseUri, HttpMethod.Get);
request.AddQueryString("api_key", ApiToken);
request.AddQueryString("query", searchTerm);
AddRetry(request);

var result = await Api.Request<TheMovieDbContainer<Keyword>>(request);
return result.results ?? new List<Keyword>();
var result = await Api.Request<TheMovieDbContainer<TheMovidDbKeyValue>>(request);
return result.results ?? new List<TheMovidDbKeyValue>();
}

public async Task<List<WatchProvidersResults>> SearchWatchProviders(string media, string searchTerm, CancellationToken cancellationToken)
{
var request = new Request($"/watch/providers/{media}", BaseUri, HttpMethod.Get);
request.AddQueryString("api_key", ApiToken);
request.AddQueryString("query", searchTerm);
AddRetry(request);

var result = await Api.Request<TheMovieDbContainer<WatchProvidersResults>>(request, cancellationToken);
return result.results ?? new List<WatchProvidersResults>();
}

public async Task<Keyword> GetKeyword(int keywordId)
public async Task<TheMovidDbKeyValue> GetKeyword(int keywordId)
{
var request = new Request($"keyword/{keywordId}", BaseUri, HttpMethod.Get);
request.AddQueryString("api_key", ApiToken);
AddRetry(request);

var keyword = await Api.Request<Keyword>(request);
var keyword = await Api.Request<TheMovidDbKeyValue>(request);
return keyword == null || keyword.Id == 0 ? null : keyword;
}

public async Task<List<Genre>> GetGenres(string media)
public async Task<List<Genre>> GetGenres(string media, CancellationToken cancellationToken)
{
var request = new Request($"genre/{media}/list", BaseUri, HttpMethod.Get);
request.AddQueryString("api_key", ApiToken);
AddRetry(request);

var result = await Api.Request<GenreContainer<Genre>>(request);
var result = await Api.Request<GenreContainer<Genre>>(request, cancellationToken);
return result.genres ?? new List<Genre>();
}

Expand Down
Loading

0 comments on commit b6b9840

Please sign in to comment.