Skip to content

Commit

Permalink
Fix update task (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
scampower3 authored Oct 30, 2024
1 parent b0319b6 commit c5b5dda
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
5 changes: 5 additions & 0 deletions Jellyfin.Plugin.Tvdb/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,10 @@ public int MetadataUpdateInHours
/// Gets or sets a value indicating whether to update movie for the Check for Metadata Updates Scheduled Task.
/// </summary>
public bool UpdateMovieScheduledTask { get; set; } = false;

/// <summary>
/// Gets or sets a value indicating whether to update person for the Check for Metadata Updates Scheduled Task.
/// </summary>
public bool UpdatePersonScheduledTask { get; set; } = false;
}
}
6 changes: 6 additions & 0 deletions Jellyfin.Plugin.Tvdb/Configuration/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ <h2 class="sectionTitle">Check for Metadata Updates Scheduled Task Settings:</h2
<input is="emby-checkbox" type="checkbox" id="updateMovieScheduledTask" />
<span>Update Movie</span>
</label>
<label class="checkboxContainer">
<input is="emby-checkbox" type="checkbox" id="updatePersonScheduledTask" />
<span>Update Person</span>
</label>
<div>
<button is="emby-button" type="submit" data-theme="b" class="raised button-submit block">
<span>Save</span>
Expand Down Expand Up @@ -126,6 +130,7 @@ <h2 class="sectionTitle">Check for Metadata Updates Scheduled Task Settings:</h2
document.getElementById('updateSeasonScheduledTask').checked = config.UpdateSeasonScheduledTask;
document.getElementById('updateEpisodeScheduledTask').checked = config.UpdateEpisodeScheduledTask;
document.getElementById('updateMovieScheduledTask').checked = config.UpdateMovieScheduledTask;
document.getElementById('updatePersonScheduledTask').checked = config.UpdatePersonScheduledTask;
Dashboard.hideLoadingMsg();
});
},
Expand All @@ -152,6 +157,7 @@ <h2 class="sectionTitle">Check for Metadata Updates Scheduled Task Settings:</h2
config.UpdateSeasonScheduledTask = document.getElementById('updateSeasonScheduledTask').checked;
config.UpdateEpisodeScheduledTask = document.getElementById('updateEpisodeScheduledTask').checked;
config.UpdateMovieScheduledTask = document.getElementById('updateMovieScheduledTask').checked;
config.UpdatePersonScheduledTask = document.getElementById('updatePersonScheduledTask').checked;

ApiClient.updatePluginConfiguration(TvdbPluginConfiguration.uniquePluginId, config).then(function (result) {
Dashboard.processPluginConfigurationUpdateResult(result);
Expand Down
32 changes: 22 additions & 10 deletions Jellyfin.Plugin.Tvdb/ScheduledTasks/UpdateTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
Expand Down Expand Up @@ -70,6 +71,8 @@ public UpdateTask(TvdbClientManager tvdbClientManager, ILibraryManager libraryMa

private static bool UpdateMovieScheduledTask => TvdbPlugin.Instance?.Configuration.UpdateMovieScheduledTask ?? false;

private static bool UpdatePersonScheduledTask => TvdbPlugin.Instance?.Configuration.UpdatePersonScheduledTask ?? false;

/// <inheritdoc/>
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -113,48 +116,57 @@ public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
private async Task<HashSet<BaseItem>> GetItemsUpdated(CancellationToken cancellationToken)
{
double fromTime = DateTimeOffset.UtcNow.AddHours(MetadataUpdateInHours).ToUnixTimeSeconds();
List<EntityUpdate> allUpdates = new List<EntityUpdate>();

HashSet<BaseItem> toUpdateItems = new HashSet<BaseItem>();

if (UpdateSeriesScheduledTask)
{
var seriesUpdates = await _tvdbClientManager.GetUpdates(fromTime, cancellationToken, Type.Series, Action.Update).ConfigureAwait(false);
allUpdates.AddRange(seriesUpdates);
AddUpdateItemsToHashSet(toUpdateItems, seriesUpdates, BaseItemKind.Series);
}

if (UpdateSeasonScheduledTask)
{
var seasonUpdates = await _tvdbClientManager.GetUpdates(fromTime, cancellationToken, Type.Seasons, Action.Update).ConfigureAwait(false);
allUpdates.AddRange(seasonUpdates);
AddUpdateItemsToHashSet(toUpdateItems, seasonUpdates, BaseItemKind.Season);
}

if (UpdateEpisodeScheduledTask)
{
var episodeUpdates = await _tvdbClientManager.GetUpdates(fromTime, cancellationToken, Type.Episodes, Action.Update).ConfigureAwait(false);
allUpdates.AddRange(episodeUpdates);
AddUpdateItemsToHashSet(toUpdateItems, episodeUpdates, BaseItemKind.Episode);
}

if (UpdateMovieScheduledTask)
{
var movieUpdates = await _tvdbClientManager.GetUpdates(fromTime, cancellationToken, Type.Movies, Action.Update).ConfigureAwait(false);
allUpdates.AddRange(movieUpdates);
AddUpdateItemsToHashSet(toUpdateItems, movieUpdates, BaseItemKind.Movie);
}

string providerId = MetadataProvider.Tvdb.ToString();
if (UpdatePersonScheduledTask)
{
var personUpdates = await _tvdbClientManager.GetUpdates(fromTime, cancellationToken, Type.People, Action.Update).ConfigureAwait(false);
AddUpdateItemsToHashSet(toUpdateItems, personUpdates, BaseItemKind.Person);
}

HashSet<BaseItem> toUpdateItems = new HashSet<BaseItem>();
return toUpdateItems;
}

private void AddUpdateItemsToHashSet(HashSet<BaseItem> toUpdateItems, IReadOnlyList<EntityUpdate> tvdbUpdates, BaseItemKind baseItemKind)
{
string providerId = MetadataProvider.Tvdb.ToString();
Dictionary<string, string> providerIdPair = new Dictionary<string, string>() { { providerId, string.Empty } };

InternalItemsQuery query = new InternalItemsQuery();
query.IncludeItemTypes = new[] { baseItemKind };

foreach (EntityUpdate update in allUpdates)
foreach (EntityUpdate update in tvdbUpdates)
{
providerIdPair[providerId] = update.RecordId!.Value.ToString(CultureInfo.InvariantCulture);
query.HasAnyProviderId = providerIdPair;
List<BaseItem> itemList = _libraryManager.GetItemList(query);
toUpdateItems.UnionWith(itemList);
}

return toUpdateItems;
}
}
}

0 comments on commit c5b5dda

Please sign in to comment.