Compare commits

...
Author SHA1 Message Date
Jason DoveandGitHub 0799fe25d1 optimize local library scanning by using etags (#196)
* use etags to optimize local movie scanner

* use etags to optimize local television scanner

* use etags to optimize local music video scanner

* code cleanup
2021-05-21 06:18:07 -05:00
Jason DoveandGitHub c0b5ecd388 custom binding and port number (#195)
* allow custom bindings

* reorganize

* cleanup
2021-05-20 20:09:14 -05:00
Jason DoveandGitHub 5fd0cc5469 only initialize search index on startup (#193) 2021-05-19 21:09:01 -05:00
Jason DoveandGitHub 34ebe9b006 handle "other" jellyfin libraries (#192) 2021-05-19 20:15:16 -05:00
Jason DoveandGitHub d7c080cafd optimize plex tv scanner (#190) 2021-05-19 07:22:42 -05:00
Jason DoveandGitHub 23bab01f2d add multi-part episode tests (#189) 2021-05-18 11:33:34 -05:00
Jason Dove c7fdacf30f another multi-episode bugfix 2021-05-18 11:00:21 -05:00
Jason Dove 6e6d53d847 multi-episode grouping bugfix 2021-05-18 09:56:04 -05:00
Jason DoveandGitHub 47e9a319ce add option to keep multi-part episodes together when shuffling (#188)
* add setting to keep multi-part episodes together

* keep multi-part episodes together when shuffling
2021-05-18 08:23:08 -05:00
Jason DoveandGitHub 9112cb3c1f only scale to even dimensions (#187) 2021-05-16 15:47:44 -05:00
55 changed files with 5786 additions and 199 deletions
@@ -57,7 +57,7 @@ namespace ErsatzTV.Application.Jellyfin.Commands
return await maybeUserId.Match(
userId =>
{
_logger.LogDebug("Jellyfin admin user id is {UserId}", userId);
// _logger.LogDebug("Jellyfin admin user id is {UserId}", userId);
_memoryCache.Set($"jellyfin_admin_user_id.{parameters.JellyfinMediaSource.Id}", userId);
return Task.FromResult<Either<BaseError, Unit>>(Unit.Default);
},
@@ -90,7 +90,6 @@ namespace ErsatzTV.Application.MediaSources.Commands
await _movieFolderScanner.ScanFolder(
libraryPath,
ffprobePath,
lastScan,
progressMin,
progressMax);
break;
@@ -98,7 +97,6 @@ namespace ErsatzTV.Application.MediaSources.Commands
await _televisionFolderScanner.ScanFolder(
libraryPath,
ffprobePath,
lastScan,
progressMin,
progressMax);
break;
@@ -106,7 +104,6 @@ namespace ErsatzTV.Application.MediaSources.Commands
await _musicVideoFolderScanner.ScanFolder(
libraryPath,
ffprobePath,
lastScan,
progressMin,
progressMax);
break;
@@ -5,6 +5,8 @@ using MediatR;
namespace ErsatzTV.Application.ProgramSchedules.Commands
{
public record CreateProgramSchedule(string Name, PlaybackOrder MediaCollectionPlaybackOrder) :
IRequest<Either<BaseError, ProgramScheduleViewModel>>;
public record CreateProgramSchedule(
string Name,
PlaybackOrder MediaCollectionPlaybackOrder,
bool KeepMultiPartEpisodesTogether) : IRequest<Either<BaseError, ProgramScheduleViewModel>>;
}
@@ -36,7 +36,11 @@ namespace ErsatzTV.Application.ProgramSchedules.Commands
.MapT(
name => new ProgramSchedule
{
Name = name, MediaCollectionPlaybackOrder = request.MediaCollectionPlaybackOrder
Name = name,
MediaCollectionPlaybackOrder = request.MediaCollectionPlaybackOrder,
KeepMultiPartEpisodesTogether =
request.MediaCollectionPlaybackOrder == PlaybackOrder.Shuffle &&
request.KeepMultiPartEpisodesTogether
});
private async Task<Validation<BaseError, string>> ValidateName(CreateProgramSchedule createProgramSchedule)
@@ -9,5 +9,6 @@ namespace ErsatzTV.Application.ProgramSchedules.Commands
(
int ProgramScheduleId,
string Name,
PlaybackOrder MediaCollectionPlaybackOrder) : IRequest<Either<BaseError, ProgramScheduleViewModel>>;
PlaybackOrder MediaCollectionPlaybackOrder,
bool KeepMultiPartEpisodesTogether) : IRequest<Either<BaseError, ProgramScheduleViewModel>>;
}
@@ -37,12 +37,16 @@ namespace ErsatzTV.Application.ProgramSchedules.Commands
ProgramSchedule programSchedule,
UpdateProgramSchedule update)
{
// we only need to rebuild playouts if the playback order has been modified
// we need to rebuild playouts if the playback order or keep multi-episodes has been modified
bool needToRebuildPlayout =
programSchedule.MediaCollectionPlaybackOrder != update.MediaCollectionPlaybackOrder;
programSchedule.MediaCollectionPlaybackOrder != update.MediaCollectionPlaybackOrder ||
programSchedule.KeepMultiPartEpisodesTogether != update.KeepMultiPartEpisodesTogether;
programSchedule.Name = update.Name;
programSchedule.MediaCollectionPlaybackOrder = update.MediaCollectionPlaybackOrder;
programSchedule.KeepMultiPartEpisodesTogether =
update.MediaCollectionPlaybackOrder == PlaybackOrder.Shuffle &&
update.KeepMultiPartEpisodesTogether;
await _programScheduleRepository.Update(programSchedule);
if (needToRebuildPlayout)
@@ -6,7 +6,11 @@ namespace ErsatzTV.Application.ProgramSchedules
internal static class Mapper
{
internal static ProgramScheduleViewModel ProjectToViewModel(ProgramSchedule programSchedule) =>
new(programSchedule.Id, programSchedule.Name, programSchedule.MediaCollectionPlaybackOrder);
new(
programSchedule.Id,
programSchedule.Name,
programSchedule.MediaCollectionPlaybackOrder,
programSchedule.KeepMultiPartEpisodesTogether);
internal static ProgramScheduleItemViewModel ProjectToViewModel(ProgramScheduleItem programScheduleItem) =>
programScheduleItem switch
@@ -2,5 +2,9 @@
namespace ErsatzTV.Application.ProgramSchedules
{
public record ProgramScheduleViewModel(int Id, string Name, PlaybackOrder MediaCollectionPlaybackOrder);
public record ProgramScheduleViewModel(
int Id,
string Name,
PlaybackOrder MediaCollectionPlaybackOrder,
bool KeepMultiPartEpisodesTogether);
}
@@ -1,6 +1,7 @@
using System;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.FFmpeg;
using ErsatzTV.Core.Interfaces.FFmpeg;
using FluentAssertions;
using NUnit.Framework;
@@ -277,6 +278,32 @@ namespace ErsatzTV.Core.Tests.FFmpeg
actual.PadToDesiredResolution.Should().BeTrue();
}
[Test]
public void Should_ScaleToEvenDimensions_ForTransportStream()
{
FFmpegProfile ffmpegProfile = TestProfile() with
{
NormalizeVideo = true,
Resolution = new Resolution { Width = 1280, Height = 720 }
};
var version = new MediaVersion { Width = 706, Height = 362, SampleAspectRatio = "32:27" };
FFmpegPlaybackSettings actual = _calculator.CalculateSettings(
StreamingMode.TransportStream,
ffmpegProfile,
version,
new MediaStream(),
new MediaStream(),
DateTimeOffset.Now,
DateTimeOffset.Now);
IDisplaySize scaledSize = actual.ScaledSize.IfNone(new MediaVersion { Width = 0, Height = 0 });
scaledSize.Width.Should().Be(1280);
scaledSize.Height.Should().Be(554);
actual.PadToDesiredResolution.Should().BeTrue();
}
[Test]
public void Should_NotPadToDesiredResolution_When_UnscaledContentIsUnderSized_ForHttpLiveStreaming()
{
@@ -81,12 +81,11 @@ namespace ErsatzTV.Core.Tests.Metadata
MovieFolderScanner service = GetService(
new FakeFileEntry(Path.Combine(FakeRoot, Path.Combine("Movie (2020)", "Movie (2020).mkv")))
);
var libraryPath = new LibraryPath { Path = BadFakeRoot };
var libraryPath = new LibraryPath { Path = BadFakeRoot, LibraryFolders = new List<LibraryFolder>() };
Either<BaseError, Unit> result = await service.ScanFolder(
libraryPath,
FFprobePath,
DateTimeOffset.MinValue,
0,
1);
@@ -106,12 +105,12 @@ namespace ErsatzTV.Core.Tests.Metadata
MovieFolderScanner service = GetService(
new FakeFileEntry(moviePath) { LastWriteTime = DateTime.Now }
);
var libraryPath = new LibraryPath { Id = 1, Path = FakeRoot };
var libraryPath = new LibraryPath
{ Id = 1, Path = FakeRoot, LibraryFolders = new List<LibraryFolder>() };
Either<BaseError, Unit> result = await service.ScanFolder(
libraryPath,
FFprobePath,
DateTimeOffset.MinValue,
0,
1);
@@ -147,12 +146,12 @@ namespace ErsatzTV.Core.Tests.Metadata
new FakeFileEntry(moviePath) { LastWriteTime = DateTime.Now },
new FakeFileEntry(metadataPath)
);
var libraryPath = new LibraryPath { Id = 1, Path = FakeRoot };
var libraryPath = new LibraryPath
{ Id = 1, Path = FakeRoot, LibraryFolders = new List<LibraryFolder>() };
Either<BaseError, Unit> result = await service.ScanFolder(
libraryPath,
FFprobePath,
DateTimeOffset.MinValue,
0,
1);
@@ -189,12 +188,12 @@ namespace ErsatzTV.Core.Tests.Metadata
new FakeFileEntry(moviePath) { LastWriteTime = DateTime.Now },
new FakeFileEntry(metadataPath)
);
var libraryPath = new LibraryPath { Id = 1, Path = FakeRoot };
var libraryPath = new LibraryPath
{ Id = 1, Path = FakeRoot, LibraryFolders = new List<LibraryFolder>() };
Either<BaseError, Unit> result = await service.ScanFolder(
libraryPath,
FFprobePath,
DateTimeOffset.MinValue,
0,
1);
@@ -235,12 +234,12 @@ namespace ErsatzTV.Core.Tests.Metadata
new FakeFileEntry(moviePath) { LastWriteTime = DateTime.Now },
new FakeFileEntry(posterPath) { LastWriteTime = DateTime.Now }
);
var libraryPath = new LibraryPath { Id = 1, Path = FakeRoot };
var libraryPath = new LibraryPath
{ Id = 1, Path = FakeRoot, LibraryFolders = new List<LibraryFolder>() };
Either<BaseError, Unit> result = await service.ScanFolder(
libraryPath,
FFprobePath,
DateTimeOffset.MinValue,
0,
1);
@@ -284,12 +283,12 @@ namespace ErsatzTV.Core.Tests.Metadata
new FakeFileEntry(moviePath) { LastWriteTime = DateTime.Now },
new FakeFileEntry(posterPath) { LastWriteTime = DateTime.Now }
);
var libraryPath = new LibraryPath { Id = 1, Path = FakeRoot };
var libraryPath = new LibraryPath
{ Id = 1, Path = FakeRoot, LibraryFolders = new List<LibraryFolder>() };
Either<BaseError, Unit> result = await service.ScanFolder(
libraryPath,
FFprobePath,
DateTimeOffset.MinValue,
0,
1);
@@ -333,12 +332,12 @@ namespace ErsatzTV.Core.Tests.Metadata
new FakeFileEntry(moviePath) { LastWriteTime = DateTime.Now },
new FakeFileEntry(posterPath) { LastWriteTime = DateTime.Now }
);
var libraryPath = new LibraryPath { Id = 1, Path = FakeRoot };
var libraryPath = new LibraryPath
{ Id = 1, Path = FakeRoot, LibraryFolders = new List<LibraryFolder>() };
Either<BaseError, Unit> result = await service.ScanFolder(
libraryPath,
FFprobePath,
DateTimeOffset.MinValue,
0,
1);
@@ -381,12 +380,12 @@ namespace ErsatzTV.Core.Tests.Metadata
Path.GetDirectoryName(moviePath) ?? string.Empty,
$"Movie (2020)-{extraFile}{videoExtension}"))
);
var libraryPath = new LibraryPath { Id = 1, Path = FakeRoot };
var libraryPath = new LibraryPath
{ Id = 1, Path = FakeRoot, LibraryFolders = new List<LibraryFolder>() };
Either<BaseError, Unit> result = await service.ScanFolder(
libraryPath,
FFprobePath,
DateTimeOffset.MinValue,
0,
1);
@@ -425,12 +424,12 @@ namespace ErsatzTV.Core.Tests.Metadata
Path.GetDirectoryName(moviePath) ?? string.Empty,
Path.Combine(extraFolder, $"Movie (2020){videoExtension}")))
);
var libraryPath = new LibraryPath { Id = 1, Path = FakeRoot };
var libraryPath = new LibraryPath
{ Id = 1, Path = FakeRoot, LibraryFolders = new List<LibraryFolder>() };
Either<BaseError, Unit> result = await service.ScanFolder(
libraryPath,
FFprobePath,
DateTimeOffset.MinValue,
0,
1);
@@ -463,12 +462,12 @@ namespace ErsatzTV.Core.Tests.Metadata
MovieFolderScanner service = GetService(
new FakeFileEntry(moviePath) { LastWriteTime = DateTime.Now }
);
var libraryPath = new LibraryPath { Id = 1, Path = FakeRoot };
var libraryPath = new LibraryPath
{ Id = 1, Path = FakeRoot, LibraryFolders = new List<LibraryFolder>() };
Either<BaseError, Unit> result = await service.ScanFolder(
libraryPath,
FFprobePath,
DateTimeOffset.MinValue,
0,
1);
@@ -503,12 +502,12 @@ namespace ErsatzTV.Core.Tests.Metadata
MovieFolderScanner service = GetService(
new FakeFileEntry(moviePath) { LastWriteTime = DateTime.Now }
);
var libraryPath = new LibraryPath { Id = 1, Path = FakeRoot };
var libraryPath = new LibraryPath
{ Id = 1, Path = FakeRoot, LibraryFolders = new List<LibraryFolder>() };
Either<BaseError, Unit> result = await service.ScanFolder(
libraryPath,
FFprobePath,
DateTimeOffset.MinValue,
0,
1);
@@ -532,12 +531,12 @@ namespace ErsatzTV.Core.Tests.Metadata
MovieFolderScanner service = GetService(
new FakeFileEntry(moviePath) { LastWriteTime = DateTime.Now }
);
var libraryPath = new LibraryPath { Id = 1, Path = FakeRoot };
var libraryPath = new LibraryPath
{ Id = 1, Path = FakeRoot, LibraryFolders = new List<LibraryFolder>() };
Either<BaseError, Unit> result = await service.ScanFolder(
libraryPath,
FFprobePath,
DateTimeOffset.MinValue,
0,
1);
@@ -558,6 +557,7 @@ namespace ErsatzTV.Core.Tests.Metadata
_imageCache.Object,
new Mock<ISearchIndex>().Object,
new Mock<ISearchRepository>().Object,
new Mock<ILibraryRepository>().Object,
new Mock<IMediator>().Object,
new Mock<ILogger<MovieFolderScanner>>().Object
);
@@ -0,0 +1,100 @@
using System.Collections.Generic;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Scheduling;
using FluentAssertions;
using NUnit.Framework;
namespace ErsatzTV.Core.Tests.Scheduling
{
public class MultiPartEpisodeGrouperTests
{
[Test]
public void NotGrouped_Grouped_NotGrouped()
{
var mediaItems = new List<MediaItem>
{
NamedEpisode("Episode 1"),
NamedEpisode("Episode 2 (1)"),
NamedEpisode("Episode 3 (2)"),
NamedEpisode("Episode 4")
};
List<GroupedMediaItem> result = MultiPartEpisodeGrouper.GroupMediaItems(mediaItems);
result.Count.Should().Be(3);
result[0].First.Should().Be(mediaItems[0]);
result[1].First.Should().Be(mediaItems[1]);
result[1].Additional[0].Should().Be(mediaItems[2]);
result[2].First.Should().Be(mediaItems[3]);
}
[Test]
public void Grouped_NotGrouped()
{
var mediaItems = new List<MediaItem>
{
NamedEpisode("Episode 1 (1)"),
NamedEpisode("Episode 2 (2)"),
NamedEpisode("Episode 3")
};
List<GroupedMediaItem> result = MultiPartEpisodeGrouper.GroupMediaItems(mediaItems);
result.Count.Should().Be(2);
result[0].First.Should().Be(mediaItems[0]);
result[0].Additional[0].Should().Be(mediaItems[1]);
result[1].First.Should().Be(mediaItems[2]);
}
[Test]
public void Grouped_NotGrouped_Grouped()
{
var mediaItems = new List<MediaItem>
{
NamedEpisode("Episode 1 (1)"),
NamedEpisode("Episode 2 (2)"),
NamedEpisode("Episode 3"),
NamedEpisode("Episode 4 (1)"),
NamedEpisode("Episode 5 (2)")
};
List<GroupedMediaItem> result = MultiPartEpisodeGrouper.GroupMediaItems(mediaItems);
result.Count.Should().Be(3);
result[0].First.Should().Be(mediaItems[0]);
result[0].Additional[0].Should().Be(mediaItems[1]);
result[1].First.Should().Be(mediaItems[2]);
result[2].First.Should().Be(mediaItems[3]);
result[2].Additional[0].Should().Be(mediaItems[4]);
}
[Test]
public void Grouped_Grouped()
{
var mediaItems = new List<MediaItem>
{
NamedEpisode("Episode 1 (1)"),
NamedEpisode("Episode 2 (2)"),
NamedEpisode("Episode 3 (1)"),
NamedEpisode("Episode 4 (2)")
};
List<GroupedMediaItem> result = MultiPartEpisodeGrouper.GroupMediaItems(mediaItems);
result.Count.Should().Be(2);
result[0].First.Should().Be(mediaItems[0]);
result[0].Additional[0].Should().Be(mediaItems[1]);
result[1].First.Should().Be(mediaItems[2]);
result[1].Additional[0].Should().Be(mediaItems[3]);
}
private static Episode NamedEpisode(string title) =>
new()
{
EpisodeMetadata = new List<EpisodeMetadata>
{
new() { Title = title }
}
};
}
}
@@ -23,7 +23,7 @@ namespace ErsatzTV.Core.Tests.Scheduling
// normally returns 10 5 7 4 3 6 2 8 9 1 1 (note duplicate 1 at end)
var state = new CollectionEnumeratorState { Seed = 8 };
var shuffledContent = new ShuffledMediaCollectionEnumerator(contents, state);
var shuffledContent = new ShuffledMediaCollectionEnumerator(contents, state, false);
var list = new List<int>();
for (var i = 1; i <= 1000; i++)
@@ -50,7 +50,7 @@ namespace ErsatzTV.Core.Tests.Scheduling
var state = new CollectionEnumeratorState();
var shuffledContent = new ShuffledMediaCollectionEnumerator(contents, state);
var shuffledContent = new ShuffledMediaCollectionEnumerator(contents, state, false);
var list = new List<int>();
for (var i = 1; i <= 10; i++)
@@ -70,7 +70,7 @@ namespace ErsatzTV.Core.Tests.Scheduling
var state = new CollectionEnumeratorState();
var shuffledContent = new ShuffledMediaCollectionEnumerator(contents, state);
var shuffledContent = new ShuffledMediaCollectionEnumerator(contents, state, false);
var list = new List<int>();
for (var i = 1; i <= 10; i++)
@@ -90,7 +90,7 @@ namespace ErsatzTV.Core.Tests.Scheduling
List<MediaItem> contents = Episodes(10);
var state = new CollectionEnumeratorState();
var shuffledContent = new ShuffledMediaCollectionEnumerator(contents, state);
var shuffledContent = new ShuffledMediaCollectionEnumerator(contents, state, false);
for (var i = 0; i < 10; i++)
{
@@ -105,7 +105,7 @@ namespace ErsatzTV.Core.Tests.Scheduling
List<MediaItem> contents = Episodes(10);
var state = new CollectionEnumeratorState { Index = 5, Seed = MagicSeed };
var shuffledContent = new ShuffledMediaCollectionEnumerator(contents, state);
var shuffledContent = new ShuffledMediaCollectionEnumerator(contents, state, false);
for (var i = 6; i <= 10; i++)
{
@@ -0,0 +1,11 @@
namespace ErsatzTV.Core.Domain
{
public class LibraryFolder
{
public int Id { get; set; }
public string Path { get; set; }
public int LibraryPathId { get; set; }
public LibraryPath LibraryPath { get; set; }
public string Etag { get; set; }
}
}
@@ -13,5 +13,6 @@ namespace ErsatzTV.Core.Domain
public Library Library { get; set; }
public List<MediaItem> MediaItems { get; set; }
public List<LibraryFolder> LibraryFolders { get; set; }
}
}
@@ -1,7 +1,9 @@
using System.Collections.Generic;
using System.Diagnostics;
namespace ErsatzTV.Core.Domain
{
[DebuggerDisplay("{EpisodeMetadata[0].Title}")]
public class Episode : MediaItem
{
public int EpisodeNumber { get; set; }
@@ -1,5 +1,8 @@
namespace ErsatzTV.Core.Domain
using System.Diagnostics;
namespace ErsatzTV.Core.Domain
{
[DebuggerDisplay("{EpisodeMetadata[0].Title}")]
public class JellyfinEpisode : Episode
{
public string ItemId { get; set; }
+1
View File
@@ -7,6 +7,7 @@ namespace ErsatzTV.Core.Domain
public int Id { get; set; }
public string Name { get; set; }
public PlaybackOrder MediaCollectionPlaybackOrder { get; set; }
public bool KeepMultiPartEpisodesTogether { get; set; }
public List<ProgramScheduleItem> Items { get; set; }
public List<Playout> Playouts { get; set; }
}
+2 -2
View File
@@ -4,7 +4,7 @@
{
public override string ToString() =>
$@"ffconcat version 1.0
file http://localhost:8409/ffmpeg/stream/{ChannelNumber}
file http://localhost:8409/ffmpeg/stream/{ChannelNumber}";
file http://localhost:{Settings.ListenPort}/ffmpeg/stream/{ChannelNumber}
file http://localhost:{Settings.ListenPort}/ffmpeg/stream/{ChannelNumber}";
}
}
@@ -79,7 +79,9 @@ namespace ErsatzTV.Core.FFmpeg
IDisplaySize scaledSize = CalculateScaledSize(ffmpegProfile, version);
if (!scaledSize.IsSameSizeAs(version))
{
result.ScaledSize = Some(CalculateScaledSize(ffmpegProfile, version));
int fixedHeight = scaledSize.Height + scaledSize.Height % 2;
int fixedWidth = scaledSize.Width + scaledSize.Width % 2;
result.ScaledSize = Some((IDisplaySize) new DisplaySize(fixedWidth, fixedHeight));
}
}
+1 -1
View File
@@ -134,7 +134,7 @@ namespace ErsatzTV.Core.FFmpeg
.WithFormatFlags(playbackSettings.FormatFlags)
.WithRealtimeOutput(playbackSettings.RealtimeOutput)
.WithInfiniteLoop()
.WithConcat($"http://localhost:8409/ffmpeg/concat/{channel.Number}")
.WithConcat($"http://localhost:{Settings.ListenPort}/ffmpeg/concat/{channel.Number}")
.WithMetadata(channel)
.WithFormat("mpegts")
.WithPipe()
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;
using ErsatzTV.Core.Domain;
using LanguageExt;
@@ -10,7 +9,6 @@ namespace ErsatzTV.Core.Interfaces.Metadata
Task<Either<BaseError, Unit>> ScanFolder(
LibraryPath libraryPath,
string ffprobePath,
DateTimeOffset lastScan,
decimal progressMin,
decimal progressMax);
}
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;
using ErsatzTV.Core.Domain;
using LanguageExt;
@@ -10,7 +9,6 @@ namespace ErsatzTV.Core.Interfaces.Metadata
Task<Either<BaseError, Unit>> ScanFolder(
LibraryPath libraryPath,
string ffprobePath,
DateTimeOffset lastScan,
decimal progressMin,
decimal progressMax);
}
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;
using ErsatzTV.Core.Domain;
using LanguageExt;
@@ -10,7 +9,6 @@ namespace ErsatzTV.Core.Interfaces.Metadata
Task<Either<BaseError, Unit>> ScanFolder(
LibraryPath libraryPath,
string ffprobePath,
DateTimeOffset lastScan,
decimal progressMin,
decimal progressMax);
}
@@ -18,5 +18,6 @@ namespace ErsatzTV.Core.Interfaces.Repositories
Task<int> CountMediaItemsByPath(int libraryPathId);
Task<List<int>> GetMediaIdsByLocalPath(int libraryPathId);
Task DeleteLocalPath(int libraryPathId);
Task<Unit> SetEtag(LibraryPath libraryPath, Option<LibraryFolder> knownFolder, string path, string etag);
}
}
+35
View File
@@ -0,0 +1,35 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using ErsatzTV.Core.Interfaces.Metadata;
using static LanguageExt.Prelude;
namespace ErsatzTV.Core.Metadata
{
public static class FolderEtag
{
private static readonly MD5CryptoServiceProvider Crypto = new();
public static string Calculate(string folder, ILocalFileSystem localFileSystem)
{
IEnumerable<string> allFiles = localFileSystem.ListFiles(folder);
var sb = new StringBuilder();
foreach (string file in allFiles.OrderBy(identity))
{
sb.Append(file);
sb.Append(localFileSystem.GetLastWriteTime(file).Ticks);
}
var hash = new StringBuilder();
byte[] bytes = Crypto.ComputeHash(Encoding.UTF8.GetBytes(sb.ToString()));
foreach (byte t in bytes)
{
hash.Append(t.ToString("x2"));
}
return hash.ToString();
}
}
}
+1 -1
View File
@@ -78,7 +78,7 @@ namespace ErsatzTV.Core.Metadata
string path = version.MediaFiles.Head().Path;
if (version.DateUpdated < _localFileSystem.GetLastWriteTime(path) || !version.Streams.Any())
if (version.DateUpdated != _localFileSystem.GetLastWriteTime(path) || !version.Streams.Any())
{
_logger.LogDebug("Refreshing {Attribute} for {Path}", "Statistics", path);
Either<BaseError, bool> refreshResult =
+20 -4
View File
@@ -20,6 +20,7 @@ namespace ErsatzTV.Core.Metadata
{
public class MovieFolderScanner : LocalFolderScanner, IMovieFolderScanner
{
private readonly ILibraryRepository _libraryRepository;
private readonly ILocalFileSystem _localFileSystem;
private readonly ILocalMetadataProvider _localMetadataProvider;
private readonly ILogger<MovieFolderScanner> _logger;
@@ -37,6 +38,7 @@ namespace ErsatzTV.Core.Metadata
IImageCache imageCache,
ISearchIndex searchIndex,
ISearchRepository searchRepository,
ILibraryRepository libraryRepository,
IMediator mediator,
ILogger<MovieFolderScanner> logger)
: base(localFileSystem, localStatisticsProvider, metadataRepository, imageCache, logger)
@@ -46,6 +48,7 @@ namespace ErsatzTV.Core.Metadata
_localMetadataProvider = localMetadataProvider;
_searchIndex = searchIndex;
_searchRepository = searchRepository;
_libraryRepository = libraryRepository;
_mediator = mediator;
_logger = logger;
}
@@ -53,7 +56,6 @@ namespace ErsatzTV.Core.Metadata
public async Task<Either<BaseError, Unit>> ScanFolder(
LibraryPath libraryPath,
string ffprobePath,
DateTimeOffset lastScan,
decimal progressMin,
decimal progressMax)
{
@@ -81,7 +83,9 @@ namespace ErsatzTV.Core.Metadata
string movieFolder = folderQueue.Dequeue();
foldersCompleted++;
var allFiles = _localFileSystem.ListFiles(movieFolder)
var filesForEtag = _localFileSystem.ListFiles(movieFolder).ToList();
var allFiles = filesForEtag
.Filter(f => VideoFileExtensions.Contains(Path.GetExtension(f)))
.Filter(
f => !ExtraFiles.Any(
@@ -98,11 +102,21 @@ namespace ErsatzTV.Core.Metadata
continue;
}
if (allFiles.All(file => _localFileSystem.GetLastWriteTime(file) < lastScan))
string etag = FolderEtag.Calculate(movieFolder, _localFileSystem);
Option<LibraryFolder> knownFolder = libraryPath.LibraryFolders
.Filter(f => f.Path == movieFolder)
.HeadOrNone();
// skip folder if etag matches
if (await knownFolder.Map(f => f.Etag).IfNoneAsync(string.Empty) == etag)
{
continue;
}
_logger.LogDebug(
"UPDATE: Etag has changed for folder {Folder}",
movieFolder);
foreach (string file in allFiles.OrderBy(identity))
{
// TODO: figure out how to rebuild playlists
@@ -124,6 +138,8 @@ namespace ErsatzTV.Core.Metadata
{
await _searchIndex.UpdateItems(_searchRepository, new List<MediaItem> { result.Item });
}
await _libraryRepository.SetEtag(libraryPath, knownFolder, movieFolder, etag);
},
error =>
{
@@ -158,7 +174,7 @@ namespace ErsatzTV.Core.Metadata
{
bool shouldUpdate = Optional(movie.MovieMetadata).Flatten().HeadOrNone().Match(
m => m.MetadataKind == MetadataKind.Fallback ||
m.DateUpdated < _localFileSystem.GetLastWriteTime(nfoFile),
m.DateUpdated != _localFileSystem.GetLastWriteTime(nfoFile),
true);
if (shouldUpdate)
@@ -20,6 +20,7 @@ namespace ErsatzTV.Core.Metadata
public class MusicVideoFolderScanner : LocalFolderScanner, IMusicVideoFolderScanner
{
private readonly IArtistRepository _artistRepository;
private readonly ILibraryRepository _libraryRepository;
private readonly ILocalFileSystem _localFileSystem;
private readonly ILocalMetadataProvider _localMetadataProvider;
private readonly ILogger<MusicVideoFolderScanner> _logger;
@@ -38,6 +39,7 @@ namespace ErsatzTV.Core.Metadata
ISearchRepository searchRepository,
IArtistRepository artistRepository,
IMusicVideoRepository musicVideoRepository,
ILibraryRepository libraryRepository,
IMediator mediator,
ILogger<MusicVideoFolderScanner> logger) : base(
localFileSystem,
@@ -52,6 +54,7 @@ namespace ErsatzTV.Core.Metadata
_searchRepository = searchRepository;
_artistRepository = artistRepository;
_musicVideoRepository = musicVideoRepository;
_libraryRepository = libraryRepository;
_mediator = mediator;
_logger = logger;
}
@@ -59,7 +62,6 @@ namespace ErsatzTV.Core.Metadata
public async Task<Either<BaseError, Unit>> ScanFolder(
LibraryPath libraryPath,
string ffprobePath,
DateTimeOffset lastScan,
decimal progressMin,
decimal progressMax)
{
@@ -96,9 +98,7 @@ namespace ErsatzTV.Core.Metadata
libraryPath,
ffprobePath,
result.Item,
artistFolder,
// force scanning all folders if we're adding a new artist
result.IsAdded ? DateTimeOffset.MinValue : lastScan);
artistFolder);
if (result.IsAdded)
{
@@ -167,7 +167,7 @@ namespace ErsatzTV.Core.Metadata
{
bool shouldUpdate = Optional(artist.ArtistMetadata).Flatten().HeadOrNone().Match(
m => m.MetadataKind == MetadataKind.Fallback ||
m.DateUpdated < _localFileSystem.GetLastWriteTime(nfoFile),
m.DateUpdated != _localFileSystem.GetLastWriteTime(nfoFile),
true);
if (shouldUpdate)
@@ -222,12 +222,11 @@ namespace ErsatzTV.Core.Metadata
}
}
public async Task ScanMusicVideos(
private async Task ScanMusicVideos(
LibraryPath libraryPath,
string ffprobePath,
Artist artist,
string artistFolder,
DateTimeOffset lastScan)
string artistFolder)
{
var folderQueue = new Queue<string>();
folderQueue.Enqueue(artistFolder);
@@ -247,7 +246,13 @@ namespace ErsatzTV.Core.Metadata
folderQueue.Enqueue(subdirectory);
}
if (_localFileSystem.GetLastWriteTime(musicVideoFolder) < lastScan)
string etag = FolderEtag.Calculate(musicVideoFolder, _localFileSystem);
Option<LibraryFolder> knownFolder = libraryPath.LibraryFolders
.Filter(f => f.Path == musicVideoFolder)
.HeadOrNone();
// skip folder if etag matches
if (await knownFolder.Map(f => f.Etag).IfNoneAsync(string.Empty) == etag)
{
continue;
}
@@ -272,6 +277,8 @@ namespace ErsatzTV.Core.Metadata
{
await _searchIndex.UpdateItems(_searchRepository, new List<MediaItem> { result.Item });
}
await _libraryRepository.SetEtag(libraryPath, knownFolder, musicVideoFolder, etag);
},
error =>
{
@@ -293,7 +300,7 @@ namespace ErsatzTV.Core.Metadata
{
bool shouldUpdate = Optional(musicVideo.MusicVideoMetadata).Flatten().HeadOrNone().Match(
m => m.MetadataKind == MetadataKind.Fallback ||
m.DateUpdated < _localFileSystem.GetLastWriteTime(nfoFile),
m.DateUpdated != _localFileSystem.GetLastWriteTime(nfoFile),
true);
if (shouldUpdate)
@@ -19,6 +19,7 @@ namespace ErsatzTV.Core.Metadata
{
public class TelevisionFolderScanner : LocalFolderScanner, ITelevisionFolderScanner
{
private readonly ILibraryRepository _libraryRepository;
private readonly ILocalFileSystem _localFileSystem;
private readonly ILocalMetadataProvider _localMetadataProvider;
private readonly ILogger<TelevisionFolderScanner> _logger;
@@ -36,6 +37,7 @@ namespace ErsatzTV.Core.Metadata
IImageCache imageCache,
ISearchIndex searchIndex,
ISearchRepository searchRepository,
ILibraryRepository libraryRepository,
IMediator mediator,
ILogger<TelevisionFolderScanner> logger) : base(
localFileSystem,
@@ -49,6 +51,7 @@ namespace ErsatzTV.Core.Metadata
_localMetadataProvider = localMetadataProvider;
_searchIndex = searchIndex;
_searchRepository = searchRepository;
_libraryRepository = libraryRepository;
_mediator = mediator;
_logger = logger;
}
@@ -56,7 +59,6 @@ namespace ErsatzTV.Core.Metadata
public async Task<Either<BaseError, Unit>> ScanFolder(
LibraryPath libraryPath,
string ffprobePath,
DateTimeOffset lastScan,
decimal progressMin,
decimal progressMax)
{
@@ -91,9 +93,7 @@ namespace ErsatzTV.Core.Metadata
libraryPath,
ffprobePath,
result.Item,
showFolder,
// force scanning all folders if we're adding a new show
result.IsAdded ? DateTimeOffset.MinValue : lastScan);
showFolder);
if (result.IsAdded)
{
@@ -146,12 +146,22 @@ namespace ErsatzTV.Core.Metadata
LibraryPath libraryPath,
string ffprobePath,
Show show,
string showFolder,
DateTimeOffset lastScan)
string showFolder)
{
foreach (string seasonFolder in _localFileSystem.ListSubdirectories(showFolder).Filter(ShouldIncludeFolder)
.OrderBy(identity))
{
string etag = FolderEtag.Calculate(seasonFolder, _localFileSystem);
Option<LibraryFolder> knownFolder = libraryPath.LibraryFolders
.Filter(f => f.Path == seasonFolder)
.HeadOrNone();
// skip folder if etag matches
if (await knownFolder.Map(f => f.Etag).IfNoneAsync(string.Empty) == etag)
{
continue;
}
Option<int> maybeSeasonNumber = SeasonNumberForFolder(seasonFolder);
await maybeSeasonNumber.IfSomeAsync(
async seasonNumber =>
@@ -161,7 +171,11 @@ namespace ErsatzTV.Core.Metadata
.BindT(season => UpdatePoster(season, seasonFolder));
await maybeSeason.Match(
season => ScanEpisodes(libraryPath, ffprobePath, season, seasonFolder, lastScan),
async season =>
{
await ScanEpisodes(libraryPath, ffprobePath, season, seasonFolder);
await _libraryRepository.SetEtag(libraryPath, knownFolder, seasonFolder, etag);
},
error =>
{
_logger.LogWarning(
@@ -180,14 +194,8 @@ namespace ErsatzTV.Core.Metadata
LibraryPath libraryPath,
string ffprobePath,
Season season,
string seasonPath,
DateTimeOffset lastScan)
string seasonPath)
{
if (_localFileSystem.GetLastWriteTime(seasonPath) < lastScan)
{
return Unit.Default;
}
foreach (string file in _localFileSystem.ListFiles(seasonPath)
.Filter(f => VideoFileExtensions.Contains(Path.GetExtension(f))).OrderBy(identity))
{
@@ -219,7 +227,7 @@ namespace ErsatzTV.Core.Metadata
{
bool shouldUpdate = Optional(show.ShowMetadata).Flatten().HeadOrNone().Match(
m => m.MetadataKind == MetadataKind.Fallback ||
m.DateUpdated < _localFileSystem.GetLastWriteTime(nfoFile),
m.DateUpdated != _localFileSystem.GetLastWriteTime(nfoFile),
true);
if (shouldUpdate)
@@ -261,7 +269,7 @@ namespace ErsatzTV.Core.Metadata
{
bool shouldUpdate = Optional(episode.EpisodeMetadata).Flatten().HeadOrNone().Match(
m => m.MetadataKind == MetadataKind.Fallback ||
m.DateUpdated < _localFileSystem.GetLastWriteTime(nfoFile),
m.DateUpdated != _localFileSystem.GetLastWriteTime(nfoFile),
true);
if (shouldUpdate)
@@ -70,7 +70,17 @@ namespace ErsatzTV.Core.Plex
await maybeShow.Match(
async result =>
{
await ScanSeasons(library, result.Item, connection, token);
if (result.IsAdded || incoming.ShowMetadata.Head().DateUpdated >
result.Item.ShowMetadata.Head().DateUpdated)
{
await ScanSeasons(library, result.Item, connection, token);
}
else
{
_logger.LogDebug(
"Skipping Plex show that has not been updated: {Show}",
incoming.ShowMetadata.Head().Title);
}
if (result.IsAdded)
{
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Interfaces.Scheduling;
@@ -16,7 +15,7 @@ namespace ErsatzTV.Core.Scheduling
IEnumerable<MediaItem> mediaItems,
CollectionEnumeratorState state)
{
_sortedMediaItems = mediaItems.OrderBy(identity, new ChronologicalComparer()).ToList();
_sortedMediaItems = mediaItems.OrderBy(identity, new ChronologicalMediaComparer()).ToList();
State = new CollectionEnumeratorState { Seed = state.Seed };
while (State.Index < state.Index)
@@ -30,85 +29,5 @@ namespace ErsatzTV.Core.Scheduling
public Option<MediaItem> Current => _sortedMediaItems.Any() ? _sortedMediaItems[State.Index] : None;
public void MoveNext() => State.Index = (State.Index + 1) % _sortedMediaItems.Count;
private class ChronologicalComparer : IComparer<MediaItem>
{
public int Compare(MediaItem x, MediaItem y)
{
if (x == null || y == null)
{
return 0;
}
DateTime date1 = x switch
{
Episode e => e.EpisodeMetadata.HeadOrNone().Match(
em => em.ReleaseDate ?? DateTime.MaxValue,
() => DateTime.MaxValue),
Movie m => m.MovieMetadata.HeadOrNone().Match(
mm => mm.ReleaseDate ?? DateTime.MaxValue,
() => DateTime.MaxValue),
MusicVideo mv => mv.MusicVideoMetadata.HeadOrNone().Match(
mvm => mvm.ReleaseDate ?? DateTime.MaxValue,
() => DateTime.MaxValue),
_ => DateTime.MaxValue
};
DateTime date2 = y switch
{
Episode e => e.EpisodeMetadata.HeadOrNone().Match(
em => em.ReleaseDate ?? DateTime.MaxValue,
() => DateTime.MaxValue),
Movie m => m.MovieMetadata.HeadOrNone().Match(
mm => mm.ReleaseDate ?? DateTime.MaxValue,
() => DateTime.MaxValue),
MusicVideo mv => mv.MusicVideoMetadata.HeadOrNone().Match(
mvm => mvm.ReleaseDate ?? DateTime.MaxValue,
() => DateTime.MaxValue),
_ => DateTime.MaxValue
};
if (date1 != date2)
{
return date1.CompareTo(date2);
}
int season1 = x switch
{
Episode e => e.Season?.SeasonNumber ?? int.MaxValue,
_ => int.MaxValue
};
int season2 = y switch
{
Episode e => e.Season?.SeasonNumber ?? int.MaxValue,
_ => int.MaxValue
};
if (season1 != season2)
{
return season1.CompareTo(season2);
}
int episode1 = x switch
{
Episode e => e.EpisodeNumber,
_ => int.MaxValue
};
int episode2 = y switch
{
Episode e => e.EpisodeNumber,
_ => int.MaxValue
};
if (episode1 != episode2)
{
return episode1.CompareTo(episode2);
}
return x.Id.CompareTo(y.Id);
}
}
}
}
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using ErsatzTV.Core.Domain;
namespace ErsatzTV.Core.Scheduling
{
internal class ChronologicalMediaComparer : IComparer<MediaItem>
{
public int Compare(MediaItem x, MediaItem y)
{
if (x == null || y == null)
{
return 0;
}
DateTime date1 = x switch
{
Episode e => e.EpisodeMetadata.HeadOrNone().Match(
em => em.ReleaseDate ?? DateTime.MaxValue,
() => DateTime.MaxValue),
Movie m => m.MovieMetadata.HeadOrNone().Match(
mm => mm.ReleaseDate ?? DateTime.MaxValue,
() => DateTime.MaxValue),
MusicVideo mv => mv.MusicVideoMetadata.HeadOrNone().Match(
mvm => mvm.ReleaseDate ?? DateTime.MaxValue,
() => DateTime.MaxValue),
_ => DateTime.MaxValue
};
DateTime date2 = y switch
{
Episode e => e.EpisodeMetadata.HeadOrNone().Match(
em => em.ReleaseDate ?? DateTime.MaxValue,
() => DateTime.MaxValue),
Movie m => m.MovieMetadata.HeadOrNone().Match(
mm => mm.ReleaseDate ?? DateTime.MaxValue,
() => DateTime.MaxValue),
MusicVideo mv => mv.MusicVideoMetadata.HeadOrNone().Match(
mvm => mvm.ReleaseDate ?? DateTime.MaxValue,
() => DateTime.MaxValue),
_ => DateTime.MaxValue
};
if (date1 != date2)
{
return date1.CompareTo(date2);
}
int season1 = x switch
{
Episode e => e.Season?.SeasonNumber ?? int.MaxValue,
_ => int.MaxValue
};
int season2 = y switch
{
Episode e => e.Season?.SeasonNumber ?? int.MaxValue,
_ => int.MaxValue
};
if (season1 != season2)
{
return season1.CompareTo(season2);
}
int episode1 = x switch
{
Episode e => e.EpisodeNumber,
_ => int.MaxValue
};
int episode2 = y switch
{
Episode e => e.EpisodeNumber,
_ => int.MaxValue
};
if (episode1 != episode2)
{
return episode1.CompareTo(episode2);
}
return x.Id.CompareTo(y.Id);
}
}
}
@@ -0,0 +1,9 @@
using System.Collections.Generic;
using System.Diagnostics;
using ErsatzTV.Core.Domain;
namespace ErsatzTV.Core.Scheduling
{
[DebuggerDisplay("{First}")]
public record GroupedMediaItem(MediaItem First, List<MediaItem> Additional);
}
@@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using ErsatzTV.Core.Domain;
using static LanguageExt.Prelude;
namespace ErsatzTV.Core.Scheduling
{
public static class MultiPartEpisodeGrouper
{
public static List<GroupedMediaItem> GroupMediaItems(IList<MediaItem> mediaItems)
{
var sortedMediaItems = mediaItems.OrderBy(identity, new ChronologicalMediaComparer()).ToList();
var groups = new List<GroupedMediaItem>();
GroupedMediaItem group = null;
var lastNumber = 0;
foreach (MediaItem item in sortedMediaItems)
{
if (item is Episode e)
{
const string PATTERN = @"^.*\((\d+)\)( - .*)?$";
Match match = Regex.Match(e.EpisodeMetadata.Head().Title, PATTERN);
if (match.Success)
{
var number = int.Parse(match.Groups[1].Value);
if (number <= lastNumber && group != null)
{
groups.Add(group);
group = null;
lastNumber = 0;
}
if (number == lastNumber + 1)
{
if (lastNumber == 0)
{
// start a new group
group = new GroupedMediaItem(item, null);
}
else if (group != null)
{
// add to current group
List<MediaItem> additional = group.Additional ?? new List<MediaItem>();
additional.Add(item);
group = group with { Additional = additional };
}
else
{
// this should never happen
throw new InvalidOperationException("Bad shuffle state");
}
lastNumber = number;
}
else
{
// this should never happen
throw new InvalidOperationException(
$"Bad shuffle state; unexpected number {number} after {lastNumber}");
}
}
else
{
if (group != null && lastNumber != 0)
{
groups.Add(group);
group = null;
lastNumber = 0;
}
groups.Add(new GroupedMediaItem(item, null));
}
}
else
{
groups.Add(new GroupedMediaItem(item, null));
}
}
if (group != null && lastNumber != 0)
{
groups.Add(group);
}
return groups;
}
public static IList<MediaItem> FlattenGroups(GroupedMediaItem[] copy, int mediaItemCount)
{
var result = new MediaItem[mediaItemCount];
var i = 0;
foreach (GroupedMediaItem group in copy)
{
result[i++] = group.First;
foreach (MediaItem additional in Optional(group.Additional).Flatten())
{
result[i++] = additional;
}
}
return result;
}
}
}
+4 -1
View File
@@ -468,7 +468,10 @@ namespace ErsatzTV.Core.Scheduling
case PlaybackOrder.Random:
return new RandomizedMediaCollectionEnumerator(mediaItems, state);
case PlaybackOrder.Shuffle:
return new ShuffledMediaCollectionEnumerator(mediaItems, state);
return new ShuffledMediaCollectionEnumerator(
mediaItems,
state,
playout.ProgramSchedule.KeepMultiPartEpisodesTogether);
default:
// TODO: handle this error case differently?
return new RandomizedMediaCollectionEnumerator(mediaItems, state);
@@ -10,14 +10,22 @@ namespace ErsatzTV.Core.Scheduling
{
public class ShuffledMediaCollectionEnumerator : IMediaCollectionEnumerator
{
private readonly IList<MediaItem> _mediaItems;
private readonly int _mediaItemCount;
private readonly IList<GroupedMediaItem> _mediaItems;
private Random _random;
private IList<MediaItem> _shuffled;
public ShuffledMediaCollectionEnumerator(IList<MediaItem> mediaItems, CollectionEnumeratorState state)
public ShuffledMediaCollectionEnumerator(
IList<MediaItem> mediaItems,
CollectionEnumeratorState state,
bool keepMultiPartEpisodesTogether)
{
_mediaItems = mediaItems;
_mediaItemCount = mediaItems.Count;
_mediaItems = keepMultiPartEpisodesTogether
? MultiPartEpisodeGrouper.GroupMediaItems(mediaItems)
: mediaItems.Map(mi => new GroupedMediaItem(mi, null)).ToList();
_random = new Random(state.Seed);
_shuffled = Shuffle(_mediaItems, _random);
@@ -30,7 +38,7 @@ namespace ErsatzTV.Core.Scheduling
public CollectionEnumeratorState State { get; }
public Option<MediaItem> Current => _shuffled.Any() ? _shuffled[State.Index % _mediaItems.Count] : None;
public Option<MediaItem> Current => _shuffled.Any() ? _shuffled[State.Index % _mediaItemCount] : None;
public void MoveNext()
{
@@ -54,21 +62,21 @@ namespace ErsatzTV.Core.Scheduling
State.Index %= _shuffled.Count;
}
private static IList<T> Shuffle<T>(IEnumerable<T> list, Random random)
private IList<MediaItem> Shuffle(IEnumerable<GroupedMediaItem> list, Random random)
{
T[] copy = list.ToArray();
GroupedMediaItem[] copy = list.ToArray();
int n = copy.Length;
while (n > 1)
{
n--;
int k = random.Next(n + 1);
T value = copy[k];
GroupedMediaItem value = copy[k];
copy[k] = copy[n];
copy[n] = value;
}
return copy;
return MultiPartEpisodeGrouper.FlattenGroups(copy, _mediaItemCount);
}
}
}
+7
View File
@@ -0,0 +1,7 @@
namespace ErsatzTV.Core
{
public static class Settings
{
public static int ListenPort { get; set; }
}
}
@@ -0,0 +1,11 @@
using ErsatzTV.Core.Domain;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace ErsatzTV.Infrastructure.Data.Configurations
{
public class LibraryFolderConfiguration : IEntityTypeConfiguration<LibraryFolder>
{
public void Configure(EntityTypeBuilder<LibraryFolder> builder) => builder.ToTable("LibraryFolder");
}
}
@@ -14,6 +14,11 @@ namespace ErsatzTV.Infrastructure.Data.Configurations
.WithOne(i => i.LibraryPath)
.HasForeignKey(i => i.LibraryPathId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasMany(p => p.LibraryFolders)
.WithOne(f => f.LibraryPath)
.HasForeignKey(f => f.LibraryPathId)
.OnDelete(DeleteBehavior.Cascade);
}
}
}
@@ -35,6 +35,7 @@ namespace ErsatzTV.Infrastructure.Data.Repositories
using TvContext context = _dbContextFactory.CreateDbContext();
return context.Libraries
.Include(l => l.Paths)
.ThenInclude(p => p.LibraryFolders)
.OrderBy(l => l.Id)
.SingleOrDefaultAsync(l => l.Id == libraryId)
.Map(Optional);
@@ -104,5 +105,30 @@ namespace ErsatzTV.Infrastructure.Data.Repositories
context.LibraryPaths.Remove(libraryPath);
await context.SaveChangesAsync();
}
public Task<Unit> SetEtag(
LibraryPath libraryPath,
Option<LibraryFolder> knownFolder,
string path,
string etag) =>
knownFolder.Match(
async folder =>
{
await _dbConnection.ExecuteAsync(
"UPDATE LibraryFolder SET Etag = @Etag WHERE Id = @Id",
new { folder.Id, Etag = etag });
},
async () =>
{
await using TvContext context = _dbContextFactory.CreateDbContext();
await context.LibraryFolders.AddAsync(
new LibraryFolder
{
Path = path,
Etag = etag,
LibraryPathId = libraryPath.Id
});
await context.SaveChangesAsync();
}).ToUnit();
}
}
@@ -21,6 +21,7 @@ namespace ErsatzTV.Infrastructure.Data
public DbSet<Library> Libraries { get; set; }
public DbSet<LocalLibrary> LocalLibraries { get; set; }
public DbSet<LibraryPath> LibraryPaths { get; set; }
public DbSet<LibraryFolder> LibraryFolders { get; set; }
public DbSet<PlexLibrary> PlexLibraries { get; set; }
public DbSet<JellyfinLibrary> JellyfinLibraries { get; set; }
public DbSet<PlexPathReplacement> PlexPathReplacements { get; set; }
@@ -196,7 +196,7 @@ namespace ErsatzTV.Infrastructure.Jellyfin
}
private static Option<JellyfinLibrary> Project(JellyfinLibraryResponse response) =>
response.CollectionType.ToLowerInvariant() switch
response.CollectionType?.ToLowerInvariant() switch
{
"tvshows" => new JellyfinLibrary
{
@@ -0,0 +1,20 @@
using Microsoft.EntityFrameworkCore.Migrations;
namespace ErsatzTV.Infrastructure.Migrations
{
public partial class Add_ProgramSchedule_KeepMultiPartEpisodesTogether : Migration
{
protected override void Up(MigrationBuilder migrationBuilder) =>
migrationBuilder.AddColumn<bool>(
"KeepMultiPartEpisodesTogether",
"ProgramSchedule",
"INTEGER",
nullable: false,
defaultValue: false);
protected override void Down(MigrationBuilder migrationBuilder) =>
migrationBuilder.DropColumn(
"KeepMultiPartEpisodesTogether",
"ProgramSchedule");
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore.Migrations;
namespace ErsatzTV.Infrastructure.Migrations
{
public partial class Add_LibraryFolder : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
"LibraryFolder",
table => new
{
Id = table.Column<int>("INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Path = table.Column<string>("TEXT", nullable: true),
LibraryPathId = table.Column<int>("INTEGER", nullable: false),
Etag = table.Column<string>("TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_LibraryFolder", x => x.Id);
table.ForeignKey(
"FK_LibraryFolder_LibraryPath_LibraryPathId",
x => x.LibraryPathId,
"LibraryPath",
"Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
"IX_LibraryFolder_LibraryPathId",
"LibraryFolder",
"LibraryPathId");
}
protected override void Down(MigrationBuilder migrationBuilder) =>
migrationBuilder.DropTable(
"LibraryFolder");
}
}
@@ -517,6 +517,30 @@ namespace ErsatzTV.Infrastructure.Migrations
b.ToTable("Library");
});
modelBuilder.Entity(
"ErsatzTV.Core.Domain.LibraryFolder",
b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Etag")
.HasColumnType("TEXT");
b.Property<int>("LibraryPathId")
.HasColumnType("INTEGER");
b.Property<string>("Path")
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("LibraryPathId");
b.ToTable("LibraryFolder");
});
modelBuilder.Entity(
"ErsatzTV.Core.Domain.LibraryPath",
b =>
@@ -969,6 +993,9 @@ namespace ErsatzTV.Infrastructure.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<bool>("KeepMultiPartEpisodesTogether")
.HasColumnType("INTEGER");
b.Property<int>("MediaCollectionPlaybackOrder")
.HasColumnType("INTEGER");
@@ -1808,6 +1835,19 @@ namespace ErsatzTV.Infrastructure.Migrations
b.Navigation("MediaSource");
});
modelBuilder.Entity(
"ErsatzTV.Core.Domain.LibraryFolder",
b =>
{
b.HasOne("ErsatzTV.Core.Domain.LibraryPath", "LibraryPath")
.WithMany("LibraryFolders")
.HasForeignKey("LibraryPathId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("LibraryPath");
});
modelBuilder.Entity(
"ErsatzTV.Core.Domain.LibraryPath",
b =>
@@ -2556,7 +2596,14 @@ namespace ErsatzTV.Infrastructure.Migrations
modelBuilder.Entity("ErsatzTV.Core.Domain.Library", b => { b.Navigation("Paths"); });
modelBuilder.Entity("ErsatzTV.Core.Domain.LibraryPath", b => { b.Navigation("MediaItems"); });
modelBuilder.Entity(
"ErsatzTV.Core.Domain.LibraryPath",
b =>
{
b.Navigation("LibraryFolders");
b.Navigation("MediaItems");
});
modelBuilder.Entity("ErsatzTV.Core.Domain.MediaItem", b => { b.Navigation("CollectionItems"); });
+13 -7
View File
@@ -55,27 +55,33 @@ namespace ErsatzTV.Infrastructure.Search
private readonly ILogger<SearchIndex> _logger;
private FSDirectory _directory;
private bool _initialized;
private IndexWriter _writer;
public SearchIndex(ILogger<SearchIndex> logger)
{
_logger = logger;
_cultureInfos = CultureInfo.GetCultures(CultureTypes.NeutralCultures).ToList();
_initialized = false;
}
public int Version => 9;
public Task<bool> Initialize(ILocalFileSystem localFileSystem)
{
localFileSystem.EnsureFolderExists(FileSystemLayout.SearchIndexFolder);
if (!_initialized)
{
localFileSystem.EnsureFolderExists(FileSystemLayout.SearchIndexFolder);
_directory = FSDirectory.Open(FileSystemLayout.SearchIndexFolder);
var analyzer = new StandardAnalyzer(AppLuceneVersion);
var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer)
{ OpenMode = OpenMode.CREATE_OR_APPEND };
_writer = new IndexWriter(_directory, indexConfig);
_directory = FSDirectory.Open(FileSystemLayout.SearchIndexFolder);
var analyzer = new StandardAnalyzer(AppLuceneVersion);
var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer)
{ OpenMode = OpenMode.CREATE_OR_APPEND };
_writer = new IndexWriter(_directory, indexConfig);
_initialized = true;
}
return Task.FromResult(true);
return Task.FromResult(_initialized);
}
public async Task<Unit> Rebuild(ISearchRepository searchRepository, List<int> itemIds)
+7
View File
@@ -22,6 +22,12 @@
<MudSelectItem Value="@playbackOrder">@playbackOrder</MudSelectItem>
}
</MudSelect>
<MudElement HtmlTag="div" Class="mt-3">
<MudCheckBox Label="Keep Multi-Part Episodes Together"
@bind-Checked="@_model.KeepMultiPartEpisodesTogether"
Disabled="@(_model.MediaCollectionPlaybackOrder != PlaybackOrder.Shuffle)"
For="@(() => _model.KeepMultiPartEpisodesTogether)"/>
</MudElement>
</MudCardContent>
<MudCardActions>
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary">
@@ -53,6 +59,7 @@
_model.Id = viewModel.Id;
_model.Name = viewModel.Name;
_model.MediaCollectionPlaybackOrder = viewModel.MediaCollectionPlaybackOrder;
_model.KeepMultiPartEpisodesTogether = viewModel.KeepMultiPartEpisodesTogether;
},
() => NavigationManager.NavigateTo("404"));
}
+1 -1
View File
@@ -48,7 +48,7 @@ namespace ErsatzTV
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(
webBuilder => webBuilder.UseStartup<Startup>()
.UseUrls("http://+:8409")
.UseConfiguration(Configuration)
.UseKestrel(options => options.AddServerHeader = false))
.UseSerilog();
}
-1
View File
@@ -3,7 +3,6 @@
"ErsatzTV": {
"commandName": "Project",
"launchBrowser": false,
"applicationUrl": "http://0.0.0.0:8409",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
@@ -0,0 +1,63 @@
using System;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using ErsatzTV.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace ErsatzTV.Services.RunOnce
{
public class EndpointValidatorService : IHostedService
{
private readonly IConfiguration _configuration;
private readonly ILogger<EndpointValidatorService> _logger;
public EndpointValidatorService(IConfiguration configuration, ILogger<EndpointValidatorService> logger)
{
_configuration = configuration;
_logger = logger;
}
public Task StartAsync(CancellationToken cancellationToken)
{
string urls = _configuration.GetValue<string>("Kestrel:Endpoints:Http:Url");
if (urls.Split(";").Length > 1)
{
throw new NotSupportedException($"Multiple endpoints are not supported: {urls}");
}
const string PATTERN = @"http:\/\/(.*):(\d+)";
Match match = Regex.Match(urls, PATTERN);
if (match.Success)
{
string hostname = match.Groups[1].Value;
Settings.ListenPort = int.Parse(match.Groups[2].Value);
// IP address must be 0.0.0.0 or 127.0.0.1
if (IPAddress.TryParse(hostname, out IPAddress address))
{
if (!address.Equals(IPAddress.Parse("0.0.0.0")) && !IPAddress.IsLoopback(address))
{
throw new NotSupportedException($"Endpoint MUST include loopback: {urls}");
}
}
}
else
{
throw new NotSupportedException($"Invalid endpoint format: {urls}");
}
_logger.LogInformation(
"Server will listen on port {Port} - try UI at {UI}",
Settings.ListenPort,
$"http://localhost:{Settings.ListenPort}");
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
}
+1 -5
View File
@@ -110,11 +110,6 @@ namespace ErsatzTV
"https://github.com/jasongdove/ErsatzTV",
"https://discord.gg/hHaJm3yGy6");
Log.Logger.Information(
"Server will listen on port {Port} - try UI at {UI}",
8409,
"http://localhost:8409");
if (!Directory.Exists(FileSystemLayout.AppDataFolder))
{
Directory.CreateDirectory(FileSystemLayout.AppDataFolder);
@@ -247,6 +242,7 @@ namespace ErsatzTV
});
services.AddScoped<IJellyfinSecretStore, JellyfinSecretStore>();
services.AddHostedService<EndpointValidatorService>();
services.AddHostedService<DatabaseMigratorService>();
services.AddHostedService<CacheCleanerService>();
services.AddHostedService<JellyfinService>();
@@ -8,7 +8,12 @@ namespace ErsatzTV.ViewModels
public int Id { get; set; }
public string Name { get; set; }
public PlaybackOrder MediaCollectionPlaybackOrder { get; set; }
public UpdateProgramSchedule ToUpdate() => new(Id, Name, MediaCollectionPlaybackOrder);
public CreateProgramSchedule ToCreate() => new(Name, MediaCollectionPlaybackOrder);
public bool KeepMultiPartEpisodesTogether { get; set; }
public UpdateProgramSchedule ToUpdate() =>
new(Id, Name, MediaCollectionPlaybackOrder, KeepMultiPartEpisodesTogether);
public CreateProgramSchedule ToCreate() =>
new(Name, MediaCollectionPlaybackOrder, KeepMultiPartEpisodesTogether);
}
}
+8 -1
View File
@@ -24,5 +24,12 @@
"WithThreadId"
]
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://+:8409"
}
}
}
}