public static IEnumerable<TraktCommentItem> GetCommentedSeasonsFromTrakt(bool ignoreLastSyncTime = false)
{
// get from cache regardless of last sync time
if (ignoreLastSyncTime)
return CommentedSeasons;
TraktLogger.Info("Getting current user commented seasons from trakt.tv");
// get the last time we did anything to our library online
var lastSyncActivities = LastSyncActivities;
// something bad happened e.g. site not available
if (lastSyncActivities == null || lastSyncActivities.Seasons == null)
return null;
// check the last time we have against the online time
// if the times are the same try to load from cache
if (lastSyncActivities.Seasons.Comment == TraktSettings.LastSyncActivities.Seasons.Comment)
{
var cachedItems = CommentedSeasons;
if (cachedItems != null)
return cachedItems;
}
TraktLogger.Info("TV season comments cache is out of date, requesting updated data. Local Date = '{0}', Online Date = '{1}'", TraktSettings.LastSyncActivities.Seasons.Comment ?? "<empty>", lastSyncActivities.Seasons.Comment ?? "<empty>");
// we get from online, local cache is not up to date
var onlineItems = TraktAPI.TraktAPI.GetUsersComments("me", "all", "seasons", "min");
if (onlineItems != null)
{
bool commentExists = false;
var pagedItems = onlineItems.Comments;
// check if we need to request more pages
if (CommentedSeasons != null && pagedItems.IsAny())
{
// if the comment id exists then we already have all comments
commentExists = CommentedSeasons.Any(c => c.Comment.Id == pagedItems.Last().Comment.Id);
// add the latest page to our previous cached comments
pagedItems = pagedItems.Union(CommentedSeasons);
}
// get more pages
if (!commentExists && pagedItems.IsAny() && onlineItems.Comments.Count() == onlineItems.TotalItemsPerPage)
{
for (int i = 2; i <= onlineItems.TotalPages; i++)
{
var nextPage = TraktAPI.TraktAPI.GetUsersComments("me", "all", "seasons", "min", i);
if (nextPage == null || !nextPage.Comments.IsAny()) break;
// if the comment id exists then we already have all comments
if (pagedItems.Any(c => c.Comment.Id == nextPage.Comments.Last().Comment.Id))
commentExists = true;
// add the latest page to our previous requested comments
pagedItems = pagedItems.Union(nextPage.Comments);
if (commentExists || nextPage.Comments.Count() < nextPage.TotalItemsPerPage)
break;
}
}
// evaluate any union additions
if (pagedItems != null)
pagedItems = pagedItems.ToList();
_CommentedSeasons = pagedItems;
// save to local file cache
SaveFileCache(SeasonsCommentedFile, _CommentedSeasons.ToJSON());
// save new activity time for next time
TraktSettings.LastSyncActivities.Seasons.Comment = lastSyncActivities.Seasons.Comment;
return pagedItems == null ? null : pagedItems.OrderByDescending(c => c.Comment.CreatedAt);
}
else
{
return null;
}
}