diff --git a/Jellyfin.Plugin.Bangumi/BangumiApi.cs b/Jellyfin.Plugin.Bangumi/BangumiApi.cs index 1ad59b8..8e7b7f2 100644 --- a/Jellyfin.Plugin.Bangumi/BangumiApi.cs +++ b/Jellyfin.Plugin.Bangumi/BangumiApi.cs @@ -194,10 +194,15 @@ public async Task> GetSubjectPersonInfos(int id, CancellationTo { return await SendRequest("https://api.bgm.tv/v0/me", accessToken, token); } + + public async Task?> GetEpisodeCollectionInfo(string accessToken, int subjectId, int episodeType, CancellationToken token) + { + return await SendRequest>($"https://api.bgm.tv/v0/users/-/collections/{subjectId}/episodes?episode_type={episodeType}", accessToken, token); + } public async Task UpdateCollectionStatus(string accessToken, int subjectId, CollectionType type, CancellationToken token) { - var request = new HttpRequestMessage(HttpMethod.Patch, $"https://api.bgm.tv/v0/users/-/collections/{subjectId}"); + var request = new HttpRequestMessage(HttpMethod.Post, $"https://api.bgm.tv/v0/users/-/collections/{subjectId}"); request.Content = new JsonContent(new Collection { Type = type }); await SendRequest(request, accessToken, token); } diff --git a/Jellyfin.Plugin.Bangumi/PlaybackScrobbler.cs b/Jellyfin.Plugin.Bangumi/PlaybackScrobbler.cs index 0c29caf..9da1ebd 100644 --- a/Jellyfin.Plugin.Bangumi/PlaybackScrobbler.cs +++ b/Jellyfin.Plugin.Bangumi/PlaybackScrobbler.cs @@ -169,7 +169,7 @@ private async Task ReportPlaybackStatus(BaseItem item, Guid userId, bool played) return; } - _log.LogInformation("report episode #{Episode} status {Status} to bangumi", episodeId, EpisodeCollectionType.Watched); + _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); } @@ -177,7 +177,43 @@ private async Task ReportPlaybackStatus(BaseItem item, Guid userId, bool played) } catch (Exception e) { - _log.LogError(e, "report playback status failed"); + if (played && e.Message == "Bad Request: you need to add subject to your collection first") + { + _log.LogInformation("report subject #{Subject} status {Status} to bangumi", subjectId, CollectionType.Watching); + await _api.UpdateCollectionStatus(user.AccessToken, subjectId, CollectionType.Watching, CancellationToken.None); + + _log.LogInformation("report episode #{Episode} status {Status} to bangumi", episodeId, EpisodeCollectionType.Watched); + await _api.UpdateEpisodeStatus(user.AccessToken, subjectId, episodeId, played ? EpisodeCollectionType.Watched : EpisodeCollectionType.Default, CancellationToken.None); + } + else + { + _log.LogError(e, "report playback status failed"); + } + } + + // report subject status watched + if (played && item is not Book) + { + // skip if episode type not normal + var episode = await _api.GetEpisode(episodeId, CancellationToken.None); + if (episode is {Type: EpisodeType.Normal}) + { + // check each episode status + var epList = await _api.GetEpisodeCollectionInfo(user.AccessToken, subjectId, (int)EpisodeType.Normal, CancellationToken.None); + if (epList is {Total: > 0}) + { + var subjectPlayed = true; + epList.Data.ForEach(ep => + { + if (ep.Type != EpisodeCollectionType.Watched) subjectPlayed = false; + }); + if (subjectPlayed) + { + _log.LogInformation("report subject #{Subject} status {Status} to bangumi", subjectId, CollectionType.Watched); + await _api.UpdateCollectionStatus(user.AccessToken, subjectId, CollectionType.Watched, CancellationToken.None); + } + } + } } }