1use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
5use serde::{ Deserialize, Serialize };
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct BangumiFollowResult {
9 pub fmid: i64,
10 pub relation: bool,
11 pub status: i32,
12 pub toast: String,
13}
14
15impl BpiClient {
16 pub async fn bangumi_follow(
24 &self,
25 season_id: u64
26 ) -> Result<BpiResponse<BangumiFollowResult>, BpiError> {
27 let csrf = self.csrf()?;
28 self
29 .post("https://api.bilibili.com/pgc/web/follow/add")
30 .with_bilibili_headers()
31 .form(
32 &[
33 ("season_id", season_id.to_string()),
34 ("csrf", csrf.to_string()),
35 ]
36 )
37 .send_bpi("追番").await
38 }
39
40 pub async fn bangumi_unfollow(
47 &self,
48 season_id: u64
49 ) -> Result<BpiResponse<BangumiFollowResult>, BpiError> {
50 let csrf = self.csrf()?;
51 self
52 .post("https://api.bilibili.com/pgc/web/follow/del")
53 .with_bilibili_headers()
54 .form(
55 &[
56 ("season_id", season_id.to_string()),
57 ("csrf", csrf.to_string()),
58 ]
59 )
60 .send_bpi("取消追番").await
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 const TEST_BANGUMI_ID: u64 = 99644; #[tokio::test]
70 async fn test_follow_bangumi() -> Result<(), Box<BpiError>> {
71 let bpi = BpiClient::new();
72 let result = bpi.bangumi_follow(TEST_BANGUMI_ID).await?;
73
74 let data = result.into_data()?;
75 tracing::info!("{:#?}", data);
76
77 assert_eq!(data.toast, "自己追的番就要好好看完哟^o^");
78
79 Ok(())
80 }
81
82 #[tokio::test]
83 async fn test_unfollow_bangumi() -> Result<(), Box<BpiError>> {
84 let bpi = BpiClient::new();
85 let result = bpi.bangumi_unfollow(TEST_BANGUMI_ID).await?;
86
87 let data = result.into_data()?;
88 tracing::info!("{:#?}", data);
89
90 assert_eq!(data.toast, "已取消追番");
91
92 Ok(())
93 }
94}