Skip to content

Commit

Permalink
feat(discover): Add new trending source experimental feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sephrat committed Apr 25, 2022
1 parent e0a2331 commit 1a0823c
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 8 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 @@ -17,6 +17,7 @@ public interface IMovieEngineV2
Task<IEnumerable<SearchMovieViewModel>> UpcomingMovies();
Task<IEnumerable<SearchMovieViewModel>> NowPlayingMovies();
Task<IEnumerable<SearchMovieViewModel>> NowPlayingMovies(int currentPosition, int amountToLoad);
Task<IEnumerable<SearchMovieViewModel>> TrendingMovies(int currentPosition, int amountToLoad);
Task<MovieCollectionsViewModel> GetCollection(int collectionId, CancellationToken cancellationToken, string langCode = null);
Task<int> GetTvDbId(int theMovieDbId);
Task<IEnumerable<SearchMovieViewModel>> PopularMovies(int currentlyLoaded, int toLoad, CancellationToken cancellationToken, string langCustomCode = null);
Expand Down
16 changes: 16 additions & 0 deletions src/Ombi.Core/Engine/V2/MovieSearchEngineV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,22 @@ public async Task<IEnumerable<SearchMovieViewModel>> NowPlayingMovies(int curren
}
return await TransformMovieResultsToResponse(results);
}

public async Task<IEnumerable<SearchMovieViewModel>> TrendingMovies(int currentPosition, int amountToLoad)
{
var langCode = await DefaultLanguageCode(null);

var pages = PaginationHelper.GetNextPages(currentPosition, amountToLoad, _theMovieDbMaxPageItems);

var results = new List<MovieDbSearchResult>();
foreach (var pagesToLoad in pages)
{
var apiResult = await Cache.GetOrAddAsync(nameof(NowPlayingMovies) + pagesToLoad.Page + langCode,
() => MovieApi.TrendingMovies(langCode, pagesToLoad.Page), DateTimeOffset.Now.AddHours(12));
results.AddRange(apiResult.Skip(pagesToLoad.Skip).Take(pagesToLoad.Take));
}
return await TransformMovieResultsToResponse(results);
}

