Skip to content

Commit

Permalink
fix playback report under 10.10
Browse files Browse the repository at this point in the history
  • Loading branch information
kookxiang committed Oct 5, 2024
1 parent 5399da1 commit 3a12126
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
7 changes: 6 additions & 1 deletion Jellyfin.Plugin.Bangumi/BangumiApi.Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ private Task<string> SendRequest(HttpRequestMessage request, string? accessToken

private Task<string> SendRequestWithCache(HttpRequestMessage request, string? accessToken, CancellationToken token)
{
if (request.Method != HttpMethod.Get || request.RequestUri == null) return SendRequestWithOutCache(request, accessToken, token);
if (request.RequestUri == null) return SendRequestWithOutCache(request, accessToken, token);
if (request.Method != HttpMethod.Get)
{
_cache.Remove(request.RequestUri.ToString());
return SendRequestWithOutCache(request, accessToken, token);
}

return _cache.GetOrCreateAsync<string>(request.RequestUri.ToString(), async entry =>
{
Expand Down
41 changes: 9 additions & 32 deletions Jellyfin.Plugin.Bangumi/PlaybackScrobbler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,20 @@ public class PlaybackScrobbler : IHostedService
// https://github.com/jellyfin/jellyfin/blob/master/Emby.Server.Implementations/Localization/Ratings/us.csv
private const int RatingNSFW = 10;

private static readonly Dictionary<Guid, HashSet<string>> Store = new();
private readonly BangumiApi _api;
private readonly ILocalizationManager _localizationManager;
private readonly ILogger<PlaybackScrobbler> _log;

private readonly OAuthStore _store;
private readonly IUserDataManager _userDataManager;

public PlaybackScrobbler(IUserManager userManager, IUserDataManager userDataManager, ILocalizationManager localizationManager, OAuthStore store, BangumiApi api, ILogger<PlaybackScrobbler> log)
public PlaybackScrobbler(IUserDataManager userDataManager, ILocalizationManager localizationManager, OAuthStore store, BangumiApi api, ILogger<PlaybackScrobbler> log)
{
_userDataManager = userDataManager;
_localizationManager = localizationManager;
_store = store;
_api = api;
_log = log;

foreach (var userId in userManager.UsersIds)
GetPlaybackHistory(userId);
}

private static PluginConfiguration Configuration => Plugin.Instance!.Configuration;
Expand All @@ -63,28 +59,18 @@ private void OnUserDataSaved(object? sender, UserDataSaveEventArgs e)
switch (e.SaveReason)
{
case UserDataSaveReason.TogglePlayed when e.UserData.Played:
// delay 3 seconds to avoid conflict with playback finished event
Task.Delay(TimeSpan.FromSeconds(3))
.ContinueWith(_ =>
{
GetPlaybackHistory(e.UserId).Add(e.UserData.Key);
_log.LogInformation("mark {Name} (#{Id}) as played for user #{User}", e.Item.Name, e.Item.Id, e.UserId);
}).ConfigureAwait(false);
if (Configuration.ReportManualStatusChangeToBangumi)
ReportPlaybackStatus(e.Item, e.UserId, true).ConfigureAwait(false);
break;

case UserDataSaveReason.TogglePlayed when !e.UserData.Played:
GetPlaybackHistory(e.UserId).Remove(e.UserData.Key);
_log.LogInformation("mark {Name} (#{Id}) as new for user #{User}", e.Item.Name, e.Item.Id, e.UserId);
if (Configuration.ReportManualStatusChangeToBangumi)
ReportPlaybackStatus(e.Item, e.UserId, false).ConfigureAwait(false);
break;

case UserDataSaveReason.PlaybackFinished when e.UserData.Played:
if (Configuration.ReportPlaybackStatusToBangumi)
ReportPlaybackStatus(e.Item, e.UserId, true).ConfigureAwait(false);
e.Keys.ForEach(key => GetPlaybackHistory(e.UserId).Add(key));
break;
}
}
Expand Down Expand Up @@ -136,16 +122,6 @@ private async Task ReportPlaybackStatus(BaseItem item, Guid userId, bool played)
return;
}

if (item.GetUserDataKeys().Intersect(GetPlaybackHistory(userId)).Any())
{
var episodeStatus = await _api.GetEpisodeStatus(user.AccessToken, episodeId, CancellationToken.None);
if (played && episodeStatus is { Type: EpisodeCollectionType.Watched })
{
_log.LogInformation("item {Name} (#{Id}) has been marked as watched before, ignored", item.Name, item.Id);
return;
}
}

try
{
if (item is Book)
Expand Down Expand Up @@ -185,6 +161,14 @@ private async Task ReportPlaybackStatus(BaseItem item, Guid userId, bool played)
return;
}

var episodeStatus = await _api.GetEpisodeStatus(user.AccessToken, episodeId, CancellationToken.None);
if (episodeStatus?.Type == EpisodeCollectionType.Watched)
{
_log.LogInformation("item {Name} (#{Id}) has been marked as watched before, ignored", item.Name,
item.Id);
return;
}

_log.LogInformation("report episode #{Episode} status {Status} to bangumi", episodeId, played ? EpisodeCollectionType.Watched : EpisodeCollectionType.Default);
await _api.UpdateEpisodeStatus(user.AccessToken, subjectId, episodeId, played ? EpisodeCollectionType.Watched : EpisodeCollectionType.Default, CancellationToken.None);
}
Expand Down Expand Up @@ -232,11 +216,4 @@ private async Task ReportPlaybackStatus(BaseItem item, Guid userId, bool played)
}
}
}

private HashSet<string> GetPlaybackHistory(Guid userId)
{
if (!Store.TryGetValue(userId, out var history))
Store[userId] = history = _userDataManager.GetAllUserData(userId).Where(item => item.Played).Select(item => item.Key).ToHashSet();
return history;
}
}

0 comments on commit 3a12126

Please sign in to comment.