From f3012cce06b41df9b0237a7e6118d82f06cb1b69 Mon Sep 17 00:00:00 2001 From: fio Date: Mon, 8 May 2023 19:03:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feature:=20=E8=AE=BE=E7=BD=AE=E8=8A=82?= =?UTF-8?q?=E7=9B=AE=E5=88=B0'=E5=9C=A8=E7=9C=8B'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jellyfin.Plugin.Bangumi/BangumiApi.cs | 2 +- Jellyfin.Plugin.Bangumi/PlaybackScrobbler.cs | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Jellyfin.Plugin.Bangumi/BangumiApi.cs b/Jellyfin.Plugin.Bangumi/BangumiApi.cs index 02f0a8e..791df9c 100644 --- a/Jellyfin.Plugin.Bangumi/BangumiApi.cs +++ b/Jellyfin.Plugin.Bangumi/BangumiApi.cs @@ -197,7 +197,7 @@ public async Task> GetSubjectPersonInfos(int id, CancellationTo 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 3be9ee6..4ea4bc6 100644 --- a/Jellyfin.Plugin.Bangumi/PlaybackScrobbler.cs +++ b/Jellyfin.Plugin.Bangumi/PlaybackScrobbler.cs @@ -165,7 +165,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); } @@ -173,7 +173,18 @@ 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"); + } } } From 525e72f3db844212f701969543a0601a8461a848 Mon Sep 17 00:00:00 2001 From: fio Date: Mon, 8 May 2023 19:05:52 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feature:=20=E8=AE=BE=E7=BD=AE=E8=8A=82?= =?UTF-8?q?=E7=9B=AE=E5=88=B0'=E7=9C=8B=E8=BF=87'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jellyfin.Plugin.Bangumi/BangumiApi.cs | 5 ++++ Jellyfin.Plugin.Bangumi/PlaybackScrobbler.cs | 25 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/Jellyfin.Plugin.Bangumi/BangumiApi.cs b/Jellyfin.Plugin.Bangumi/BangumiApi.cs index 791df9c..6b21aa7 100644 --- a/Jellyfin.Plugin.Bangumi/BangumiApi.cs +++ b/Jellyfin.Plugin.Bangumi/BangumiApi.cs @@ -194,6 +194,11 @@ 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) { diff --git a/Jellyfin.Plugin.Bangumi/PlaybackScrobbler.cs b/Jellyfin.Plugin.Bangumi/PlaybackScrobbler.cs index 4ea4bc6..e26f9d1 100644 --- a/Jellyfin.Plugin.Bangumi/PlaybackScrobbler.cs +++ b/Jellyfin.Plugin.Bangumi/PlaybackScrobbler.cs @@ -186,6 +186,31 @@ private async Task ReportPlaybackStatus(BaseItem item, Guid userId, bool played) _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); + } + } + } + } } private HashSet GetPlaybackHistory(Guid userId)