Add a scheduled task that looks for new box sets every 24 hours

This commit is contained in:
Claus Vium 2019-02-06 09:29:57 +01:00
parent 71fe4dce48
commit f06b2b2dda
2 changed files with 17 additions and 9 deletions

View File

@ -1,3 +1,4 @@
using System;
using MediaBrowser.Model.Services;
using Microsoft.Extensions.Logging;
@ -19,11 +20,11 @@ namespace TMDbBoxSets.Api
_logger = logger;
}
public void RefreshMetadata(RefreshMetadataRequest request)
public void RefreshMetadata(RefreshMetadataRequest request, IProgress<double> progress)
{
// TODO
_logger.LogInformation("Starting a manual refresh of TMDb collections");
_tmDbBoxSetManager.ScanLibrary();
_tmDbBoxSetManager.ScanLibrary(progress);
_logger.LogInformation("Completed refresh of TMDb collections");
}
}

View File

@ -69,7 +69,8 @@ namespace TMDbBoxSets
},
Recursive = true,
HasTmdbId = true
}).Select(m => m as Movie).Where(m => m.HasProviderId(MetadataProviders.TmdbCollection));
}).Select(m => m as Movie).Where(m => m.HasProviderId(MetadataProviders.TmdbCollection)
&& !string.IsNullOrWhiteSpace(m.GetProviderId(MetadataProviders.TmdbCollection)));
}
private IReadOnlyCollection<BoxSet> GetAllBoxSetsFromLibrary()
@ -82,21 +83,29 @@ namespace TMDbBoxSets
}).Select(b => b as BoxSet).Where(b => b.HasProviderId(MetadataProviders.Tmdb)).ToList();
}
public void ScanLibrary()
public void ScanLibrary(IProgress<double> progress)
{
var boxSets = GetAllBoxSetsFromLibrary();
var movieCollections = GetMoviesFromLibrary()
.Where(m => string.IsNullOrWhiteSpace(m.GetProviderId(MetadataProviders.TmdbCollection)))
.GroupBy(m => m.GetProviderId(MetadataProviders.TmdbCollection));
.GroupBy(m => m.GetProviderId(MetadataProviders.TmdbCollection))
.ToArray();
_logger.LogInformation("Found {Count} TMDb collection(s) across all movies", movieCollections.Length);
int index = 0;
foreach (var movieCollection in movieCollections)
{
double percent = (double)index / movieCollections.Length;
progress.Report(100 * percent);
var tmdbCollectionId = movieCollection.Key;
var boxSet = boxSets.FirstOrDefault(b => b.GetProviderId(MetadataProviders.Tmdb) == tmdbCollectionId);
AddMoviesToCollection(movieCollection.ToList(), tmdbCollectionId, boxSet);
index++;
}
progress.Report(100);
}
private void OnLibraryManagerItemUpdated(object sender, ItemChangeEventArgs e)
@ -125,9 +134,7 @@ namespace TMDbBoxSets
// Stop the timer until next update
_timer.Change(Timeout.Infinite, Timeout.Infinite);
var tmdbCollectionIds = _queuedTmdbCollectionIds
.Where(m => !string.IsNullOrWhiteSpace(m))
.ToArray();
var tmdbCollectionIds = _queuedTmdbCollectionIds.ToArray();
// Clear the queue now, TODO what if it crashes? Should it be cleared after it's done?
_queuedTmdbCollectionIds.Clear();