public async Task<IEnumerable<SearchMovieViewModel>> SeasonalList(int currentPosition, int amountToLoad, CancellationToken cancellationToken)
{
Expand Down
14 changes: 11 additions & 3 deletions src/Ombi.Core/Engine/V2/TvSearchEngineV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using Ombi.Core.Engine.Interfaces;
using Ombi.Core.Models.UI;
using Ombi.Core.Helpers;
using Ombi.Core.Services;

namespace Ombi.Core.Engine.V2
{
Expand All @@ -37,10 +38,12 @@ public class TvSearchEngineV2 : BaseMediaEngine, ITVSearchEngineV2
private readonly IMovieDbApi _movieApi;
private readonly ISettingsService<CustomizationSettings> _customization;
private readonly ITvRequestEngine _requestEngine;
private readonly IFeatureService _feature;

public TvSearchEngineV2(ICurrentUser identity, IRequestServiceMain service, ITvMazeApi tvMaze, IMapper mapper,
ITraktApi trakt, IRuleEvaluator r, OmbiUserManager um, ICacheService memCache, ISettingsService<OmbiSettings> s,
IRepository<RequestSubscription> sub, IMovieDbApi movieApi, ISettingsService<CustomizationSettings> customization, ITvRequestEngine requestEngine)
IRepository<RequestSubscription> sub, IMovieDbApi movieApi, ISettingsService<CustomizationSettings> customization, ITvRequestEngine requestEngine,
IFeatureService feature)
: base(identity, service, r, um, memCache, s, sub)
{
_tvMaze = tvMaze;
Expand All @@ -49,6 +52,7 @@ public TvSearchEngineV2(ICurrentUser identity, IRequestServiceMain service, ITvM
_movieApi = movieApi;
_customization = customization;
_requestEngine = requestEngine;
_feature = feature;
}


Expand Down Expand Up @@ -132,15 +136,19 @@ public async Task<IEnumerable<SearchTvShowViewModel>> Anticipated(int currentlyL
}

public async Task<IEnumerable<SearchTvShowViewModel>> Trending(int currentlyLoaded, int amountToLoad)
{
{
var langCode = await DefaultLanguageCode(null);
var isNewTrendingSourceEnabled = await _feature.FeatureEnabled(FeatureNames.NewTrendingSource);

var pages = PaginationHelper.GetNextPages(currentlyLoaded, amountToLoad, ResultLimit);
var results = new List<MovieDbSearchResult>();
foreach (var pagesToLoad in pages)
{
var search = ( async () => (isNewTrendingSourceEnabled) ?
await _movieApi.TrendingTv(langCode, pagesToLoad.Page)
: await _movieApi.TopRatedTv(langCode, pagesToLoad.Page));
var apiResult = await Cache.GetOrAddAsync(nameof(Trending) + langCode + pagesToLoad.Page,
async () => await _movieApi.TopRatedTv(langCode, pagesToLoad.Page), DateTimeOffset.Now.AddHours(12));
search, DateTimeOffset.Now.AddHours(12));
results.AddRange(apiResult.Skip(pagesToLoad.Skip).Take(pagesToLoad.Take));
}

Expand Down
1 change: 1 addition & 0 deletions src/Ombi.Settings/Settings/Models/FeatureSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public class FeatureEnablement
public static class FeatureNames
{
public const string Movie4KRequests = nameof(Movie4KRequests);
public const string NewTrendingSource = nameof(NewTrendingSource);
}
}
2 changes: 2 additions & 0 deletions src/Ombi.TheMovieDbApi/IMovieDbApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface IMovieDbApi
Task<MovieResponseDto> GetMovieInformation(int movieId);
Task<MovieResponseDto> GetMovieInformationWithExtraInfo(int movieId, string langCode = "en");
Task<List<MovieDbSearchResult>> NowPlaying(string languageCode, int? page = null);
Task<List<MovieDbSearchResult>> TrendingMovies(string languageCode, int? page = null);
Task<List<MovieDbSearchResult>> PopularMovies(string languageCode, int? page = null, CancellationToken cancellationToken = default(CancellationToken));
Task<List<MovieDbSearchResult>> PopularTv(string langCode, int? page = null, CancellationToken cancellationToken = default(CancellationToken));
Task<List<MovieDbSearchResult>> SearchMovie(string searchTerm, int? year, string languageCode);
Expand All @@ -23,6 +24,7 @@ public interface IMovieDbApi
Task<List<MovieDbSearchResult>> TopRated(string languageCode, int? page = null);
Task<List<MovieDbSearchResult>> Upcoming(string languageCode, int? page = null);
Task<List<MovieDbSearchResult>> TopRatedTv(string languageCode, int? page = null);
Task<List<MovieDbSearchResult>> TrendingTv(string languageCode, int? page = null);
Task<List<MovieDbSearchResult>> UpcomingTv(string languageCode, int? page = null);
Task<List<MovieDbSearchResult>> SimilarMovies(int movieId, string langCode);
Task<FindResult> Find(string externalId, ExternalSource source);
Expand Down
27 changes: 27 additions & 0 deletions src/Ombi.TheMovieDbApi/TheMovieDbApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,33 @@ private async Task<List<MovieDbSearchResult>> TopRated(string type, string langC
var result = await Api.Request<TheMovieDbContainer<SearchResult>>(request);
return Mapper.Map<List<MovieDbSearchResult>>(result.results);
}

public Task<List<MovieDbSearchResult>> TrendingMovies(string langCode, int? page = null)
{
return Trending("movie", langCode, page);
}

public Task<List<MovieDbSearchResult>> TrendingTv(string langCode, int? page = null)
{
return Trending("tv", langCode, page);
}
private async Task<List<MovieDbSearchResult>> Trending(string type, string langCode, int? page = null)
{
// https://developers.themoviedb.org/3/trending/get-trending
var timeWindow = "week"; // another option can be 'day'
var request = new Request($"trending/{type}/{timeWindow}", BaseUri, HttpMethod.Get);
request.AddQueryString("api_key", ApiToken);
request.AddQueryString("language", langCode);

if (page != null)
{
request.AddQueryString("page", page.ToString());
}

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

public Task<List<MovieDbSearchResult>> Upcoming(string langCode, int? page = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@ export class CarouselListComponent implements OnInit {
this.movies = await this.searchService.popularMoviesByPage(this.currentlyLoaded, this.amountToLoad);
break;
case DiscoverType.Trending:
if(this.featureFacade.isNewTrendingSourceEnabled) {
this.movies = await this.searchService.trendingMoviesByPage(this.currentlyLoaded, this.amountToLoad);
} else {
this.movies = await this.searchService.nowPlayingMoviesByPage(this.currentlyLoaded, this.amountToLoad);
}
break;
case DiscoverType.Upcoming:
this.movies = await this.searchService.upcomingMoviesByPage(this.currentlyLoaded, this.amountToLoad);
Expand Down
4 changes: 4 additions & 0 deletions src/Ombi/ClientApp/src/app/services/searchV2.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export class SearchV2Service extends ServiceHelpers {
return this.http.get<ISearchMovieResult[]>(`${this.url}/Movie/nowplaying/${currentlyLoaded}/${toLoad}`).toPromise();
}

public trendingMoviesByPage(currentlyLoaded: number, toLoad: number): Promise<ISearchMovieResult[]> {
return this.http.get<ISearchMovieResult[]>(`${this.url}/Movie/trending/${currentlyLoaded}/${toLoad}`).toPromise();
}

public topRatedMovies(): Observable<ISearchMovieResult[]> {
return this.http.get<ISearchMovieResult[]>(`${this.url}/Movie/toprated`);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Ombi/ClientApp/src/app/state/features/features.facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ export class FeaturesFacade {

public is4kEnabled = (): boolean => this.store.selectSnapshot(FeaturesSelectors.is4kEnabled);

}
public isNewTrendingSourceEnabled = (): boolean => this.store.selectSnapshot(FeaturesSelectors.isNewTrendingSourceEnabled);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ export class FeaturesSelectors {
return features.filter(x => x.name === "Movie4KRequests")[0].enabled;
}

}
@Selector([FeaturesSelectors.features])
public static isNewTrendingSourceEnabled(features: IFeatureEnablement[]): boolean {
return features.filter(x => x.name === "NewTrendingSource")[0].enabled;
}

}
21 changes: 18 additions & 3 deletions src/Ombi/Controllers/V2/SearchController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,21 @@ public Task<IEnumerable<SearchMovieViewModel>> NowPlayingMovies(int currentPosit
() => _movieEngineV2.NowPlayingMovies(currentPosition, amountToLoad),
DateTimeOffset.Now.AddHours(12));
}

/// <summary>
/// Returns trending movies by page
/// </summary>
/// <remarks>We use TheMovieDb as the Provider</remarks>
/// <returns></returns>
[HttpGet("movie/trending/{currentPosition}/{amountToLoad}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesDefaultResponseType]
public Task<IEnumerable<SearchMovieViewModel>> TrendingMovies(int currentPosition, int amountToLoad)
{
return _mediaCacheService.GetOrAddAsync(nameof(TrendingMovies) + currentPosition + amountToLoad,
() => _movieEngineV2.TrendingMovies(currentPosition, amountToLoad),
DateTimeOffset.Now.AddHours(12));
}

/// <summary>
/// Returns top rated movies.
Expand Down Expand Up @@ -392,14 +407,14 @@ public Task<IEnumerable<SearchTvShowViewModel>> MostWatched(int currentPosition,
/// <summary>
/// Returns trending shows by page
/// </summary>
/// <remarks>We use Trakt.tv as the Provider</remarks>
/// <remarks>We use TheMovieDb as the Provider</remarks>
/// <returns></returns>
[HttpGet("tv/trending/{currentPosition}/{amountToLoad}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesDefaultResponseType]
public Task<IEnumerable<SearchTvShowViewModel>> Trending(int currentPosition, int amountToLoad)
public Task<IEnumerable<SearchTvShowViewModel>> TrendingTv(int currentPosition, int amountToLoad)
{
return _mediaCacheService.GetOrAddAsync(nameof(Trending) + currentPosition + amountToLoad,
return _mediaCacheService.GetOrAddAsync(nameof(TrendingTv) + currentPosition + amountToLoad,
() => _tvEngineV2.Trending(currentPosition, amountToLoad),
DateTimeOffset.Now.AddHours(12));
}
Expand Down

0 comments on commit 1a0823c

Please sign in to comment.