Skip to content

Commit

Permalink
updating plugin to the latest jellyfin libraries (10.10)
Browse files Browse the repository at this point in the history
  • Loading branch information
vosmiic committed Nov 7, 2024
1 parent f38365b commit 277a984
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.3.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="Moq" Version="4.17.1" />
<PackageReference Include="Moq.Contrib.HttpClient" Version="1.3.0" />
Expand Down
15 changes: 0 additions & 15 deletions jellyfin-ani-sync/Helpers/SyncHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,6 @@ public static List<BaseItem> GetUsersJellyfinLibrary(Guid userId, IUserManager u
return libraryManager.GetItemList(query);
}

public static Dictionary<int, (int, DateTime)> FilterSeriesByUserProgress(Guid userId, Series series, IUserDataManager userDataManager) {
Dictionary<int, (int, DateTime)> returnDictionary = new Dictionary<int, (int, DateTime)>();
var seasons = series.Children.OfType<Season>().Select(baseItem => baseItem).ToList();
foreach (Season season in seasons) {
List<Episode> episodes = season.Children.OfType<Episode>().Select(baseItem => baseItem).ToList();
int highestEpisodeWatched = episodes.Max(item => userDataManager.GetUserData(userId, item).Played ? item.IndexNumber.Value : 0);
DateTime? dateTimeWatched = episodes.Max(episode => userDataManager.GetUserData(userId, episode).LastPlayedDate);
if (highestEpisodeWatched != 0 && dateTimeWatched != null) {
returnDictionary.Add(season.IndexNumber.Value, (highestEpisodeWatched, dateTimeWatched.Value));
}
}

return returnDictionary;
}

/// <summary>
/// Get the preferred series provider ID.
/// </summary>
Expand Down
17 changes: 8 additions & 9 deletions jellyfin-ani-sync/Sync/Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ private async Task<List<Anime>> GetAnimeList(string userId) {
/// <param name="convertedWatchList">List of metadata IDs of shows.</param>
private async Task GetCurrentLibrary(string userId, List<SyncAnimeMetadata> convertedWatchList) {
var userLibrary = SyncHelper.GetUsersJellyfinLibrary(Guid.Parse(userId), _userManager, _libraryManager);
var user = _userManager.GetUserById(Guid.Parse(userId));
if (user == null) {
_logger.LogError($"(Sync) User with ID of {userId} not found");
return;
}
foreach (BaseItem baseItem in userLibrary) {
if (baseItem is Series series) {
(AnimeOfflineDatabaseHelpers.Source? source, int? providerId) = SyncHelper.GetSeriesProviderId(series);
Expand All @@ -159,9 +164,6 @@ private async Task GetCurrentLibrary(string userId, List<SyncAnimeMetadata> conv

foreach ((Season season, DateTime? completedAt, int episodesWatched) seasonsTuple in seasonsToMarkAsPlayed) {
if (seasonsTuple.season != null) {
var user = _userManager.GetUserById(Guid.Parse(userId));
List<UserItemData> userItemDataCollection = new List<UserItemData>();

var seasonEpisodes = seasonsTuple.season.Children.Where(episode => episode is Episode && episode.IndexNumber != null);
if (seasonsTuple.episodesWatched != -1) {
seasonEpisodes = seasonEpisodes.Where(episode => episode.IndexNumber != null && episode.IndexNumber <= seasonsTuple.episodesWatched);
Expand All @@ -172,18 +174,15 @@ private async Task GetCurrentLibrary(string userId, List<SyncAnimeMetadata> conv
_logger.LogInformation($"(Sync) Setting {episode.Series.Name} season {episode.Season.IndexNumber} episode {episode.IndexNumber} for user {userId} as played...");

if (seasonsTuple.completedAt != null) {
userItemDataCollection.Add(SetUserData(user, episode, seasonsTuple.completedAt));
_userDataManager.SaveUserData(user, seasonChild, SetUserData(user, episode, seasonsTuple.completedAt), UserDataSaveReason.UpdateUserData, CancellationToken.None);
} else {
userItemDataCollection.Add(SetUserData(user, episode, DateTime.UtcNow));
_userDataManager.SaveUserData(user, seasonChild, SetUserData(user, episode, DateTime.UtcNow), UserDataSaveReason.UpdateUserData, CancellationToken.None);
}
}
}

// this could fail because show has ovas, hard to detect. todo warn users
userItemDataCollection.Add(SetUserData(user, seasonsTuple.season, seasonsTuple.completedAt));


_userDataManager.SaveAllUserData(user.Id, userItemDataCollection.ToArray(), CancellationToken.None);
_userDataManager.SaveUserData(user, seasonsTuple.season, SetUserData(user, seasonsTuple.season, seasonsTuple.completedAt), UserDataSaveReason.UpdateUserData, CancellationToken.None);
_logger.LogInformation("(Sync) Saved");
}
}
Expand Down
21 changes: 14 additions & 7 deletions jellyfin-ani-sync/Sync/SyncProviderFromLocal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using jellyfin_ani_sync.Helpers;
using jellyfin_ani_sync.Interfaces;
using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller;
Expand Down Expand Up @@ -79,42 +80,47 @@ private async Task GetSeasonDetails(List<Series> userSeriesList) {
_logger.LogError($"(Sync) Could not sync item; error: {e.Message}");
continue;
}

_logger.LogInformation("(Sync) Waiting 2 seconds before continuing...");
Thread.Sleep(2000);
} else {
_logger.LogError("(Sync) Could not get users Jellyfin data for this season");
}
}
} else {
_logger.LogError($"(Sync) User with ID of {_userId} not found");
}
}
}

