Skip to main content

bpi_rs/creativecenter/season/
action.rs

1// 创建合集 API
2//
3// [参考文档](https://github.com/Yuelioi/bilibili-API-collect/tree/cfc5fddcc8a94b74d91970bb5b4eaeb349addc47/docs/creativecenter/season.md)
4
5use crate::BilibiliRequest;
6use crate::BpiResult;
7use crate::creativecenter::CreativeCenterClient;
8use serde::{Deserialize, Serialize};
9use serde_json::json;
10
11/// 合集视频条目(添加用)
12
13#[derive(Debug, Clone, Serialize, Deserialize, Default)]
14pub struct EpisodeAdd {
15    pub title: String,
16    pub aid: u64,
17    pub cid: u64,
18
19    #[serde(default)]
20    pub charging_pay: i64,
21    #[serde(default)]
22    pub member_first: i64,
23    #[serde(default)]
24    pub limited_free: bool,
25}
26
27impl<'a> CreativeCenterClient<'a> {
28    /// 创建合集
29    ///
30    /// 创建一个新的视频合集,需要提供标题、封面等信息。
31    ///
32    /// # 参数
33    /// | 名称 | 类型 | 说明 |
34    /// | ---- | ---- | ---- |
35    /// | `title` | &str | 合集标题 |
36    /// | `desc` | `Option<&str>` | 合集简介,可选 |
37    /// | `cover` | &str | 封面图 URL(从上传接口获取) |
38    /// | `season_price` | `Option<u32>` | 合集价格,可选,默认 0 |
39    ///
40    /// # 文档
41    /// [创建合集](https://github.com/Yuelioi/bilibili-API-collect/tree/cfc5fddcc8a94b74d91970bb5b4eaeb349addc47/docs/creativecenter/season/create.md#创建合集)
42    pub async fn season_create(
43        &self,
44        title: &str,
45        desc: Option<&str>,
46        cover: &str,
47        season_price: Option<u32>,
48    ) -> BpiResult<u64> {
49        // 校验 csrf
50        let csrf = self.client.csrf()?;
51
52        let mut form = vec![
53            ("title", title.to_string()),
54            ("cover", cover.to_string()),
55            ("csrf", csrf),
56        ];
57
58        if let Some(d) = desc {
59            form.push(("desc", d.to_string()));
60        }
61        if let Some(price) = season_price {
62            form.push(("season_price", price.to_string()));
63        }
64
65        self.client
66            .post("https://member.bilibili.com/x2/creative/web/season/add")
67            .form(&form)
68            .send_bpi_payload("creativecenter.season.create")
69            .await
70    }
71
72    /// 删除合集
73    ///
74    /// 删除指定的合集,需要提供合集 ID。
75    ///
76    /// # 参数
77    /// | 名称 | 类型 | 说明 |
78    /// | ---- | ---- | ---- |
79    /// | `season_id` | u64 | 合集 ID |
80    ///
81    /// # 文档
82    /// [删除合集](https://github.com/Yuelioi/bilibili-API-collect/tree/cfc5fddcc8a94b74d91970bb5b4eaeb349addc47/docs/creativecenter/season/del.md#删除合集)
83    pub async fn season_delete(&self, season_id: u64) -> BpiResult<Option<serde_json::Value>> {
84        let csrf = self.client.csrf()?;
85
86        let form = vec![("id", season_id.to_string()), ("csrf", csrf)];
87
88        self.client
89            .post("https://member.bilibili.com/x2/creative/web/season/del")
90            .form(&form)
91            .send_bpi_optional_payload("creativecenter.season.delete")
92            .await
93    }
94
95    /// 添加视频到合集
96    ///
97    /// 将视频添加到指定的合集小节中。
98    ///
99    /// # 参数
100    /// | 名称 | 类型 | 说明 |
101    /// | ---- | ---- | ---- |
102    /// | `section_id` | u64 | 合集小节 ID |
103    /// | `episodes` | `Vec<EpisodeAdd>` | 视频列表 |
104    ///
105    /// # 文档
106    /// [添加视频到合集](https://github.com/Yuelioi/bilibili-API-collect/tree/cfc5fddcc8a94b74d91970bb5b4eaeb349addc47/docs/creativecenter/season/push.md#添加视频到合集)
107    pub async fn season_episodes_add(
108        &self,
109        section_id: u64,
110        episodes: Vec<EpisodeAdd>,
111    ) -> BpiResult<Option<serde_json::Value>> {
112        // 校验 csrf
113        let csrf = self.client.csrf()?;
114
115        let payload = json!({
116            "sectionId": section_id,
117            "episodes": episodes
118        });
119
120        self.client
121            .post("https://member.bilibili.com/x2/creative/web/season/section/episodes/add")
122            .with_bilibili_headers()
123            .query(&[("csrf", csrf)])
124            .json(&payload)
125            .send_bpi_optional_payload("creativecenter.season.episodes.add")
126            .await
127    }
128}
129
130#[cfg(test)]
131mod tests {}