Skip to content

Commit

Permalink
Merge pull request #66 from fiochen/master
Browse files Browse the repository at this point in the history
feat: 设置节目到'在看/看过'
  • Loading branch information
kookxiang authored May 16, 2023
2 parents 1137398 + 525e72f commit fdd4752
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
7 changes: 6 additions & 1 deletion Jellyfin.Plugin.Bangumi/BangumiApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,15 @@ public async Task<List<PersonInfo>> GetSubjectPersonInfos(int id, CancellationTo
{
return await SendRequest<User>("https://api.bgm.tv/v0/me", accessToken, token);
}

public async Task<DataList<EpisodeCollectionInfo>?> GetEpisodeCollectionInfo(string accessToken, int subjectId, int episodeType, CancellationToken token)
{
return await SendRequest<DataList<EpisodeCollectionInfo>>($"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);
}
Expand Down
40 changes: 38 additions & 2 deletions Jellyfin.Plugin.Bangumi/PlaybackScrobbler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,51 @@ 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);
}

_log.LogInformation("report completed");
}
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);
}
}
}
}
}

Expand Down

0 comments on commit fdd4752

Please sign in to comment.