From 075b3364308f9d7dbb292a197b92f60bd63f2ad3 Mon Sep 17 00:00:00 2001 From: Jamie Rees Date: Fri, 29 Jun 2018 22:13:53 +0100 Subject: [PATCH] More Emby improvements !wip --- src/Ombi.Api.Emby/EmbyApi.cs | 4 +- .../Jobs/Emby/EmbyContentSync.cs | 86 +++++++++++-------- .../Jobs/Emby/EmbyEpisodeSync.cs | 4 +- 3 files changed, 56 insertions(+), 38 deletions(-) diff --git a/src/Ombi.Api.Emby/EmbyApi.cs b/src/Ombi.Api.Emby/EmbyApi.cs index 714ce2d28..d94a42f90 100644 --- a/src/Ombi.Api.Emby/EmbyApi.cs +++ b/src/Ombi.Api.Emby/EmbyApi.cs @@ -104,9 +104,9 @@ namespace Ombi.Api.Emby return await GetAll("Movie", apiKey, userId, baseUri, true, startIndex, count); } - public async Task> GetAllEpisodes(string apiKey, string userId, string baseUri) + public async Task> GetAllEpisodes(string apiKey, int startIndex, int count, string userId, string baseUri) { - return await GetAll("Episode", apiKey, userId, baseUri); + return await GetAll("Episode", apiKey, userId, baseUri, false, startIndex, count); } public async Task> GetAllShows(string apiKey, int startIndex, int count, string userId, string baseUri) diff --git a/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs b/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs index 4c11e7cc2..03f61f4e4 100644 --- a/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs +++ b/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs @@ -71,65 +71,83 @@ namespace Ombi.Schedule.Jobs.Emby //await _repo.ExecuteSql("DELETE FROM EmbyEpisode"); //await _repo.ExecuteSql("DELETE FROM EmbyContent"); - var movies = await _api.GetAllMovies(server.ApiKey,0, 200, 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 processed = 1; var mediaToAdd = new HashSet(); while (processed < totalCount) { - foreach (var movie in movies.Items) + try { - processed++; - // Regular movie - await ProcessMovies(movie, mediaToAdd); - } + foreach (var movie in movies.Items) + { + processed++; + // Regular movie + await ProcessMovies(movie, mediaToAdd); + } - // Get the next batch - movies = await _api.GetAllMovies(server.ApiKey, processed + 1, 200, server.AdministratorId, server.FullUri); - await _repo.AddRange(mediaToAdd); - mediaToAdd.Clear(); + // Get the next batch + movies = await _api.GetAllMovies(server.ApiKey, processed, 200, server.AdministratorId, server.FullUri); + await _repo.AddRange(mediaToAdd); + mediaToAdd.Clear(); + } + catch (Exception) + { + + throw; + } } // TV Time var tv = await _api.GetAllShows(server.ApiKey, 0, 200, server.AdministratorId, server.FullUri); var totalTv = tv.TotalRecordCount; - processed = 0; + processed = 1; while (processed < totalTv) { foreach (var tvShow in tv.Items) { - processed++; - if (string.IsNullOrEmpty(tvShow.ProviderIds?.Tvdb)) + try { - _logger.LogInformation("Provider Id on tv {0} is null", tvShow.Name); - continue; - } - var existingTv = await _repo.GetByEmbyId(tvShow.Id); - if (existingTv == null) - { - _logger.LogDebug("Adding new TV Show {0}", tvShow.Name); - mediaToAdd.Add(new EmbyContent + processed++; + if (string.IsNullOrEmpty(tvShow.ProviderIds?.Tvdb)) { - 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 - }); + _logger.LogInformation("Provider Id on tv {0} is null", tvShow.Name); + continue; + } + + var existingTv = await _repo.GetByEmbyId(tvShow.Id); + if (existingTv == null) + { + _logger.LogDebug("Adding new TV Show {0}", tvShow.Name); + mediaToAdd.Add(new EmbyContent + { + 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 + }); + } + else + { + _logger.LogDebug("We already have TV Show {0}", tvShow.Name); + } + } - else + catch (Exception) { - _logger.LogDebug("We already have TV Show {0}", tvShow.Name); + + throw; } } // Get the next batch - tv = await _api.GetAllShows(server.ApiKey, processed + 1, 200, server.AdministratorId, server.FullUri); + tv = await _api.GetAllShows(server.ApiKey, processed, 200, server.AdministratorId, server.FullUri); await _repo.AddRange(mediaToAdd); mediaToAdd.Clear(); } diff --git a/src/Ombi.Schedule/Jobs/Emby/EmbyEpisodeSync.cs b/src/Ombi.Schedule/Jobs/Emby/EmbyEpisodeSync.cs index b0f361a8c..f66ff89ab 100644 --- a/src/Ombi.Schedule/Jobs/Emby/EmbyEpisodeSync.cs +++ b/src/Ombi.Schedule/Jobs/Emby/EmbyEpisodeSync.cs @@ -75,7 +75,7 @@ namespace Ombi.Schedule.Jobs.Emby { var allEpisodes = await _api.GetAllEpisodes(server.ApiKey, 0, 200, server.AdministratorId, server.FullUri); var total = allEpisodes.TotalRecordCount; - var processed = 0; + var processed = 1; var epToAdd = new HashSet(); while (processed < total) { @@ -117,7 +117,7 @@ namespace Ombi.Schedule.Jobs.Emby await _repo.AddRange(epToAdd); epToAdd.Clear(); - allEpisodes = await _api.GetAllEpisodes(server.ApiKey, processed + 1, 200, server.AdministratorId, server.FullUri); + allEpisodes = await _api.GetAllEpisodes(server.ApiKey, processed, 200, server.AdministratorId, server.FullUri); } if (epToAdd.Any())