mirror of https://github.com/ErsatzTV/ErsatzTV.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.9 KiB
77 lines
2.9 KiB
using System.Collections.Generic; |
|
using System.Threading.Tasks; |
|
using ErsatzTV.Core.Domain; |
|
using ErsatzTV.Core.Interfaces.Plex; |
|
using ErsatzTV.Core.Interfaces.Repositories; |
|
using LanguageExt; |
|
using Microsoft.Extensions.Logging; |
|
using static LanguageExt.Prelude; |
|
|
|
namespace ErsatzTV.Core.Plex |
|
{ |
|
public class PlexMovieLibraryScanner : IPlexMovieLibraryScanner |
|
{ |
|
private readonly ILogger<PlexMovieLibraryScanner> _logger; |
|
private readonly IMovieRepository _movieRepository; |
|
private readonly IPlexServerApiClient _plexServerApiClient; |
|
|
|
public PlexMovieLibraryScanner( |
|
IPlexServerApiClient plexServerApiClient, |
|
IMovieRepository movieRepository, |
|
ILogger<PlexMovieLibraryScanner> logger) |
|
{ |
|
_plexServerApiClient = plexServerApiClient; |
|
_movieRepository = movieRepository; |
|
_logger = logger; |
|
} |
|
|
|
public async Task<Either<BaseError, Unit>> ScanLibrary( |
|
PlexConnection connection, |
|
PlexServerAuthToken token, |
|
PlexLibrary plexMediaSourceLibrary) |
|
{ |
|
Either<BaseError, List<PlexMovie>> entries = await _plexServerApiClient.GetLibraryContents( |
|
plexMediaSourceLibrary, |
|
connection, |
|
token); |
|
|
|
await entries.Match( |
|
async movieEntries => |
|
{ |
|
foreach (PlexMovie entry in movieEntries) |
|
{ |
|
// TODO: optimize dbcontext use here, do we need tracking? can we make partial updates with dapper? |
|
// TODO: figure out how to rebuild playlists |
|
Either<BaseError, PlexMovie> maybeMovie = await _movieRepository |
|
.GetOrAdd(plexMediaSourceLibrary, entry) |
|
.BindT(UpdateIfNeeded); |
|
|
|
maybeMovie.IfLeft( |
|
error => _logger.LogWarning( |
|
"Error processing plex movie at {Key}: {Error}", |
|
entry.Key, |
|
error.Value)); |
|
} |
|
}, |
|
error => |
|
{ |
|
_logger.LogWarning( |
|
"Error synchronizing plex library {Path}: {Error}", |
|
plexMediaSourceLibrary.Name, |
|
error.Value); |
|
|
|
return Task.CompletedTask; |
|
}); |
|
|
|
// need plex media item model that can be used to lookup by unique id (metadata key?) |
|
|
|
return Unit.Default; |
|
} |
|
|
|
private Task<Either<BaseError, PlexMovie>> UpdateIfNeeded(PlexMovie plexMovie) => |
|
// .BindT(movie => UpdateStatistics(movie, ffprobePath).MapT(_ => movie)) |
|
// .BindT(UpdateMetadata) |
|
// .BindT(UpdatePoster); |
|
Right<BaseError, PlexMovie>(plexMovie).AsTask(); |
|
} |
|
}
|
|
|