Emby Improvements: Batch up the amount we get from the server.

pull/2332/head
Jamie Rees 6 years ago
parent b573bc30f5
commit a4da4d0cc5

@ -99,9 +99,9 @@ namespace Ombi.Api.Emby
return await Api.Request<EmbyItemContainer<MovieInformation>>(request); return await Api.Request<EmbyItemContainer<MovieInformation>>(request);
} }
public async Task<EmbyItemContainer<EmbyMovie>> GetAllMovies(string apiKey, string userId, string baseUri) public async Task<EmbyItemContainer<EmbyMovie>> GetAllMovies(string apiKey, int startIndex, int count, string userId, string baseUri)
{ {
return await GetAll<EmbyMovie>("Movie", apiKey, userId, baseUri, true); return await GetAll<EmbyMovie>("Movie", apiKey, userId, baseUri, true, startIndex, count);
} }
public async Task<EmbyItemContainer<EmbyEpisodes>> GetAllEpisodes(string apiKey, string userId, string baseUri) public async Task<EmbyItemContainer<EmbyEpisodes>> GetAllEpisodes(string apiKey, string userId, string baseUri)
@ -109,9 +109,9 @@ namespace Ombi.Api.Emby
return await GetAll<EmbyEpisodes>("Episode", apiKey, userId, baseUri); return await GetAll<EmbyEpisodes>("Episode", apiKey, userId, baseUri);
} }
public async Task<EmbyItemContainer<EmbySeries>> GetAllShows(string apiKey, string userId, string baseUri) public async Task<EmbyItemContainer<EmbySeries>> GetAllShows(string apiKey, int startIndex, int count, string userId, string baseUri)
{ {
return await GetAll<EmbySeries>("Series", apiKey, userId, baseUri); return await GetAll<EmbySeries>("Series", apiKey, userId, baseUri, false, startIndex, count);
} }
public async Task<SeriesInformation> GetSeriesInformation(string mediaId, string apiKey, string userId, string baseUrl) public async Task<SeriesInformation> GetSeriesInformation(string mediaId, string apiKey, string userId, string baseUrl)
@ -145,7 +145,25 @@ namespace Ombi.Api.Emby
request.AddQueryString("IncludeItemTypes", type); request.AddQueryString("IncludeItemTypes", type);
request.AddQueryString("Fields", includeOverview ? "ProviderIds,Overview" : "ProviderIds"); request.AddQueryString("Fields", includeOverview ? "ProviderIds,Overview" : "ProviderIds");
request.AddQueryString("VirtualItem","False"); request.AddQueryString("VirtualItem", "False");
AddHeaders(request, apiKey);
var obj = await Api.Request<EmbyItemContainer<T>>(request);
return obj;
}
private async Task<EmbyItemContainer<T>> GetAll<T>(string type, string apiKey, string userId, string baseUri, bool includeOverview, int startIndex, int count)
{
var request = new Request($"emby/users/{userId}/items", baseUri, HttpMethod.Get);
request.AddQueryString("Recursive", true.ToString());
request.AddQueryString("IncludeItemTypes", type);
request.AddQueryString("Fields", includeOverview ? "ProviderIds,Overview" : "ProviderIds");
request.AddQueryString("startIndex", startIndex.ToString());
request.AddQueryString("limit", count.ToString());
request.AddQueryString("VirtualItem", "False");
AddHeaders(request, apiKey); AddHeaders(request, apiKey);

@ -14,9 +14,14 @@ namespace Ombi.Api.Emby
Task<EmbyUser> LogIn(string username, string password, string apiKey, string baseUri); Task<EmbyUser> LogIn(string username, string password, string apiKey, string baseUri);
Task<EmbyConnectUser> LoginConnectUser(string username, string password); Task<EmbyConnectUser> LoginConnectUser(string username, string password);
Task<EmbyItemContainer<EmbyMovie>> GetAllMovies(string apiKey, string userId, string baseUri); Task<EmbyItemContainer<EmbyMovie>> GetAllMovies(string apiKey, int startIndex, int count, string userId,
Task<EmbyItemContainer<EmbyEpisodes>> GetAllEpisodes(string apiKey, string userId, string baseUri); string baseUri);
Task<EmbyItemContainer<EmbySeries>> GetAllShows(string apiKey, string userId, string baseUri);
Task<EmbyItemContainer<EmbyEpisodes>> GetAllEpisodes(string apiKey, int startIndex, int count, string userId,
string baseUri);
Task<EmbyItemContainer<EmbySeries>> GetAllShows(string apiKey, int startIndex, int count, string userId,
string baseUri);
Task<EmbyItemContainer<MovieInformation>> GetCollection(string mediaId, string apiKey, string userId, Task<EmbyItemContainer<MovieInformation>> GetCollection(string mediaId, string apiKey, string userId,
string baseUrl); string baseUrl);

@ -71,37 +71,60 @@ namespace Ombi.Schedule.Jobs.Emby
await _repo.ExecuteSql("DELETE FROM EmbyEpisode"); await _repo.ExecuteSql("DELETE FROM EmbyEpisode");
await _repo.ExecuteSql("DELETE FROM EmbyContent"); await _repo.ExecuteSql("DELETE FROM EmbyContent");
var movies = await _api.GetAllMovies(server.ApiKey, server.AdministratorId, server.FullUri); var movies = await _api.GetAllMovies(server.ApiKey,0, 200, server.AdministratorId, server.FullUri);
var totalCount = movies.TotalRecordCount;
var processed = 0;
var mediaToAdd = new HashSet<EmbyContent>(); var mediaToAdd = new HashSet<EmbyContent>();
foreach (var movie in movies.Items)
{
// Regular movie
await ProcessMovies(movie, mediaToAdd);
}
// TV Time
var tv = await _api.GetAllShows(server.ApiKey, server.AdministratorId, server.FullUri);
foreach (var tvShow in tv.Items) while (processed < totalCount)
{ {
if (string.IsNullOrEmpty(tvShow.ProviderIds?.Tvdb)) foreach (var movie in movies.Items)
{ {
Log.Error("Provider Id on tv {0} is null", tvShow.Name); processed++;
continue; // Regular movie
await ProcessMovies(movie, mediaToAdd);
} }
var existingTv = await _repo.GetByEmbyId(tvShow.Id); // Get the next batch
if (existingTv == null) movies = await _api.GetAllMovies(server.ApiKey, processed + 1, 200, server.AdministratorId, server.FullUri);
mediaToAdd.Add(new EmbyContent await _repo.AddRange(mediaToAdd);
mediaToAdd.Clear();
}
// TV Time
var tv = await _api.GetAllShows(server.ApiKey, 0, 200, server.AdministratorId, server.FullUri);
var totalTv = tv.TotalRecordCount;
processed = 0;
while (processed < totalTv)
{
foreach (var tvShow in tv.Items)
{
processed++;
if (string.IsNullOrEmpty(tvShow.ProviderIds?.Tvdb))
{ {
TvDbId = tvShow.ProviderIds?.Tvdb, Log.Error("Provider Id on tv {0} is null", tvShow.Name);
ImdbId = tvShow.ProviderIds?.Imdb, continue;
TheMovieDbId = tvShow.ProviderIds?.Tmdb, }
Title = tvShow.Name,
Type = EmbyMediaType.Series, var existingTv = await _repo.GetByEmbyId(tvShow.Id);
EmbyId = tvShow.Id, if (existingTv == null)
Url = EmbyHelper.GetEmbyMediaUrl(tvShow.Id), mediaToAdd.Add(new EmbyContent
AddedAt = DateTime.UtcNow {
}); TvDbId = tvShow.ProviderIds?.Tvdb,
ImdbId = tvShow.ProviderIds?.Imdb,
TheMovieDbId = tvShow.ProviderIds?.Tmdb,
Title = tvShow.Name,
Type = EmbyMediaType.Series,
EmbyId = tvShow.Id,
Url = EmbyHelper.GetEmbyMediaUrl(tvShow.Id),
AddedAt = DateTime.UtcNow
});
}
// Get the next batch
tv = await _api.GetAllShows(server.ApiKey, processed + 1, 200, server.AdministratorId, server.FullUri);
await _repo.AddRange(mediaToAdd);
mediaToAdd.Clear();
} }
if (mediaToAdd.Any()) if (mediaToAdd.Any())

@ -73,37 +73,47 @@ namespace Ombi.Schedule.Jobs.Emby
private async Task CacheEpisodes(EmbyServers server) private async Task CacheEpisodes(EmbyServers server)
{ {
var allEpisodes = await _api.GetAllEpisodes(server.ApiKey, server.AdministratorId, server.FullUri); var allEpisodes = await _api.GetAllEpisodes(server.ApiKey, 0, 200, server.AdministratorId, server.FullUri);
var total = allEpisodes.TotalRecordCount;
var processed = 0;
var epToAdd = new List<EmbyEpisode>(); var epToAdd = new List<EmbyEpisode>();
while (processed < total)
foreach (var ep in allEpisodes.Items)
{ {
// Let's make sure we have the parent request, stop those pesky forign key errors, foreach (var ep in allEpisodes.Items)
// Damn me having data integrity
var parent = await _repo.GetByEmbyId(ep.SeriesId);
if (parent == null)
{ {
_logger.LogInformation("The episode {0} does not relate to a series, so we cannot save this", ep.Name); processed++;
continue; // Let's make sure we have the parent request, stop those pesky forign key errors,
} // Damn me having data integrity
var parent = await _repo.GetByEmbyId(ep.SeriesId);
if (parent == null)
{
_logger.LogInformation("The episode {0} does not relate to a series, so we cannot save this",
ep.Name);
continue;
}
var existingEpisode = await _repo.GetEpisodeByEmbyId(ep.Id); var existingEpisode = await _repo.GetEpisodeByEmbyId(ep.Id);
if (existingEpisode == null) if (existingEpisode == null)
{
// add it
epToAdd.Add(new EmbyEpisode
{ {
EmbyId = ep.Id, // add it
EpisodeNumber = ep.IndexNumber, epToAdd.Add(new EmbyEpisode
SeasonNumber = ep.ParentIndexNumber, {
ParentId = ep.SeriesId, EmbyId = ep.Id,
TvDbId = ep.ProviderIds.Tvdb, EpisodeNumber = ep.IndexNumber,
TheMovieDbId = ep.ProviderIds.Tmdb, SeasonNumber = ep.ParentIndexNumber,
ImdbId = ep.ProviderIds.Imdb, ParentId = ep.SeriesId,
Title = ep.Name, TvDbId = ep.ProviderIds.Tvdb,
AddedAt = DateTime.UtcNow TheMovieDbId = ep.ProviderIds.Tmdb,
}); ImdbId = ep.ProviderIds.Imdb,
Title = ep.Name,
AddedAt = DateTime.UtcNow
});
}
} }
await _repo.AddRange(epToAdd);
epToAdd.Clear();
allEpisodes = await _api.GetAllEpisodes(server.ApiKey, processed + 1, 200, server.AdministratorId, server.FullUri);
} }
if (epToAdd.Any()) if (epToAdd.Any())

Loading…
Cancel
Save