update media server scanning and paging (#1770)

* update media server scanning and paging

* remove unused types
This commit is contained in:
Jason Dove
2024-07-02 13:12:09 -05:00
committed by GitHub
parent f62e841af4
commit f41fa669be
23 changed files with 117 additions and 529 deletions
+2
View File
@@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [Unreleased]
### Changed
- Remove some unnecessary API calls related to media server scanning and paging
## [0.8.7-beta] - 2024-06-26
### Added
-11
View File
@@ -1,11 +0,0 @@
namespace ErsatzTV.Core.Emby;
public static class EmbyItemType
{
public static readonly string Movie = "Movie";
public static readonly string Show = "Series";
public static readonly string Season = "Season";
public static readonly string Episode = "Episode";
public static readonly string Collection = "BoxSet";
public static readonly string CollectionItems = "Movie,Series,Season,Episode";
}
@@ -8,32 +8,26 @@ public interface IEmbyApiClient
Task<Either<BaseError, EmbyServerInformation>> GetServerInformation(string address, string apiKey);
Task<Either<BaseError, List<EmbyLibrary>>> GetLibraries(string address, string apiKey);
IAsyncEnumerable<EmbyMovie> GetMovieLibraryItems(string address, string apiKey, EmbyLibrary library);
IAsyncEnumerable<Tuple<EmbyMovie, int>> GetMovieLibraryItems(string address, string apiKey, EmbyLibrary library);
IAsyncEnumerable<EmbyShow> GetShowLibraryItems(string address, string apiKey, EmbyLibrary library);
IAsyncEnumerable<Tuple<EmbyShow, int>> GetShowLibraryItems(string address, string apiKey, EmbyLibrary library);
IAsyncEnumerable<EmbySeason> GetSeasonLibraryItems(
IAsyncEnumerable<Tuple<EmbySeason, int>> GetSeasonLibraryItems(
string address,
string apiKey,
EmbyLibrary library,
string showId);
IAsyncEnumerable<EmbyEpisode> GetEpisodeLibraryItems(
IAsyncEnumerable<Tuple<EmbyEpisode, int>> GetEpisodeLibraryItems(
string address,
string apiKey,
EmbyLibrary library,
string showId,
string seasonId);
IAsyncEnumerable<EmbyCollection> GetCollectionLibraryItems(string address, string apiKey);
IAsyncEnumerable<Tuple<EmbyCollection, int>> GetCollectionLibraryItems(string address, string apiKey);
IAsyncEnumerable<MediaItem> GetCollectionItems(string address, string apiKey, string collectionId);
Task<Either<BaseError, int>> GetLibraryItemCount(
string address,
string apiKey,
string parentId,
string includeItemTypes);
IAsyncEnumerable<Tuple<MediaItem, int>> GetCollectionItems(string address, string apiKey, string collectionId);
Task<Either<BaseError, MediaVersion>> GetPlaybackInfo(
string address,
@@ -9,38 +9,30 @@ public interface IJellyfinApiClient
Task<Either<BaseError, List<JellyfinLibrary>>> GetLibraries(string address, string apiKey);
Task<Either<BaseError, string>> GetAdminUserId(string address, string apiKey);
IAsyncEnumerable<JellyfinMovie> GetMovieLibraryItems(string address, string apiKey, JellyfinLibrary library);
IAsyncEnumerable<Tuple<JellyfinMovie, int>> GetMovieLibraryItems(string address, string apiKey, JellyfinLibrary library);
IAsyncEnumerable<JellyfinShow> GetShowLibraryItems(string address, string apiKey, JellyfinLibrary library);
IAsyncEnumerable<Tuple<JellyfinShow, int>> GetShowLibraryItems(string address, string apiKey, JellyfinLibrary library);
IAsyncEnumerable<JellyfinSeason> GetSeasonLibraryItems(
IAsyncEnumerable<Tuple<JellyfinSeason, int>> GetSeasonLibraryItems(
string address,
string apiKey,
JellyfinLibrary library,
string showId);
IAsyncEnumerable<JellyfinEpisode> GetEpisodeLibraryItems(
IAsyncEnumerable<Tuple<JellyfinEpisode, int>> GetEpisodeLibraryItems(
string address,
string apiKey,
JellyfinLibrary library,
string seasonId);
IAsyncEnumerable<JellyfinCollection> GetCollectionLibraryItems(string address, string apiKey, int mediaSourceId);
IAsyncEnumerable<Tuple<JellyfinCollection, int>> GetCollectionLibraryItems(string address, string apiKey, int mediaSourceId);
IAsyncEnumerable<MediaItem> GetCollectionItems(
IAsyncEnumerable<Tuple<MediaItem, int>> GetCollectionItems(
string address,
string apiKey,
int mediaSourceId,
string collectionId);
Task<Either<BaseError, int>> GetLibraryItemCount(
string address,
string apiKey,
JellyfinLibrary library,
string parentId,
string includeItemTypes,
bool excludeFolders);
Task<Either<BaseError, MediaVersion>> GetPlaybackInfo(
string address,
string apiKey,
@@ -13,33 +13,23 @@ public interface IPlexServerApiClient
PlexConnection connection,
PlexServerAuthToken token);
IAsyncEnumerable<PlexMovie> GetMovieLibraryContents(
IAsyncEnumerable<Tuple<PlexMovie, int>> GetMovieLibraryContents(
PlexLibrary library,
PlexConnection connection,
PlexServerAuthToken token);
IAsyncEnumerable<PlexShow> GetShowLibraryContents(
IAsyncEnumerable<Tuple<PlexShow, int>> GetShowLibraryContents(
PlexLibrary library,
PlexConnection connection,
PlexServerAuthToken token);
Task<Either<BaseError, int>> CountShowSeasons(
PlexShow show,
PlexConnection connection,
PlexServerAuthToken token);
IAsyncEnumerable<PlexSeason> GetShowSeasons(
IAsyncEnumerable<Tuple<PlexSeason, int>> GetShowSeasons(
PlexLibrary library,
PlexShow show,
PlexConnection connection,
PlexServerAuthToken token);
Task<Either<BaseError, int>> CountSeasonEpisodes(
PlexSeason season,
PlexConnection connection,
PlexServerAuthToken token);
IAsyncEnumerable<PlexEpisode> GetSeasonEpisodes(
IAsyncEnumerable<Tuple<PlexEpisode, int>> GetSeasonEpisodes(
PlexLibrary library,
PlexSeason season,
PlexConnection connection,
@@ -63,17 +53,12 @@ public interface IPlexServerApiClient
PlexConnection connection,
PlexServerAuthToken token);
Task<Either<BaseError, int>> GetLibraryItemCount(
PlexLibrary library,
PlexConnection connection,
PlexServerAuthToken token);
IAsyncEnumerable<PlexCollection> GetAllCollections(
IAsyncEnumerable<Tuple<PlexCollection, int>> GetAllCollections(
PlexConnection connection,
PlexServerAuthToken token,
CancellationToken cancellationToken);
IAsyncEnumerable<MediaItem> GetCollectionItems(
IAsyncEnumerable<Tuple<MediaItem, int>> GetCollectionItems(
PlexConnection connection,
PlexServerAuthToken token,
string key,
@@ -1,11 +0,0 @@
namespace ErsatzTV.Core.Jellyfin;
public static class JellyfinItemType
{
public static readonly string Movie = "Movie";
public static readonly string Show = "Series";
public static readonly string Season = "Season";
public static readonly string Episode = "Episode";
public static readonly string Collection = "BoxSet";
public static readonly string CollectionItems = "Movie,Series,Season,Episode";
}
+15 -51
View File
@@ -69,13 +69,11 @@ public class EmbyApiClient : IEmbyApiClient
}
}
public IAsyncEnumerable<EmbyMovie> GetMovieLibraryItems(string address, string apiKey, EmbyLibrary library)
public IAsyncEnumerable<Tuple<EmbyMovie, int>> GetMovieLibraryItems(string address, string apiKey, EmbyLibrary library)
=> GetPagedLibraryContents(
address,
apiKey,
library,
library.ItemId,
EmbyItemType.Movie,
(service, itemId, skip, pageSize) => service.GetMovieLibraryItems(
apiKey,
itemId,
@@ -83,13 +81,11 @@ public class EmbyApiClient : IEmbyApiClient
limit: pageSize),
(maybeLibrary, item) => maybeLibrary.Map(lib => ProjectToMovie(lib, item)).Flatten());
public IAsyncEnumerable<EmbyShow> GetShowLibraryItems(string address, string apiKey, EmbyLibrary library)
public IAsyncEnumerable<Tuple<EmbyShow, int>> GetShowLibraryItems(string address, string apiKey, EmbyLibrary library)
=> GetPagedLibraryContents(
address,
apiKey,
library,
library.ItemId,
EmbyItemType.Show,
(service, itemId, skip, pageSize) => service.GetShowLibraryItems(
apiKey,
itemId,
@@ -97,16 +93,14 @@ public class EmbyApiClient : IEmbyApiClient
limit: pageSize),
(_, item) => ProjectToShow(item));
public IAsyncEnumerable<EmbySeason> GetSeasonLibraryItems(
public IAsyncEnumerable<Tuple<EmbySeason, int>> GetSeasonLibraryItems(
string address,
string apiKey,
EmbyLibrary library,
string showId) => GetPagedLibraryContents(
address,
apiKey,
library,
showId,
EmbyItemType.Season,
(service, itemId, skip, pageSize) => service.GetSeasonLibraryItems(
apiKey,
itemId,
@@ -114,17 +108,15 @@ public class EmbyApiClient : IEmbyApiClient
limit: pageSize),
(_, item) => ProjectToSeason(item));
public IAsyncEnumerable<EmbyEpisode> GetEpisodeLibraryItems(
public IAsyncEnumerable<Tuple<EmbyEpisode, int>> GetEpisodeLibraryItems(
string address,
string apiKey,
EmbyLibrary library,
string showId,
string seasonId) => GetPagedLibraryContents(
address,
apiKey,
library,
seasonId,
EmbyItemType.Episode,
(service, _, skip, pageSize) => service.GetEpisodeLibraryItems(
apiKey,
showId,
@@ -133,7 +125,7 @@ public class EmbyApiClient : IEmbyApiClient
limit: pageSize),
(maybeLibrary, item) => maybeLibrary.Map(lib => ProjectToEpisode(lib, item)).Flatten());
public IAsyncEnumerable<EmbyCollection> GetCollectionLibraryItems(string address, string apiKey)
public IAsyncEnumerable<Tuple<EmbyCollection, int>> GetCollectionLibraryItems(string address, string apiKey)
{
// TODO: should we enumerate collection libraries here?
@@ -141,10 +133,8 @@ public class EmbyApiClient : IEmbyApiClient
{
return GetPagedLibraryContents(
address,
apiKey,
None,
itemId,
EmbyItemType.Collection,
(service, _, skip, pageSize) => service.GetCollectionLibraryItems(
apiKey,
itemId,
@@ -153,19 +143,17 @@ public class EmbyApiClient : IEmbyApiClient
(_, item) => ProjectToCollection(item));
}
return AsyncEnumerable.Empty<EmbyCollection>();
return AsyncEnumerable.Empty<Tuple<EmbyCollection, int>>();
}
public IAsyncEnumerable<MediaItem> GetCollectionItems(
public IAsyncEnumerable<Tuple<MediaItem, int>> GetCollectionItems(
string address,
string apiKey,
string collectionId) =>
GetPagedLibraryContents(
address,
apiKey,
None,
collectionId,
EmbyItemType.CollectionItems,
(service, _, skip, pageSize) => service.GetCollectionItems(
apiKey,
collectionId,
@@ -173,25 +161,6 @@ public class EmbyApiClient : IEmbyApiClient
limit: pageSize),
(_, item) => ProjectToCollectionMediaItem(item));
public async Task<Either<BaseError, int>> GetLibraryItemCount(
string address,
string apiKey,
string parentId,
string includeItemTypes)
{
try
{
IEmbyApi service = RestService.For<IEmbyApi>(address);
EmbyLibraryItemsResponse items = await service.GetLibraryStats(apiKey, parentId, includeItemTypes);
return items.TotalRecordCount;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting Emby library item count");
return BaseError.New(ex.Message);
}
}
public async Task<Either<BaseError, MediaVersion>> GetPlaybackInfo(
string address,
string apiKey,
@@ -212,36 +181,31 @@ public class EmbyApiClient : IEmbyApiClient
}
}
private static async IAsyncEnumerable<TItem> GetPagedLibraryContents<TItem>(
private static async IAsyncEnumerable<Tuple<TItem, int>> GetPagedLibraryContents<TItem>(
string address,
string apiKey,
Option<EmbyLibrary> maybeLibrary,
string parentId,
string itemType,
Func<IEmbyApi, string, int, int, Task<EmbyLibraryItemsResponse>> getItems,
Func<Option<EmbyLibrary>, EmbyLibraryItemResponse, Option<TItem>> mapper)
{
IEmbyApi service = RestService.For<IEmbyApi>(address);
int size = await service
.GetLibraryStats(apiKey, parentId, itemType)
.Map(r => r.TotalRecordCount);
const int PAGE_SIZE = 10;
int pages = (size - 1) / PAGE_SIZE + 1;
int pages = int.MaxValue;
for (var i = 0; i < pages; i++)
{
int skip = i * PAGE_SIZE;
Task<IEnumerable<TItem>> result = getItems(service, parentId, skip, PAGE_SIZE)
.Map(items => items.Items.Map(item => mapper(maybeLibrary, item)).Somes());
EmbyLibraryItemsResponse result = await getItems(service, parentId, skip, PAGE_SIZE);
// update page count
pages = Math.Min(pages, (result.TotalRecordCount - 1) / PAGE_SIZE + 1);
#pragma warning disable VSTHRD003
foreach (TItem item in await result)
foreach (TItem item in result.Items.Map(item => mapper(maybeLibrary, item)).Somes())
#pragma warning restore VSTHRD003
{
yield return item;
yield return new Tuple<TItem, int>(item, result.TotalRecordCount);
}
}
}
-15
View File
@@ -17,21 +17,6 @@ public interface IEmbyApi
[Header("X-Emby-Token")]
string apiKey);
[Get("/Items")]
public Task<EmbyLibraryItemsResponse> GetLibraryStats(
[Header("X-Emby-Token")]
string apiKey,
[Query]
string parentId,
[Query]
string includeItemTypes,
[Query]
bool recursive = true,
[Query]
int startIndex = 0,
[Query]
int limit = 0);
[Get("/Items?sortOrder=Ascending&sortBy=SortName")]
public Task<EmbyLibraryItemsResponse> GetMovieLibraryItems(
[Header("X-Emby-Token")]
@@ -0,0 +1,8 @@
namespace ErsatzTV.Infrastructure.Emby.Models;
public class EmbyItemsCountsResponse
{
public int MovieCount { get; set; }
public int SeriesCount { get; set; }
public int EpisodeCount { get; set; }
}
@@ -22,25 +22,6 @@ public interface IJellyfinApi
[Header("X-Emby-Token")]
string apiKey);
[Get("/Items")]
public Task<JellyfinLibraryItemsResponse> GetLibraryStats(
[Header("X-Emby-Token")]
string apiKey,
[Query]
string userId,
[Query]
string parentId,
[Query]
string includeItemTypes,
[Query]
bool recursive = true,
[Query]
string filters = "IsNotFolder",
[Query]
int startIndex = 0,
[Query]
int limit = 0);
[Get("/Items?sortOrder=Ascending&sortBy=SortName")]
public Task<JellyfinLibraryItemsResponse> GetMovieLibraryItems(
[Header("X-Emby-Token")]
@@ -93,17 +93,15 @@ public class JellyfinApiClient : IJellyfinApiClient
}
}
public IAsyncEnumerable<JellyfinMovie> GetMovieLibraryItems(
public IAsyncEnumerable<Tuple<JellyfinMovie, int>> GetMovieLibraryItems(
string address,
string apiKey,
JellyfinLibrary library) =>
GetPagedLibraryItems(
address,
apiKey,
library,
library.MediaSourceId,
library.ItemId,
JellyfinItemType.Movie,
(service, userId, itemId, skip, pageSize) => service.GetMovieLibraryItems(
apiKey,
userId,
@@ -112,17 +110,15 @@ public class JellyfinApiClient : IJellyfinApiClient
limit: pageSize),
(maybeLibrary, item) => maybeLibrary.Map(lib => ProjectToMovie(lib, item)).Flatten());
public IAsyncEnumerable<JellyfinShow> GetShowLibraryItems(
public IAsyncEnumerable<Tuple<JellyfinShow, int>> GetShowLibraryItems(
string address,
string apiKey,
JellyfinLibrary library) =>
GetPagedLibraryItems(
address,
apiKey,
library,
library.MediaSourceId,
library.ItemId,
JellyfinItemType.Show,
(service, userId, itemId, skip, pageSize) => service.GetShowLibraryItems(
apiKey,
userId,
@@ -131,18 +127,16 @@ public class JellyfinApiClient : IJellyfinApiClient
limit: pageSize),
(_, item) => ProjectToShow(item));
public IAsyncEnumerable<JellyfinSeason> GetSeasonLibraryItems(
public IAsyncEnumerable<Tuple<JellyfinSeason, int>> GetSeasonLibraryItems(
string address,
string apiKey,
JellyfinLibrary library,
string showId) =>
GetPagedLibraryItems(
address,
apiKey,
library,
library.MediaSourceId,
showId,
JellyfinItemType.Season,
(service, userId, _, skip, pageSize) => service.GetSeasonLibraryItems(
apiKey,
userId,
@@ -151,18 +145,16 @@ public class JellyfinApiClient : IJellyfinApiClient
limit: pageSize),
(_, item) => ProjectToSeason(item));
public IAsyncEnumerable<JellyfinEpisode> GetEpisodeLibraryItems(
public IAsyncEnumerable<Tuple<JellyfinEpisode, int>> GetEpisodeLibraryItems(
string address,
string apiKey,
JellyfinLibrary library,
string seasonId) =>
GetPagedLibraryItems(
address,
apiKey,
library,
library.MediaSourceId,
seasonId,
JellyfinItemType.Episode,
(service, userId, _, skip, pageSize) => service.GetEpisodeLibraryItems(
apiKey,
userId,
@@ -171,7 +163,7 @@ public class JellyfinApiClient : IJellyfinApiClient
limit: pageSize),
(maybeLibrary, item) => maybeLibrary.Map(lib => ProjectToEpisode(lib, item)).Flatten());
public IAsyncEnumerable<JellyfinCollection> GetCollectionLibraryItems(
public IAsyncEnumerable<Tuple<JellyfinCollection, int>> GetCollectionLibraryItems(
string address,
string apiKey,
int mediaSourceId)
@@ -182,11 +174,9 @@ public class JellyfinApiClient : IJellyfinApiClient
{
return GetPagedLibraryItems(
address,
apiKey,
None,
mediaSourceId,
itemId,
JellyfinItemType.Collection,
(service, userId, _, skip, pageSize) => service.GetCollectionLibraryItems(
apiKey,
userId,
@@ -196,21 +186,19 @@ public class JellyfinApiClient : IJellyfinApiClient
(_, item) => ProjectToCollection(item));
}
return AsyncEnumerable.Empty<JellyfinCollection>();
return AsyncEnumerable.Empty<Tuple<JellyfinCollection, int>>();
}
public IAsyncEnumerable<MediaItem> GetCollectionItems(
public IAsyncEnumerable<Tuple<MediaItem, int>> GetCollectionItems(
string address,
string apiKey,
int mediaSourceId,
string collectionId) =>
GetPagedLibraryItems(
address,
apiKey,
None,
mediaSourceId,
collectionId,
JellyfinItemType.CollectionItems,
(service, userId, _, skip, pageSize) => service.GetCollectionItems(
apiKey,
userId,
@@ -219,37 +207,6 @@ public class JellyfinApiClient : IJellyfinApiClient
limit: pageSize),
(_, item) => ProjectToCollectionMediaItem(item));
public async Task<Either<BaseError, int>> GetLibraryItemCount(
string address,
string apiKey,
JellyfinLibrary library,
string parentId,
string includeItemTypes,
bool excludeFolders)
{
try
{
if (_memoryCache.TryGetValue($"jellyfin_admin_user_id.{library.MediaSourceId}", out string userId))
{
IJellyfinApi service = RestService.For<IJellyfinApi>(address);
JellyfinLibraryItemsResponse items = await service.GetLibraryStats(
apiKey,
userId,
parentId,
includeItemTypes,
filters: excludeFolders ? "IsNotFolder" : null);
return items.TotalRecordCount;
}
return BaseError.New("Jellyfin admin user id is not available");
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting jellyfin library item count");
return BaseError.New(ex.Message);
}
}
public async Task<Either<BaseError, MediaVersion>> GetPlaybackInfo(
string address,
string apiKey,
@@ -275,40 +232,33 @@ public class JellyfinApiClient : IJellyfinApiClient
}
}
private async IAsyncEnumerable<TItem> GetPagedLibraryItems<TItem>(
private async IAsyncEnumerable<Tuple<TItem, int>> GetPagedLibraryItems<TItem>(
string address,
string apiKey,
Option<JellyfinLibrary> maybeLibrary,
int mediaSourceId,
string parentId,
string itemType,
Func<IJellyfinApi, string, string, int, int, Task<JellyfinLibraryItemsResponse>> getItems,
Func<Option<JellyfinLibrary>, JellyfinLibraryItemResponse, Option<TItem>> mapper)
{
if (_memoryCache.TryGetValue($"jellyfin_admin_user_id.{mediaSourceId}", out string userId))
{
IJellyfinApi service = RestService.For<IJellyfinApi>(address);
string filters = itemType == JellyfinItemType.Movie || itemType == JellyfinItemType.Episode
? "IsNotFolder"
: null;
int size = await service
.GetLibraryStats(apiKey, userId, parentId, itemType, filters: filters)
.Map(r => r.TotalRecordCount);
const int PAGE_SIZE = 10;
int pages = (size - 1) / PAGE_SIZE + 1;
int pages = int.MaxValue;
for (var i = 0; i < pages; i++)
{
int skip = i * PAGE_SIZE;
Task<IEnumerable<TItem>> result = getItems(service, userId, parentId, skip, PAGE_SIZE)
.Map(items => items.Items.Map(item => mapper(maybeLibrary, item)).Somes());
JellyfinLibraryItemsResponse result = await getItems(service, userId, parentId, skip, PAGE_SIZE);
foreach (TItem item in await result)
// update page count
pages = Math.Min(pages, (result.TotalRecordCount - 1) / PAGE_SIZE + 1);
foreach (TItem item in result.Items.Map(item => mapper(maybeLibrary, item)).Somes())
{
yield return item;
yield return new Tuple<TItem, int>(item, result.TotalRecordCount);
}
}
}
@@ -66,7 +66,7 @@ public class PlexServerApiClient : IPlexServerApiClient
}
}
public IAsyncEnumerable<PlexMovie> GetMovieLibraryContents(
public IAsyncEnumerable<Tuple<PlexMovie, int>> GetMovieLibraryContents(
PlexLibrary library,
PlexConnection connection,
PlexServerAuthToken token)
@@ -89,7 +89,7 @@ public class PlexServerApiClient : IPlexServerApiClient
return GetPagedLibraryContents(connection, CountItems, GetItems);
}
public IAsyncEnumerable<PlexShow> GetShowLibraryContents(
public IAsyncEnumerable<Tuple<PlexShow, int>> GetShowLibraryContents(
PlexLibrary library,
PlexConnection connection,
PlexServerAuthToken token)
@@ -110,24 +110,7 @@ public class PlexServerApiClient : IPlexServerApiClient
return GetPagedLibraryContents(connection, CountItems, GetItems);
}
public async Task<Either<BaseError, int>> CountShowSeasons(
PlexShow show,
PlexConnection connection,
PlexServerAuthToken token)
{
try
{
string showMetadataKey = show.Key.Split("/").Reverse().Skip(1).Head();
IPlexServerApi service = XmlServiceFor(connection.Uri);
return await service.CountShowChildren(showMetadataKey, token.AuthToken).Map(r => r.TotalSize);
}
catch (Exception ex)
{
return BaseError.New(ex.ToString());
}
}
public IAsyncEnumerable<PlexSeason> GetShowSeasons(
public IAsyncEnumerable<Tuple<PlexSeason, int>> GetShowSeasons(
PlexLibrary library,
PlexShow show,
PlexConnection connection,
@@ -150,24 +133,7 @@ public class PlexServerApiClient : IPlexServerApiClient
return GetPagedLibraryContents(connection, CountItems, GetItems);
}
public async Task<Either<BaseError, int>> CountSeasonEpisodes(
PlexSeason season,
PlexConnection connection,
PlexServerAuthToken token)
{
try
{
string seasonMetadataKey = season.Key.Split("/").Reverse().Skip(1).Head();
IPlexServerApi service = XmlServiceFor(connection.Uri);
return await service.CountSeasonChildren(seasonMetadataKey, token.AuthToken).Map(r => r.TotalSize);
}
catch (Exception ex)
{
return BaseError.New(ex.ToString());
}
}
public IAsyncEnumerable<PlexEpisode> GetSeasonEpisodes(
public IAsyncEnumerable<Tuple<PlexEpisode, int>> GetSeasonEpisodes(
PlexLibrary library,
PlexSeason season,
PlexConnection connection,
@@ -276,23 +242,7 @@ public class PlexServerApiClient : IPlexServerApiClient
}
}
public async Task<Either<BaseError, int>> GetLibraryItemCount(
PlexLibrary library,
PlexConnection connection,
PlexServerAuthToken token)
{
try
{
IPlexServerApi service = XmlServiceFor(connection.Uri);
return await service.GetLibrarySection(library.Key, token.AuthToken).Map(r => r.TotalSize);
}
catch (Exception ex)
{
return BaseError.New(ex.ToString());
}
}
public IAsyncEnumerable<PlexCollection> GetAllCollections(
public IAsyncEnumerable<Tuple<PlexCollection, int>> GetAllCollections(
PlexConnection connection,
PlexServerAuthToken token,
CancellationToken cancellationToken)
@@ -313,7 +263,7 @@ public class PlexServerApiClient : IPlexServerApiClient
}
}
public IAsyncEnumerable<MediaItem> GetCollectionItems(
public IAsyncEnumerable<Tuple<MediaItem, int>> GetCollectionItems(
PlexConnection connection,
PlexServerAuthToken token,
string key,
@@ -335,7 +285,7 @@ public class PlexServerApiClient : IPlexServerApiClient
}
}
private static async IAsyncEnumerable<TItem> GetPagedLibraryContents<TItem>(
private static async IAsyncEnumerable<Tuple<TItem, int>> GetPagedLibraryContents<TItem>(
PlexConnection connection,
Func<IPlexServerApi, Task<PlexXmlMediaContainerStatsResponse>> countItems,
Func<IPlexServerApi, IPlexServerApi, int, int, Task<IEnumerable<TItem>>> getItems)
@@ -357,7 +307,7 @@ public class PlexServerApiClient : IPlexServerApiClient
foreach (TItem item in await result)
{
yield return item;
yield return new Tuple<TItem, int>(item, size);
}
}
}
@@ -38,7 +38,7 @@ public class EmbyCollectionScanner : IEmbyCollectionScanner
// get all collections from db (item id, etag)
List<EmbyCollection> existingCollections = await _embyCollectionRepository.GetCollections();
await foreach (EmbyCollection collection in _embyApiClient.GetCollectionLibraryItems(address, apiKey))
await foreach ((EmbyCollection collection, int _) in _embyApiClient.GetCollectionLibraryItems(address, apiKey))
{
incomingItemIds.Add(collection.ItemId);
@@ -88,13 +88,13 @@ public class EmbyCollectionScanner : IEmbyCollectionScanner
try
{
// get collection items from Emby
IAsyncEnumerable<MediaItem> items = _embyApiClient.GetCollectionItems(address, apiKey, collection.ItemId);
IAsyncEnumerable<Tuple<MediaItem, int>> items = _embyApiClient.GetCollectionItems(address, apiKey, collection.ItemId);
List<int> removedIds = await _embyCollectionRepository.RemoveAllTags(collection);
// sync tags on items
var addedIds = new List<int>();
await foreach (MediaItem item in items)
await foreach ((MediaItem item, int _) in items)
{
addedIds.Add(await _embyCollectionRepository.AddTag(item, collection));
}
@@ -75,16 +75,7 @@ public class EmbyMovieLibraryScanner :
protected override string MediaServerItemId(EmbyMovie movie) => movie.ItemId;
protected override string MediaServerEtag(EmbyMovie movie) => movie.Etag;
protected override Task<Either<BaseError, int>> CountMovieLibraryItems(
EmbyConnectionParameters connectionParameters,
EmbyLibrary library) =>
_embyApiClient.GetLibraryItemCount(
connectionParameters.Address,
connectionParameters.ApiKey,
library.ItemId,
EmbyItemType.Movie);
protected override IAsyncEnumerable<EmbyMovie> GetMovieLibraryItems(
protected override IAsyncEnumerable<Tuple<EmbyMovie, int>> GetMovieLibraryItems(
EmbyConnectionParameters connectionParameters,
EmbyLibrary library) =>
_embyApiClient.GetMovieLibraryItems(
@@ -72,16 +72,7 @@ public class EmbyTelevisionLibraryScanner : MediaServerTelevisionLibraryScanner<
cancellationToken);
}
protected override Task<Either<BaseError, int>> CountShowLibraryItems(
EmbyConnectionParameters connectionParameters,
EmbyLibrary library)
=> _embyApiClient.GetLibraryItemCount(
connectionParameters.Address,
connectionParameters.ApiKey,
library.ItemId,
EmbyItemType.Show);
protected override IAsyncEnumerable<EmbyShow> GetShowLibraryItems(
protected override IAsyncEnumerable<Tuple<EmbyShow, int>> GetShowLibraryItems(
EmbyConnectionParameters connectionParameters,
EmbyLibrary library) =>
_embyApiClient.GetShowLibraryItems(connectionParameters.Address, connectionParameters.ApiKey, library);
@@ -94,17 +85,7 @@ public class EmbyTelevisionLibraryScanner : MediaServerTelevisionLibraryScanner<
protected override string MediaServerEtag(EmbySeason season) => season.Etag;
protected override string MediaServerEtag(EmbyEpisode episode) => episode.Etag;
protected override Task<Either<BaseError, int>> CountSeasonLibraryItems(
EmbyConnectionParameters connectionParameters,
EmbyLibrary library,
EmbyShow show) =>
_embyApiClient.GetLibraryItemCount(
connectionParameters.Address,
connectionParameters.ApiKey,
show.ItemId,
EmbyItemType.Season);
protected override IAsyncEnumerable<EmbySeason> GetSeasonLibraryItems(
protected override IAsyncEnumerable<Tuple<EmbySeason, int>> GetSeasonLibraryItems(
EmbyLibrary library,
EmbyConnectionParameters connectionParameters,
EmbyShow show) =>
@@ -114,17 +95,7 @@ public class EmbyTelevisionLibraryScanner : MediaServerTelevisionLibraryScanner<
library,
show.ItemId);
protected override Task<Either<BaseError, int>> CountEpisodeLibraryItems(
EmbyConnectionParameters connectionParameters,
EmbyLibrary library,
EmbySeason season) =>
_embyApiClient.GetLibraryItemCount(
connectionParameters.Address,
connectionParameters.ApiKey,
season.ItemId,
EmbyItemType.Episode);
protected override IAsyncEnumerable<EmbyEpisode> GetEpisodeLibraryItems(
protected override IAsyncEnumerable<Tuple<EmbyEpisode, int>> GetEpisodeLibraryItems(
EmbyLibrary library,
EmbyConnectionParameters connectionParameters,
EmbyShow show,
@@ -51,7 +51,7 @@ public class JellyfinCollectionScanner : IJellyfinCollectionScanner
List<JellyfinCollection> existingCollections = await _jellyfinCollectionRepository.GetCollections();
// loop over collections
await foreach (JellyfinCollection collection in _jellyfinApiClient.GetCollectionLibraryItems(
await foreach ((JellyfinCollection collection, int _) in _jellyfinApiClient.GetCollectionLibraryItems(
address,
apiKey,
mediaSourceId))
@@ -105,7 +105,7 @@ public class JellyfinCollectionScanner : IJellyfinCollectionScanner
try
{
// get collection items from JF
IAsyncEnumerable<MediaItem> items = _jellyfinApiClient.GetCollectionItems(
IAsyncEnumerable<Tuple<MediaItem, int>> items = _jellyfinApiClient.GetCollectionItems(
address,
apiKey,
mediaSourceId,
@@ -115,7 +115,7 @@ public class JellyfinCollectionScanner : IJellyfinCollectionScanner
// sync tags on items
var addedIds = new List<int>();
await foreach (MediaItem item in items)
await foreach ((MediaItem item, int _) in items)
{
addedIds.Add(await _jellyfinCollectionRepository.AddTag(item, collection));
}
@@ -76,18 +76,7 @@ public class JellyfinMovieLibraryScanner :
protected override string MediaServerEtag(JellyfinMovie movie) => movie.Etag;
protected override Task<Either<BaseError, int>> CountMovieLibraryItems(
JellyfinConnectionParameters connectionParameters,
JellyfinLibrary library) =>
_jellyfinApiClient.GetLibraryItemCount(
connectionParameters.Address,
connectionParameters.ApiKey,
library,
library.ItemId,
JellyfinItemType.Movie,
true);
protected override IAsyncEnumerable<JellyfinMovie> GetMovieLibraryItems(
protected override IAsyncEnumerable<Tuple<JellyfinMovie, int>> GetMovieLibraryItems(
JellyfinConnectionParameters connectionParameters,
JellyfinLibrary library) =>
_jellyfinApiClient.GetMovieLibraryItems(
@@ -73,18 +73,7 @@ public class JellyfinTelevisionLibraryScanner : MediaServerTelevisionLibraryScan
cancellationToken);
}
protected override Task<Either<BaseError, int>> CountShowLibraryItems(
JellyfinConnectionParameters connectionParameters,
JellyfinLibrary library)
=> _jellyfinApiClient.GetLibraryItemCount(
connectionParameters.Address,
connectionParameters.ApiKey,
library,
library.ItemId,
JellyfinItemType.Show,
false);
protected override IAsyncEnumerable<JellyfinShow> GetShowLibraryItems(
protected override IAsyncEnumerable<Tuple<JellyfinShow, int>> GetShowLibraryItems(
JellyfinConnectionParameters connectionParameters,
JellyfinLibrary library) =>
_jellyfinApiClient.GetShowLibraryItems(connectionParameters.Address, connectionParameters.ApiKey, library);
@@ -97,19 +86,7 @@ public class JellyfinTelevisionLibraryScanner : MediaServerTelevisionLibraryScan
protected override string MediaServerEtag(JellyfinSeason season) => season.Etag;
protected override string MediaServerEtag(JellyfinEpisode episode) => episode.Etag;
protected override Task<Either<BaseError, int>> CountSeasonLibraryItems(
JellyfinConnectionParameters connectionParameters,
JellyfinLibrary library,
JellyfinShow show) =>
_jellyfinApiClient.GetLibraryItemCount(
connectionParameters.Address,
connectionParameters.ApiKey,
library,
show.ItemId,
JellyfinItemType.Season,
false);
protected override IAsyncEnumerable<JellyfinSeason> GetSeasonLibraryItems(
protected override IAsyncEnumerable<Tuple<JellyfinSeason, int>> GetSeasonLibraryItems(
JellyfinLibrary library,
JellyfinConnectionParameters connectionParameters,
JellyfinShow show) =>
@@ -119,19 +96,7 @@ public class JellyfinTelevisionLibraryScanner : MediaServerTelevisionLibraryScan
library,
show.ItemId);
protected override Task<Either<BaseError, int>> CountEpisodeLibraryItems(
JellyfinConnectionParameters connectionParameters,
JellyfinLibrary library,
JellyfinSeason season) =>
_jellyfinApiClient.GetLibraryItemCount(
connectionParameters.Address,
connectionParameters.ApiKey,
library,
season.ItemId,
JellyfinItemType.Episode,
true);
protected override IAsyncEnumerable<JellyfinEpisode> GetEpisodeLibraryItems(
protected override IAsyncEnumerable<Tuple<JellyfinEpisode, int>> GetEpisodeLibraryItems(
JellyfinLibrary library,
JellyfinConnectionParameters connectionParameters,
JellyfinShow show,
@@ -48,27 +48,14 @@ public abstract class MediaServerMovieLibraryScanner<TConnectionParameters, TLib
{
try
{
Either<BaseError, int> maybeCount = await CountMovieLibraryItems(connectionParameters, library);
foreach (BaseError error in maybeCount.LeftToSeq())
{
return error;
}
foreach (int count in maybeCount.RightToSeq())
{
return await ScanLibrary(
movieRepository,
connectionParameters,
library,
getLocalPath,
GetMovieLibraryItems(connectionParameters, library),
count,
deepScan,
cancellationToken);
}
// this won't happen
return Unit.Default;
return await ScanLibrary(
movieRepository,
connectionParameters,
library,
getLocalPath,
GetMovieLibraryItems(connectionParameters, library),
deepScan,
cancellationToken);
}
catch (Exception ex) when (ex is TaskCanceledException or OperationCanceledException)
{
@@ -81,8 +68,7 @@ public abstract class MediaServerMovieLibraryScanner<TConnectionParameters, TLib
TConnectionParameters connectionParameters,
TLibrary library,
Func<TMovie, string> getLocalPath,
IAsyncEnumerable<TMovie> movieEntries,
int totalMovieCount,
IAsyncEnumerable<Tuple<TMovie, int>> movieEntries,
bool deepScan,
CancellationToken cancellationToken)
{
@@ -90,7 +76,7 @@ public abstract class MediaServerMovieLibraryScanner<TConnectionParameters, TLib
IReadOnlyDictionary<string, TEtag> existingMovies = (await movieRepository.GetExistingMovies(library))
.ToImmutableDictionary(e => e.MediaServerItemId, e => e);
await foreach (TMovie incoming in movieEntries.WithCancellation(cancellationToken))
await foreach ((TMovie incoming, int totalMovieCount) in movieEntries.WithCancellation(cancellationToken))
{
if (cancellationToken.IsCancellationRequested)
{
@@ -237,11 +223,7 @@ public abstract class MediaServerMovieLibraryScanner<TConnectionParameters, TLib
protected abstract string MediaServerItemId(TMovie movie);
protected abstract string MediaServerEtag(TMovie movie);
protected abstract Task<Either<BaseError, int>> CountMovieLibraryItems(
TConnectionParameters connectionParameters,
TLibrary library);
protected abstract IAsyncEnumerable<TMovie> GetMovieLibraryItems(
protected abstract IAsyncEnumerable<Tuple<TMovie, int>> GetMovieLibraryItems(
TConnectionParameters connectionParameters,
TLibrary library);
@@ -50,29 +50,14 @@ public abstract class MediaServerTelevisionLibraryScanner<TConnectionParameters,
{
try
{
Either<BaseError, int> maybeCount = await CountShowLibraryItems(connectionParameters, library);
foreach (BaseError error in maybeCount.LeftToSeq())
{
return error;
}
foreach (int count in maybeCount.RightToSeq())
{
_logger.LogDebug("Library {Library} contains {Count} shows", library.Name, count);
return await ScanLibrary(
televisionRepository,
connectionParameters,
library,
getLocalPath,
GetShowLibraryItems(connectionParameters, library),
count,
deepScan,
cancellationToken);
}
// this won't happen
return Unit.Default;
return await ScanLibrary(
televisionRepository,
connectionParameters,
library,
getLocalPath,
GetShowLibraryItems(connectionParameters, library),
deepScan,
cancellationToken);
}
catch (Exception ex) when (ex is TaskCanceledException or OperationCanceledException)
{
@@ -80,11 +65,7 @@ public abstract class MediaServerTelevisionLibraryScanner<TConnectionParameters,
}
}
protected abstract Task<Either<BaseError, int>> CountShowLibraryItems(
TConnectionParameters connectionParameters,
TLibrary library);
protected abstract IAsyncEnumerable<TShow> GetShowLibraryItems(
protected abstract IAsyncEnumerable<Tuple<TShow, int>> GetShowLibraryItems(
TConnectionParameters connectionParameters,
TLibrary library);
@@ -100,15 +81,14 @@ public abstract class MediaServerTelevisionLibraryScanner<TConnectionParameters,
TConnectionParameters connectionParameters,
TLibrary library,
Func<TEpisode, string> getLocalPath,
IAsyncEnumerable<TShow> showEntries,
int totalShowCount,
IAsyncEnumerable<Tuple<TShow, int>> showEntries,
bool deepScan,
CancellationToken cancellationToken)
{
var incomingItemIds = new List<string>();
List<TEtag> existingShows = await televisionRepository.GetExistingShows(library);
await foreach (TShow incoming in showEntries.WithCancellation(cancellationToken))
await foreach ((TShow incoming, int totalShowCount) in showEntries.WithCancellation(cancellationToken))
{
if (cancellationToken.IsCancellationRequested)
{
@@ -146,23 +126,6 @@ public abstract class MediaServerTelevisionLibraryScanner<TConnectionParameters,
foreach (MediaItemScanResult<TShow> result in maybeShow.RightToSeq())
{
Either<BaseError, int> maybeCount = await CountSeasonLibraryItems(
connectionParameters,
library,
result.Item);
foreach (BaseError error in maybeCount.LeftToSeq())
{
return error;
}
foreach (int count in maybeCount.RightToSeq())
{
_logger.LogDebug(
"Show {Title} contains {Count} seasons",
result.Item.ShowMetadata.Head().Title,
count);
}
Either<BaseError, Unit> scanResult = await ScanSeasons(
televisionRepository,
library,
@@ -220,22 +183,12 @@ public abstract class MediaServerTelevisionLibraryScanner<TConnectionParameters,
return Unit.Default;
}
protected abstract Task<Either<BaseError, int>> CountSeasonLibraryItems(
TConnectionParameters connectionParameters,
TLibrary library,
TShow show);
protected abstract IAsyncEnumerable<TSeason> GetSeasonLibraryItems(
protected abstract IAsyncEnumerable<Tuple<TSeason, int>> GetSeasonLibraryItems(
TLibrary library,
TConnectionParameters connectionParameters,
TShow show);
protected abstract Task<Either<BaseError, int>> CountEpisodeLibraryItems(
TConnectionParameters connectionParameters,
TLibrary library,
TSeason season);
protected abstract IAsyncEnumerable<TEpisode> GetEpisodeLibraryItems(
protected abstract IAsyncEnumerable<Tuple<TEpisode, int>> GetEpisodeLibraryItems(
TLibrary library,
TConnectionParameters connectionParameters,
TShow show,
@@ -293,14 +246,14 @@ public abstract class MediaServerTelevisionLibraryScanner<TConnectionParameters,
TShow show,
bool showIsUpdated,
TConnectionParameters connectionParameters,
IAsyncEnumerable<TSeason> seasonEntries,
IAsyncEnumerable<Tuple<TSeason, int>> seasonEntries,
bool deepScan,
CancellationToken cancellationToken)
{
var incomingItemIds = new List<string>();
List<TEtag> existingSeasons = await televisionRepository.GetExistingSeasons(library, show);
await foreach (TSeason incoming in seasonEntries.WithCancellation(cancellationToken))
await foreach ((TSeason incoming, int _) in seasonEntries.WithCancellation(cancellationToken))
{
incoming.ShowId = show.Id;
@@ -331,24 +284,6 @@ public abstract class MediaServerTelevisionLibraryScanner<TConnectionParameters,
foreach (MediaItemScanResult<TSeason> result in maybeSeason.RightToSeq())
{
Either<BaseError, int> maybeCount = await CountEpisodeLibraryItems(
connectionParameters,
library,
result.Item);
foreach (BaseError error in maybeCount.LeftToSeq())
{
return error;
}
foreach (int count in maybeCount.RightToSeq())
{
_logger.LogDebug(
"Show {Title} season {Season} contains {Count} episodes",
show.ShowMetadata.Head().Title,
result.Item.SeasonNumber,
count);
}
Either<BaseError, Unit> scanResult = await ScanEpisodes(
televisionRepository,
library,
@@ -408,14 +343,14 @@ public abstract class MediaServerTelevisionLibraryScanner<TConnectionParameters,
bool showIsUpdated,
TSeason season,
TConnectionParameters connectionParameters,
IAsyncEnumerable<TEpisode> episodeEntries,
IAsyncEnumerable<Tuple<TEpisode, int>> episodeEntries,
bool deepScan,
CancellationToken cancellationToken)
{
var incomingItemIds = new List<string>();
List<TEtag> existingEpisodes = await televisionRepository.GetExistingEpisodes(library, season);
await foreach (TEpisode incoming in episodeEntries.WithCancellation(cancellationToken))
await foreach ((TEpisode incoming, int _) in episodeEntries.WithCancellation(cancellationToken))
{
if (cancellationToken.IsCancellationRequested)
{
@@ -39,7 +39,7 @@ public class PlexCollectionScanner : IPlexCollectionScanner
// get all collections from db (key, etag)
List<PlexCollection> existingCollections = await _plexCollectionRepository.GetCollections();
await foreach (PlexCollection collection in _plexServerApiClient.GetAllCollections(
await foreach ((PlexCollection collection, int _) in _plexServerApiClient.GetAllCollections(
connection,
token,
cancellationToken))
@@ -93,7 +93,7 @@ public class PlexCollectionScanner : IPlexCollectionScanner
try
{
// get collection items from Plex
IAsyncEnumerable<MediaItem> items = _plexServerApiClient.GetCollectionItems(
IAsyncEnumerable<Tuple<MediaItem, int>> items = _plexServerApiClient.GetCollectionItems(
connection,
token,
collection.Key,
@@ -103,7 +103,7 @@ public class PlexCollectionScanner : IPlexCollectionScanner
// sync tags on items
var addedIds = new List<int>();
await foreach (MediaItem item in items)
await foreach ((MediaItem item, int _) in items)
{
addedIds.Add(await _plexCollectionRepository.AddTag(item, collection));
cancellationToken.ThrowIfCancellationRequested();
@@ -82,15 +82,7 @@ public class PlexMovieLibraryScanner :
protected override string MediaServerEtag(PlexMovie movie) => movie.Etag;
protected override Task<Either<BaseError, int>> CountMovieLibraryItems(
PlexConnectionParameters connectionParameters,
PlexLibrary library)
=> _plexServerApiClient.GetLibraryItemCount(
library,
connectionParameters.Connection,
connectionParameters.Token);
protected override IAsyncEnumerable<PlexMovie> GetMovieLibraryItems(
protected override IAsyncEnumerable<Tuple<PlexMovie, int>> GetMovieLibraryItems(
PlexConnectionParameters connectionParameters,
PlexLibrary library) =>
_plexServerApiClient.GetMovieLibraryContents(
@@ -130,15 +130,7 @@ public class PlexTelevisionLibraryScanner :
// }
// }
protected override Task<Either<BaseError, int>> CountShowLibraryItems(
PlexConnectionParameters connectionParameters,
PlexLibrary library) =>
_plexServerApiClient.GetLibraryItemCount(
library,
connectionParameters.Connection,
connectionParameters.Token);
protected override IAsyncEnumerable<PlexShow> GetShowLibraryItems(
protected override IAsyncEnumerable<Tuple<PlexShow, int>> GetShowLibraryItems(
PlexConnectionParameters connectionParameters,
PlexLibrary library) =>
_plexServerApiClient.GetShowLibraryContents(
@@ -146,16 +138,7 @@ public class PlexTelevisionLibraryScanner :
connectionParameters.Connection,
connectionParameters.Token);
protected override Task<Either<BaseError, int>> CountSeasonLibraryItems(
PlexConnectionParameters connectionParameters,
PlexLibrary library,
PlexShow show) =>
_plexServerApiClient.CountShowSeasons(
show,
connectionParameters.Connection,
connectionParameters.Token);
protected override IAsyncEnumerable<PlexSeason> GetSeasonLibraryItems(
protected override IAsyncEnumerable<Tuple<PlexSeason, int>> GetSeasonLibraryItems(
PlexLibrary library,
PlexConnectionParameters connectionParameters,
PlexShow show) =>
@@ -165,16 +148,7 @@ public class PlexTelevisionLibraryScanner :
connectionParameters.Connection,
connectionParameters.Token);
protected override Task<Either<BaseError, int>> CountEpisodeLibraryItems(
PlexConnectionParameters connectionParameters,
PlexLibrary library,
PlexSeason season) =>
_plexServerApiClient.CountSeasonEpisodes(
season,
connectionParameters.Connection,
connectionParameters.Token);
protected override IAsyncEnumerable<PlexEpisode> GetEpisodeLibraryItems(
protected override IAsyncEnumerable<Tuple<PlexEpisode, int>> GetEpisodeLibraryItems(
PlexLibrary library,
PlexConnectionParameters connectionParameters,
PlexShow show,