1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use super::info::FavFolderInfo;
use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
impl BpiClient {
/// 新建收藏夹
///
/// # 文档
/// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/fav)
///
/// # 参数
///
/// | 名称 | 类型 | 说明 |
/// | ---- | ---- | ---- |
/// | `title` | &str | 收藏夹标题 |
/// | `intro` | `Option<&str>` | 介绍 |
/// | `privacy` | `Option<u8>` | 0 公开,1 私密 |
/// | `cover` | `Option<&str>` | 封面 URL |
pub async fn fav_folder_add(
&self,
title: &str,
intro: Option<&str>,
privacy: Option<u8>,
cover: Option<&str>
) -> Result<BpiResponse<FavFolderInfo>, BpiError> {
let csrf = self.csrf()?;
let mut form = vec![("title", title.to_string()), ("csrf", csrf)];
if let Some(intro) = intro {
form.push(("intro", intro.to_string()));
}
if let Some(privacy) = privacy {
form.push(("privacy", privacy.to_string()));
}
if let Some(cover) = cover {
form.push(("cover", cover.to_string()));
}
self
.post("https://api.bilibili.com/x/v3/fav/folder/add")
.form(&form)
.send_bpi("新建收藏夹").await
}
/// 修改收藏夹
///
/// # 文档
/// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/fav)
///
/// # 参数
///
/// | 名称 | 类型 | 说明 |
/// | ---- | ---- | ---- |
/// | `media_id` | u64 | 收藏夹 media_id |
/// | `title` | &str | 标题 |
/// | `intro` | `Option<&str>` | 介绍 |
/// | `privacy` | `Option<u8>` | 0 公开,1 私密 |
/// | `cover` | `Option<&str>` | 封面 URL |
pub async fn fav_folder_edit(
&self,
media_id: u64,
title: &str,
intro: Option<&str>,
privacy: Option<u8>,
cover: Option<&str>
) -> Result<BpiResponse<FavFolderInfo>, BpiError> {
let csrf = self.csrf()?;
let mut form = vec![
("media_id", media_id.to_string()),
("title", title.to_string()),
("csrf", csrf)
];
if let Some(intro) = intro {
form.push(("intro", intro.to_string()));
}
if let Some(privacy) = privacy {
form.push(("privacy", privacy.to_string()));
}
if let Some(cover) = cover {
form.push(("cover", cover.to_string()));
}
self
.post("https://api.bilibili.com/x/v3/fav/folder/edit")
.form(&form)
.send_bpi("修改收藏夹").await
}
/// 删除收藏夹
///
/// # 文档
/// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/fav)
///
/// # 参数
///
/// | 名称 | 类型 | 说明 |
/// | ---- | ---- | ---- |
/// | `media_ids` | &`[u64]` | 多个收藏夹 media_id |
pub async fn fav_folder_del(&self, media_ids: &[u64]) -> Result<BpiResponse<i32>, BpiError> {
let csrf = self.csrf()?;
let ids_str = media_ids
.iter()
.map(|&id| id.to_string())
.collect::<Vec<_>>()
.join(",");
let form = [
("media_ids", ids_str),
("csrf", csrf),
];
self
.post("https://api.bilibili.com/x/v3/fav/folder/del")
.form(&form)
.send_bpi("删除收藏夹").await
}
/// 批量复制内容
/// `resources`: "{内容id}:{内容类型},..."
///
/// # 文档
/// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/fav)
///
/// # 参数
///
/// | 名称 | 类型 | 说明 |
/// | ---- | ---- | ---- |
/// | `src_media_id` | u64 | 源收藏夹 media_id |
/// | `tar_media_id` | u64 | 目标收藏夹 media_id |
/// | `mid` | u64 | 用户 mid |
/// | `resources` | &str | 形如 "{内容id}:{内容类型},..." |
pub async fn fav_resource_copy(
&self,
src_media_id: u64,
tar_media_id: u64,
mid: u64,
resources: &str
) -> Result<BpiResponse<i32>, BpiError> {
let csrf = self.csrf()?;
let form = [
("src_media_id", src_media_id.to_string()),
("tar_media_id", tar_media_id.to_string()),
("mid", mid.to_string()),
("resources", resources.to_string()),
("platform", "web".to_string()),
("csrf", csrf),
];
self
.post("https://api.bilibili.com/x/v3/fav/resource/copy")
.form(&form)
.send_bpi("批量复制内容").await
}
/// 批量移动内容
/// `resources`: "{内容id}:{内容类型},..."
///
/// # 文档
/// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/fav)
///
/// # 参数
///
/// | 名称 | 类型 | 说明 |
/// | ---- | ---- | ---- |
/// | `src_media_id` | u64 | 源收藏夹 media_id |
/// | `tar_media_id` | u64 | 目标收藏夹 media_id |
/// | `mid` | u64 | 用户 mid |
/// | `resources` | &str | 形如 "{内容id}:{内容类型},..." |
pub async fn fav_resource_move(
&self,
src_media_id: u64,
tar_media_id: u64,
mid: u64,
resources: &str
) -> Result<BpiResponse<i32>, BpiError> {
let csrf = self.csrf()?;
let form = [
("src_media_id", src_media_id.to_string()),
("tar_media_id", tar_media_id.to_string()),
("mid", mid.to_string()),
("resources", resources.to_string()),
("platform", "web".to_string()),
("csrf", csrf),
];
self
.post("https://api.bilibili.com/x/v3/fav/resource/move")
.form(&form)
.send_bpi("批量移动内容").await
}
/// 批量删除内容
/// `resources`: "{内容id}:{内容类型},..."
///
/// # 文档
/// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/fav)
///
/// # 参数
///
/// | 名称 | 类型 | 说明 |
/// | ---- | ---- | ---- |
/// | `media_id` | u64 | 收藏夹 media_id |
/// | `resources` | &str | 形如 "{内容id}:{内容类型},..." |
pub async fn fav_resource_batch_del(
&self,
media_id: u64,
resources: &str
) -> Result<BpiResponse<i32>, BpiError> {
let csrf = self.csrf()?;
let form = [
("media_id", media_id.to_string()),
("resources", resources.to_string()),
("platform", "web".to_string()),
("csrf", csrf),
];
self
.post("https://api.bilibili.com/x/v3/fav/resource/batch-del")
.form(&form)
.send_bpi("批量删除内容").await
}
/// 清空所有失效内容
///
/// # 文档
/// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/fav)
///
/// # 参数
///
/// | 名称 | 类型 | 说明 |
/// | ---- | ---- | ---- |
/// | `media_id` | u64 | 收藏夹 media_id |
pub async fn fav_resource_clean(&self, media_id: u64) -> Result<BpiResponse<i32>, BpiError> {
let csrf = self.csrf()?;
let form = [
("media_id", media_id.to_string()),
("csrf", csrf),
];
self
.post("https://api.bilibili.com/x/v3/fav/resource/clean")
.form(&form)
.send_bpi("清空所有失效内容").await
}
}
#[cfg(test)]
mod tests {
use super::*;
use tracing::info;
#[tokio::test]
async fn test_fav_folder_add_and_del() {
let bpi = BpiClient::new();
let title = "Test_Fav_Folder_Add2";
let intro = "This is a test folder2.";
// 1. 新建一个私密收藏夹
let add_resp = bpi.fav_folder_add(title, Some(intro), Some(1), None).await;
info!("Add folder result: {:?}", add_resp);
assert!(add_resp.is_ok());
let new_folder_id = add_resp.unwrap().data.unwrap().id;
info!("New folder ID: {}", new_folder_id);
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
// 2. 删除新建的收藏夹
let del_resp = bpi.fav_folder_del(&[new_folder_id]).await;
info!("Delete folder result: {:?}", del_resp);
assert!(del_resp.is_ok());
}
#[tokio::test]
async fn test_fav_folder_edit() {
let bpi = BpiClient::new();
// 替换为你的测试收藏夹ID
let media_id = 3717139570;
let title = "Edited Title";
let intro = "Edited Intro";
let resp = bpi.fav_folder_edit(media_id, title, Some(intro), Some(0), None).await;
info!("Edit folder result: {:?}", resp);
assert!(resp.is_ok());
}
#[tokio::test]
async fn test_fav_resource_operations() {
let bpi = BpiClient::new();
// 替换为你的源/目标收藏夹ID和资源ID
let src_media_id = 3717139570;
let tar_media_id = 3641682570;
let mid = 4279370;
let resources_copy = "115087859779103:2";
let resources_del = "442608504:2";
let resources_move = "739661210:2";
// 1. 批量复制
let copy_resp = bpi.fav_resource_copy(
src_media_id,
tar_media_id,
mid,
resources_copy
).await;
info!("Copy resources result: {:?}", copy_resp);
assert!(copy_resp.is_ok());
// 2. 批量移动
let move_resp = bpi.fav_resource_move(
src_media_id,
tar_media_id,
mid,
resources_move
).await;
info!("Move resources result: {:?}", move_resp);
assert!(move_resp.is_ok());
// 3. 批量删除
let del_resp = bpi.fav_resource_batch_del(tar_media_id, resources_del).await;
info!("Batch delete resources result: {:?}", del_resp);
assert!(del_resp.is_ok());
}
#[tokio::test]
async fn test_fav_resource_clean() {
let bpi = BpiClient::new();
// 替换为你的测试收藏夹ID
let media_id = 3717139570;
let resp = bpi.fav_resource_clean(media_id).await;
info!("Clean invalid resources result: {:?}", resp);
assert!(resp.is_ok());
}
}