Replace progress in SyncLibraryTask

This commit is contained in:
J2ghz 2016-11-21 09:57:43 +01:00
parent 6391acf4e2
commit 2c5f44498b
3 changed files with 82 additions and 79 deletions

View File

@ -2,6 +2,10 @@
{
using System;
/// <summary>
/// Similar to <see cref="IProgress{T}"/>, but it contains a split method and Report is relative, not absolute.
/// </summary>
/// <typeparam name="T">The type of progress update value</typeparam>
public interface ISplittableProgress<T> : IProgress<T>
{
ISplittableProgress<T> Split(int parts);

View File

@ -2,7 +2,10 @@
{
using System;
// Can't be generic, because it's impossible to do arithmetics on generics
/// <summary>
/// Similar to <see cref="Progress"/>, but report is relative, not absolute.
/// </summary>
/// Can't be generic, because it's impossible to do arithmetics on generics
public class SplittableProgress : Progress<double>, ISplittableProgress<double>
{
public SplittableProgress(Action<double> handler)
@ -10,7 +13,7 @@
{
}
public double Progress { get; private set; }
private double Progress { get; set; }
ISplittableProgress<double> ISplittableProgress<double>.Split(int parts)
{

View File

@ -1,6 +1,4 @@
using MediaBrowser.Model.Tasks;
namespace Trakt.ScheduledTasks
namespace Trakt.ScheduledTasks
{
using System;
using System.Collections.Generic;
@ -15,14 +13,15 @@ namespace Trakt.ScheduledTasks
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Tasks;
using Trakt.Api;
using Trakt.Api.DataContracts.Sync;
using Trakt.Helpers;
using Trakt.Model;
using MediaBrowser.Model.IO;
/// <summary>
/// Task that will Sync each users local library with their respective trakt.tv profiles. This task will only include
@ -32,13 +31,26 @@ namespace Trakt.ScheduledTasks
{
//private readonly IHttpClient _httpClient;
private readonly IJsonSerializer _jsonSerializer;
private readonly IUserManager _userManager;
private readonly ILogger _logger;
private readonly TraktApi _traktApi;
private readonly IUserDataManager _userDataManager;
private readonly ILibraryManager _libraryManager;
public SyncLibraryTask(ILogManager logger, IJsonSerializer jsonSerializer, IUserManager userManager, IUserDataManager userDataManager, IHttpClient httpClient, IServerApplicationHost appHost, IFileSystem fileSystem, ILibraryManager libraryManager)
public SyncLibraryTask(
ILogManager logger,
IJsonSerializer jsonSerializer,
IUserManager userManager,
IUserDataManager userDataManager,
IHttpClient httpClient,
IServerApplicationHost appHost,
IFileSystem fileSystem,
ILibraryManager libraryManager)
{
_jsonSerializer = jsonSerializer;
_userManager = userManager;
@ -55,7 +67,10 @@ namespace Trakt.ScheduledTasks
public string Key
{
get { return "TraktSyncLibraryTask"; }
get
{
return "TraktSyncLibraryTask";
}
}
/// <summary>
@ -72,10 +87,6 @@ namespace Trakt.ScheduledTasks
return;
}
// purely for progress reporting
var progPercent = 0.0;
var percentPerUser = 100 / users.Count;
foreach (var user in users)
{
var traktUser = UserHelper.GetTraktUser(user);
@ -87,10 +98,9 @@ namespace Trakt.ScheduledTasks
continue;
}
await SyncUserLibrary(user, traktUser, progPercent, percentPerUser, progress, cancellationToken)
await
SyncUserLibrary(user, traktUser, progress.Split(users.Count), cancellationToken)
.ConfigureAwait(false);
progPercent += percentPerUser;
}
}
@ -101,13 +111,13 @@ namespace Trakt.ScheduledTasks
private async Task SyncUserLibrary(
User user,
TraktUser traktUser,
double progPercent,
double percentPerUser,
IProgress<double> progress,
ISplittableProgress<double> progress,
CancellationToken cancellationToken)
{
// purely for progress reporting
var mediaItemsCount = _libraryManager.GetItemList(new InternalItemsQuery
var mediaItemsCount =
_libraryManager.GetItemList(
new InternalItemsQuery
{
IncludeItemTypes = new[] { typeof(Movie).Name, typeof(Episode).Name },
ExcludeLocationTypes = new[] { LocationType.Virtual }
@ -122,10 +132,8 @@ namespace Trakt.ScheduledTasks
_logger.Info(mediaItemsCount + " Items found for '" + user.Name + "'.");
var percentPerItem = (float)percentPerUser / mediaItemsCount / 2.0;
await SyncMovies(user, traktUser, progress, progPercent, percentPerItem, cancellationToken);
await SyncShows(user, traktUser, progress, progPercent, percentPerItem, cancellationToken);
await SyncMovies(user, traktUser, progress.Split(2), cancellationToken);
await SyncShows(user, traktUser, progress.Split(2), cancellationToken);
}
/// <summary>
@ -134,9 +142,7 @@ namespace Trakt.ScheduledTasks
private async Task SyncMovies(
User user,
TraktUser traktUser,
IProgress<double> progress,
double progPercent,
double percentPerItem,
ISplittableProgress<double> progress,
CancellationToken cancellationToken)
{
/*
@ -159,6 +165,7 @@ namespace Trakt.ScheduledTasks
var playedMovies = new List<Movie>();
var unplayedMovies = new List<Movie>();
var decisionProgress = progress.Split(4).Split(libraryMovies.Count);
foreach (var child in libraryMovies)
{
cancellationToken.ThrowIfCancellationRequested();
@ -169,7 +176,8 @@ namespace Trakt.ScheduledTasks
var collectedMathingMovies = SyncFromTraktTask.FindMatches(libraryMovie, traktCollectedMovies).ToList();
if (!collectedMathingMovies.Any()
|| (traktUser.ExportMediaInfo
&& collectedMathingMovies.All(collectedMovie => collectedMovie.MetadataIsDifferent(libraryMovie))))
&& collectedMathingMovies.All(
collectedMovie => collectedMovie.MetadataIsDifferent(libraryMovie))))
{
collectedMovies.Add(libraryMovie);
}
@ -188,7 +196,8 @@ namespace Trakt.ScheduledTasks
else if (!traktUser.SkipUnwatchedImportFromTrakt)
{
userData.Played = false;
await _userDataManager.SaveUserData(
await
_userDataManager.SaveUserData(
user.Id,
libraryMovie,
userData,
@ -206,28 +215,24 @@ namespace Trakt.ScheduledTasks
}
}
// purely for progress reporting
progPercent += percentPerItem;
progress.Report(progPercent);
decisionProgress.Report(100);
}
// send movies to mark collected
await SendMovieCollectionUpdates(true, traktUser, collectedMovies, progress, progPercent, percentPerItem, cancellationToken);
await SendMovieCollectionUpdates(true, traktUser, collectedMovies, progress.Split(4), cancellationToken);
// send movies to mark watched
await SendMoviePlaystateUpdates(true, traktUser, playedMovies, progress, progPercent, percentPerItem, cancellationToken);
await SendMoviePlaystateUpdates(true, traktUser, playedMovies, progress.Split(4), cancellationToken);
// send movies to mark unwatched
await SendMoviePlaystateUpdates(false, traktUser, unplayedMovies, progress, progPercent, percentPerItem, cancellationToken);
await SendMoviePlaystateUpdates(false, traktUser, unplayedMovies, progress.Split(4), cancellationToken);
}
private async Task SendMovieCollectionUpdates(
bool collected,
TraktUser traktUser,
List<Movie> movies,
IProgress<double> progress,
double progPercent,
double percentPerItem,
ISplittableProgress<double> progress,
CancellationToken cancellationToken)
{
_logger.Info("Movies to " + (collected ? "add to" : "remove from") + " Collection: " + movies.Count);
@ -237,8 +242,11 @@ namespace Trakt.ScheduledTasks
{
var dataContracts =
await
_traktApi.SendLibraryUpdateAsync(movies, traktUser, cancellationToken, collected ? EventType.Add : EventType.Remove)
.ConfigureAwait(false);
_traktApi.SendLibraryUpdateAsync(
movies,
traktUser,
cancellationToken,
collected ? EventType.Add : EventType.Remove).ConfigureAwait(false);
if (dataContracts != null)
{
foreach (var traktSyncResponse in dataContracts)
@ -256,9 +264,7 @@ namespace Trakt.ScheduledTasks
_logger.ErrorException("Exception handled sending movies to trakt.tv", e);
}
// purely for progress reporting
progPercent += percentPerItem * movies.Count;
progress.Report(progPercent);
progress.Report(100);
}
}
@ -266,9 +272,7 @@ namespace Trakt.ScheduledTasks
bool seen,
TraktUser traktUser,
List<Movie> playedMovies,
IProgress<double> progress,
double progPercent,
double percentPerItem,
ISplittableProgress<double> progress,
CancellationToken cancellationToken)
{
_logger.Info("Movies to set " + (seen ? string.Empty : "un") + "watched: " + playedMovies.Count);
@ -291,9 +295,7 @@ namespace Trakt.ScheduledTasks
_logger.ErrorException("Error updating movie play states", e);
}
// purely for progress reporting
progPercent += percentPerItem * playedMovies.Count;
progress.Report(progPercent);
progress.Report(100);
}
}
@ -303,9 +305,7 @@ namespace Trakt.ScheduledTasks
private async Task SyncShows(
User user,
TraktUser traktUser,
IProgress<double> progress,
double progPercent,
double percentPerItem,
ISplittableProgress<double> progress,
CancellationToken cancellationToken)
{
var traktWatchedShows = await _traktApi.SendGetWatchedShowsRequest(traktUser).ConfigureAwait(false);
@ -325,6 +325,7 @@ namespace Trakt.ScheduledTasks
var playedEpisodes = new List<Episode>();
var unplayedEpisodes = new List<Episode>();
var decisionProgress = progress.Split(4).Split(episodeItems.Count);
foreach (var child in episodeItems)
{
cancellationToken.ThrowIfCancellationRequested();
@ -352,7 +353,8 @@ namespace Trakt.ScheduledTasks
else if (!traktUser.SkipUnwatchedImportFromTrakt)
{
userData.Played = false;
await _userDataManager.SaveUserData(
await
_userDataManager.SaveUserData(
user.Id,
episode,
userData,
@ -375,25 +377,21 @@ namespace Trakt.ScheduledTasks
collectedEpisodes.Add(episode);
}
// purely for progress reporting
progPercent += percentPerItem;
progress.Report(progPercent);
decisionProgress.Report(100);
}
await SendEpisodeCollectionUpdates(true, traktUser, collectedEpisodes, progress, progPercent, percentPerItem, cancellationToken);
await SendEpisodeCollectionUpdates(true, traktUser, collectedEpisodes, progress.Split(4), cancellationToken);
await SendEpisodePlaystateUpdates(true, traktUser, playedEpisodes, progress, progPercent, percentPerItem, cancellationToken);
await SendEpisodePlaystateUpdates(true, traktUser, playedEpisodes, progress.Split(4), cancellationToken);
await SendEpisodePlaystateUpdates(false, traktUser, unplayedEpisodes, progress, progPercent, percentPerItem, cancellationToken);
await SendEpisodePlaystateUpdates(false, traktUser, unplayedEpisodes, progress.Split(4), cancellationToken);
}
private async Task SendEpisodePlaystateUpdates(
bool seen,
TraktUser traktUser,
List<Episode> playedEpisodes,
IProgress<double> progress,
double progPercent,
double percentPerItem,
ISplittableProgress<double> progress,
CancellationToken cancellationToken)
{
_logger.Info("Episodes to set " + (seen ? string.Empty : "un") + "watched: " + playedEpisodes.Count);
@ -414,9 +412,7 @@ namespace Trakt.ScheduledTasks
_logger.ErrorException("Error updating episode play states", e);
}
// purely for progress reporting
progPercent += percentPerItem * playedEpisodes.Count;
progress.Report(progPercent);
progress.Report(100);
}
}
@ -424,9 +420,7 @@ namespace Trakt.ScheduledTasks
bool collected,
TraktUser traktUser,
List<Episode> collectedEpisodes,
IProgress<double> progress,
double progPercent,
double percentPerItem,
ISplittableProgress<double> progress,
CancellationToken cancellationToken)
{
_logger.Info("Episodes to add to Collection: " + collectedEpisodes.Count);
@ -436,8 +430,11 @@ namespace Trakt.ScheduledTasks
{
var dataContracts =
await
_traktApi.SendLibraryUpdateAsync(collectedEpisodes, traktUser, cancellationToken, collected ? EventType.Add : EventType.Remove)
.ConfigureAwait(false);
_traktApi.SendLibraryUpdateAsync(
collectedEpisodes,
traktUser,
cancellationToken,
collected ? EventType.Add : EventType.Remove).ConfigureAwait(false);
if (dataContracts != null)
{
foreach (var traktSyncResponse in dataContracts)
@ -455,9 +452,7 @@ namespace Trakt.ScheduledTasks
_logger.ErrorException("Exception handled sending episodes to trakt.tv", e);
}
// purely for progress reporting
progPercent += percentPerItem * collectedEpisodes.Count;
progress.Report(progPercent);
progress.Report(100);
}
}
@ -465,7 +460,8 @@ namespace Trakt.ScheduledTasks
public string Category => "Trakt";
public string Description => "Adds any media that is in each users trakt monitored locations to their trakt.tv profile";
public string Description
=> "Adds any media that is in each users trakt monitored locations to their trakt.tv profile";
private void LogTraktResponseDataContract(TraktSyncResponse dataContract)
{