|
|
|
@ -20,6 +20,7 @@ namespace ErsatzTV.Core.Plex |
|
|
|
public class PlexTelevisionLibraryScanner : PlexLibraryScanner, IPlexTelevisionLibraryScanner |
|
|
|
public class PlexTelevisionLibraryScanner : PlexLibraryScanner, IPlexTelevisionLibraryScanner |
|
|
|
{ |
|
|
|
{ |
|
|
|
private readonly ILocalFileSystem _localFileSystem; |
|
|
|
private readonly ILocalFileSystem _localFileSystem; |
|
|
|
|
|
|
|
private readonly ILocalStatisticsProvider _localStatisticsProvider; |
|
|
|
private readonly ILogger<PlexTelevisionLibraryScanner> _logger; |
|
|
|
private readonly ILogger<PlexTelevisionLibraryScanner> _logger; |
|
|
|
private readonly IMediaSourceRepository _mediaSourceRepository; |
|
|
|
private readonly IMediaSourceRepository _mediaSourceRepository; |
|
|
|
private readonly IMediator _mediator; |
|
|
|
private readonly IMediator _mediator; |
|
|
|
@ -40,6 +41,7 @@ namespace ErsatzTV.Core.Plex |
|
|
|
IMediaSourceRepository mediaSourceRepository, |
|
|
|
IMediaSourceRepository mediaSourceRepository, |
|
|
|
IPlexPathReplacementService plexPathReplacementService, |
|
|
|
IPlexPathReplacementService plexPathReplacementService, |
|
|
|
ILocalFileSystem localFileSystem, |
|
|
|
ILocalFileSystem localFileSystem, |
|
|
|
|
|
|
|
ILocalStatisticsProvider localStatisticsProvider, |
|
|
|
ILogger<PlexTelevisionLibraryScanner> logger) |
|
|
|
ILogger<PlexTelevisionLibraryScanner> logger) |
|
|
|
: base(metadataRepository, logger) |
|
|
|
: base(metadataRepository, logger) |
|
|
|
{ |
|
|
|
{ |
|
|
|
@ -52,13 +54,15 @@ namespace ErsatzTV.Core.Plex |
|
|
|
_mediaSourceRepository = mediaSourceRepository; |
|
|
|
_mediaSourceRepository = mediaSourceRepository; |
|
|
|
_plexPathReplacementService = plexPathReplacementService; |
|
|
|
_plexPathReplacementService = plexPathReplacementService; |
|
|
|
_localFileSystem = localFileSystem; |
|
|
|
_localFileSystem = localFileSystem; |
|
|
|
|
|
|
|
_localStatisticsProvider = localStatisticsProvider; |
|
|
|
_logger = logger; |
|
|
|
_logger = logger; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public async Task<Either<BaseError, Unit>> ScanLibrary( |
|
|
|
public async Task<Either<BaseError, Unit>> ScanLibrary( |
|
|
|
PlexConnection connection, |
|
|
|
PlexConnection connection, |
|
|
|
PlexServerAuthToken token, |
|
|
|
PlexServerAuthToken token, |
|
|
|
PlexLibrary library) |
|
|
|
PlexLibrary library, |
|
|
|
|
|
|
|
string ffprobePath) |
|
|
|
{ |
|
|
|
{ |
|
|
|
List<PlexPathReplacement> pathReplacements = await _mediaSourceRepository |
|
|
|
List<PlexPathReplacement> pathReplacements = await _mediaSourceRepository |
|
|
|
.GetPlexPathReplacements(library.MediaSourceId); |
|
|
|
.GetPlexPathReplacements(library.MediaSourceId); |
|
|
|
@ -85,7 +89,7 @@ namespace ErsatzTV.Core.Plex |
|
|
|
await maybeShow.Match( |
|
|
|
await maybeShow.Match( |
|
|
|
async result => |
|
|
|
async result => |
|
|
|
{ |
|
|
|
{ |
|
|
|
await ScanSeasons(library, pathReplacements, result.Item, connection, token); |
|
|
|
await ScanSeasons(library, pathReplacements, result.Item, connection, token, ffprobePath); |
|
|
|
|
|
|
|
|
|
|
|
if (result.IsAdded) |
|
|
|
if (result.IsAdded) |
|
|
|
{ |
|
|
|
{ |
|
|
|
@ -288,7 +292,8 @@ namespace ErsatzTV.Core.Plex |
|
|
|
List<PlexPathReplacement> pathReplacements, |
|
|
|
List<PlexPathReplacement> pathReplacements, |
|
|
|
PlexShow show, |
|
|
|
PlexShow show, |
|
|
|
PlexConnection connection, |
|
|
|
PlexConnection connection, |
|
|
|
PlexServerAuthToken token) |
|
|
|
PlexServerAuthToken token, |
|
|
|
|
|
|
|
string ffprobePath) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Either<BaseError, List<PlexSeason>> entries = await _plexServerApiClient.GetShowSeasons( |
|
|
|
Either<BaseError, List<PlexSeason>> entries = await _plexServerApiClient.GetShowSeasons( |
|
|
|
library, |
|
|
|
library, |
|
|
|
@ -309,7 +314,13 @@ namespace ErsatzTV.Core.Plex |
|
|
|
.BindT(existing => UpdateMetadataAndArtwork(existing, incoming)); |
|
|
|
.BindT(existing => UpdateMetadataAndArtwork(existing, incoming)); |
|
|
|
|
|
|
|
|
|
|
|
await maybeSeason.Match( |
|
|
|
await maybeSeason.Match( |
|
|
|
async season => await ScanEpisodes(library, pathReplacements, season, connection, token), |
|
|
|
async season => await ScanEpisodes( |
|
|
|
|
|
|
|
library, |
|
|
|
|
|
|
|
pathReplacements, |
|
|
|
|
|
|
|
season, |
|
|
|
|
|
|
|
connection, |
|
|
|
|
|
|
|
token, |
|
|
|
|
|
|
|
ffprobePath), |
|
|
|
error => |
|
|
|
error => |
|
|
|
{ |
|
|
|
{ |
|
|
|
_logger.LogWarning( |
|
|
|
_logger.LogWarning( |
|
|
|
@ -373,7 +384,8 @@ namespace ErsatzTV.Core.Plex |
|
|
|
List<PlexPathReplacement> pathReplacements, |
|
|
|
List<PlexPathReplacement> pathReplacements, |
|
|
|
PlexSeason season, |
|
|
|
PlexSeason season, |
|
|
|
PlexConnection connection, |
|
|
|
PlexConnection connection, |
|
|
|
PlexServerAuthToken token) |
|
|
|
PlexServerAuthToken token, |
|
|
|
|
|
|
|
string ffprobePath) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Either<BaseError, List<PlexEpisode>> entries = await _plexServerApiClient.GetSeasonEpisodes( |
|
|
|
Either<BaseError, List<PlexEpisode>> entries = await _plexServerApiClient.GetSeasonEpisodes( |
|
|
|
library, |
|
|
|
library, |
|
|
|
@ -414,11 +426,13 @@ namespace ErsatzTV.Core.Plex |
|
|
|
.BindT(existing => UpdateMetadata(existing, incoming)) |
|
|
|
.BindT(existing => UpdateMetadata(existing, incoming)) |
|
|
|
.BindT( |
|
|
|
.BindT( |
|
|
|
existing => UpdateStatistics( |
|
|
|
existing => UpdateStatistics( |
|
|
|
|
|
|
|
pathReplacements, |
|
|
|
existing, |
|
|
|
existing, |
|
|
|
incoming, |
|
|
|
incoming, |
|
|
|
library, |
|
|
|
library, |
|
|
|
connection, |
|
|
|
connection, |
|
|
|
token)) |
|
|
|
token, |
|
|
|
|
|
|
|
ffprobePath)) |
|
|
|
.BindT(existing => UpdateArtwork(existing, incoming)); |
|
|
|
.BindT(existing => UpdateArtwork(existing, incoming)); |
|
|
|
|
|
|
|
|
|
|
|
await maybeEpisode.Match( |
|
|
|
await maybeEpisode.Match( |
|
|
|
@ -484,57 +498,89 @@ namespace ErsatzTV.Core.Plex |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private async Task<Either<BaseError, PlexEpisode>> UpdateStatistics( |
|
|
|
private async Task<Either<BaseError, PlexEpisode>> UpdateStatistics( |
|
|
|
|
|
|
|
List<PlexPathReplacement> pathReplacements, |
|
|
|
PlexEpisode existing, |
|
|
|
PlexEpisode existing, |
|
|
|
PlexEpisode incoming, |
|
|
|
PlexEpisode incoming, |
|
|
|
PlexLibrary library, |
|
|
|
PlexLibrary library, |
|
|
|
PlexConnection connection, |
|
|
|
PlexConnection connection, |
|
|
|
PlexServerAuthToken token) |
|
|
|
PlexServerAuthToken token, |
|
|
|
|
|
|
|
string ffprobePath) |
|
|
|
{ |
|
|
|
{ |
|
|
|
MediaVersion existingVersion = existing.MediaVersions.Head(); |
|
|
|
MediaVersion existingVersion = existing.MediaVersions.Head(); |
|
|
|
MediaVersion incomingVersion = incoming.MediaVersions.Head(); |
|
|
|
MediaVersion incomingVersion = incoming.MediaVersions.Head(); |
|
|
|
|
|
|
|
|
|
|
|
if (incomingVersion.DateUpdated > existingVersion.DateUpdated || !existingVersion.Streams.Any()) |
|
|
|
if (incomingVersion.DateUpdated > existingVersion.DateUpdated || !existingVersion.Streams.Any()) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Either<BaseError, Tuple<EpisodeMetadata, MediaVersion>> maybeStatistics = |
|
|
|
string localPath = _plexPathReplacementService.GetReplacementPlexPath( |
|
|
|
await _plexServerApiClient.GetEpisodeMetadataAndStatistics( |
|
|
|
pathReplacements, |
|
|
|
library, |
|
|
|
incoming.MediaVersions.Head().MediaFiles.Head().Path, |
|
|
|
incoming.Key.Split("/").Last(), |
|
|
|
false); |
|
|
|
connection, |
|
|
|
|
|
|
|
token); |
|
|
|
_logger.LogDebug("Refreshing {Attribute} for {Path}", "Statistics", localPath); |
|
|
|
|
|
|
|
Either<BaseError, bool> refreshResult = |
|
|
|
await maybeStatistics.Match( |
|
|
|
await _localStatisticsProvider.RefreshStatistics(ffprobePath, incoming, localPath); |
|
|
|
async tuple => |
|
|
|
|
|
|
|
|
|
|
|
await refreshResult.Match( |
|
|
|
|
|
|
|
async _ => |
|
|
|
{ |
|
|
|
{ |
|
|
|
(EpisodeMetadata incomingMetadata, MediaVersion mediaVersion) = tuple; |
|
|
|
foreach (MediaItem updated in await _searchRepository.GetItemToIndex(incoming.Id)) |
|
|
|
|
|
|
|
|
|
|
|
Option<EpisodeMetadata> maybeExisting = existing.EpisodeMetadata |
|
|
|
|
|
|
|
.Find(em => em.EpisodeNumber == incomingMetadata.EpisodeNumber); |
|
|
|
|
|
|
|
foreach (EpisodeMetadata existingMetadata in maybeExisting) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
foreach (MetadataGuid guid in existingMetadata.Guids |
|
|
|
await _searchIndex.UpdateItems( |
|
|
|
.Filter(g => incomingMetadata.Guids.All(g2 => g2.Guid != g.Guid)) |
|
|
|
_searchRepository, |
|
|
|
.ToList()) |
|
|
|
new List<MediaItem> { updated }); |
|
|
|
{ |
|
|
|
} |
|
|
|
existingMetadata.Guids.Remove(guid); |
|
|
|
|
|
|
|
await _metadataRepository.RemoveGuid(guid); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (MetadataGuid guid in incomingMetadata.Guids |
|
|
|
Either<BaseError, Tuple<EpisodeMetadata, MediaVersion>> maybeStatistics = |
|
|
|
.Filter(g => existingMetadata.Guids.All(g2 => g2.Guid != g.Guid)) |
|
|
|
await _plexServerApiClient.GetEpisodeMetadataAndStatistics( |
|
|
|
.ToList()) |
|
|
|
library, |
|
|
|
|
|
|
|
incoming.Key.Split("/").Last(), |
|
|
|
|
|
|
|
connection, |
|
|
|
|
|
|
|
token); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await maybeStatistics.Match( |
|
|
|
|
|
|
|
async tuple => |
|
|
|
{ |
|
|
|
{ |
|
|
|
existingMetadata.Guids.Add(guid); |
|
|
|
(EpisodeMetadata incomingMetadata, MediaVersion mediaVersion) = tuple; |
|
|
|
await _metadataRepository.AddGuid(existingMetadata, guid); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
existingVersion.SampleAspectRatio = mediaVersion.SampleAspectRatio; |
|
|
|
Option<EpisodeMetadata> maybeExisting = existing.EpisodeMetadata |
|
|
|
existingVersion.VideoScanKind = mediaVersion.VideoScanKind; |
|
|
|
.Find(em => em.EpisodeNumber == incomingMetadata.EpisodeNumber); |
|
|
|
existingVersion.DateUpdated = mediaVersion.DateUpdated; |
|
|
|
foreach (EpisodeMetadata existingMetadata in maybeExisting) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
foreach (MetadataGuid guid in existingMetadata.Guids |
|
|
|
|
|
|
|
.Filter(g => incomingMetadata.Guids.All(g2 => g2.Guid != g.Guid)) |
|
|
|
|
|
|
|
.ToList()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
existingMetadata.Guids.Remove(guid); |
|
|
|
|
|
|
|
await _metadataRepository.RemoveGuid(guid); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (MetadataGuid guid in incomingMetadata.Guids |
|
|
|
|
|
|
|
.Filter(g => existingMetadata.Guids.All(g2 => g2.Guid != g.Guid)) |
|
|
|
|
|
|
|
.ToList()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
existingMetadata.Guids.Add(guid); |
|
|
|
|
|
|
|
await _metadataRepository.AddGuid(existingMetadata, guid); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
existingVersion.SampleAspectRatio = mediaVersion.SampleAspectRatio; |
|
|
|
|
|
|
|
existingVersion.VideoScanKind = mediaVersion.VideoScanKind; |
|
|
|
|
|
|
|
existingVersion.DateUpdated = mediaVersion.DateUpdated; |
|
|
|
|
|
|
|
|
|
|
|
await _metadataRepository.UpdatePlexStatistics(existingVersion.Id, mediaVersion); |
|
|
|
await _metadataRepository.UpdatePlexStatistics(existingVersion.Id, mediaVersion); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
_ => Task.CompletedTask); |
|
|
|
}, |
|
|
|
}, |
|
|
|
_ => Task.CompletedTask); |
|
|
|
error => |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
_logger.LogWarning( |
|
|
|
|
|
|
|
"Unable to refresh {Attribute} for media item {Path}. Error: {Error}", |
|
|
|
|
|
|
|
"Statistics", |
|
|
|
|
|
|
|
localPath, |
|
|
|
|
|
|
|
error.Value); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask; |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return Right<BaseError, PlexEpisode>(existing); |
|
|
|
return Right<BaseError, PlexEpisode>(existing); |
|
|
|
|