private List<Episode> GetMaxEpisodeAndCompletedTime(Series series) {
List<Episode> returnDictionary = new List<Episode>();

_logger.LogInformation($"(Sync) Getting {series.Name} seasons latest watched episode");
var seasons = series.Children.OfType<Season>().Select(baseItem => baseItem).ToList();
_logger.LogInformation($"(Sync) Series {series.Name} contains {seasons.Count} seasons");
User user = _userManager.GetUserById(_userId);
if (user == null) return null;
foreach (Season season in seasons) {
_logger.LogInformation($"(Sync) Getting user data for {season.Name} of {series.Name}...");
if (season.IndexNumber == null) {
_logger.LogError($"(Sync) Season index number is null. Skipping...");
continue;
}
var query = new InternalItemsQuery(_userManager.GetUserById(_userId)) {

var query = new InternalItemsQuery(user) {
MediaTypes = [ MediaType.Video ],
ParentId = season.ParentId,
ParentIndexNumber = season.IndexNumber,
Recursive = true
};

var itemList = _libraryManager.GetItemList(query);

List<Episode> episodes = itemList.OfType<Episode>().Select(baseItem => baseItem).ToList();

_logger.LogInformation($"(Sync) Season contains {episodes.Count} episodes");
var episodesWatched = episodes.Where(item => _userDataManager.GetUserData(_userId, item).Played).ToList();
var episodesWatched = episodes.Where(item => _userDataManager.GetUserData(user, item).Played).ToList();
_logger.LogInformation($"(Sync) User has watched {episodesWatched.Count} out of {episodes.Count} episodes of this season");
Episode highestEpisodeWatched;
if (episodesWatched.Any()) {
Expand All @@ -124,9 +130,10 @@ private List<Episode> GetMaxEpisodeAndCompletedTime(Series series) {
_logger.LogInformation($"(Sync) User has not watched any episodes of this season, skipping...");
continue;
}
returnDictionary.Add(highestEpisodeWatched/*, _userDataManager.GetUserData(_userId, highestEpisodeWatched).LastPlayedDate ?? DateTime.UtcNow*/);

returnDictionary.Add(highestEpisodeWatched);
}

_logger.LogInformation($"(Sync) Found {returnDictionary.Count} seasons that contain user data");
return returnDictionary;
}
Expand Down
4 changes: 2 additions & 2 deletions jellyfin-ani-sync/jellyfin-ani-sync.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Jellyfin.Controller" Version="10.9.11" />
<PackageReference Include="Jellyfin.Model" Version="10.9.11" />
<PackageReference Include="Jellyfin.Controller" Version="10.10.1" />
<PackageReference Include="Jellyfin.Model" Version="10.10.1" />
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0"/>
<PackageReference Include="System.Collections" Version="4.3.0"/>
Expand Down

0 comments on commit 277a984

Please sign in to comment.