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
use serde_json::{Map, Value};
use crate::error::AppError;
use super::super::{
danmaku::parse_dm_seg_mobile_reply,
sign::{av_to_bv, bv_to_av},
types::{
BilibiliArticleCards, BilibiliArticleContent, BilibiliArticleInfo, BilibiliArticleListInfo,
BilibiliAvToBv, BilibiliAvToBvData, BilibiliBangumiInfo, BilibiliBangumiStream,
BilibiliBvToAv, BilibiliBvToAvData, BilibiliCaptchaFromVoucher, BilibiliDanmakuData,
BilibiliDanmakuList, BilibiliEmojiList, BilibiliValidateCaptcha, BilibiliVideoInfo,
BilibiliVideoStream,
},
};
use super::{BilibiliFetcher, requests};
impl BilibiliFetcher {
/// Fetch one Bilibili video payload.
///
/// # Errors
///
/// Returns an error when the upstream request fails or the response body
/// contains a non-zero Bilibili API status code.
#[doc(alias = "fetchVideoInfo")]
pub async fn fetch_video_info(&self, bvid: &str) -> Result<BilibiliVideoInfo, AppError> {
self.fetch_json(&requests::video_info(self.api_base_url.as_ref(), bvid)?)
.await
}
/// Fetch stream URLs for one Bilibili video.
///
/// # Errors
///
/// Returns an error when the upstream request fails, the WBI signature
/// cannot be derived for authenticated requests, or the response body
/// contains a non-zero Bilibili API status code.
#[doc(alias = "fetchVideoStream")]
#[doc(alias = "fetchVideoStreamUrl")]
pub async fn fetch_video_stream(
&self,
aid: u64,
cid: u64,
) -> Result<BilibiliVideoStream, AppError> {
let url = self
.build_playurl_request(&requests::video_stream(
self.api_base_url.as_ref(),
aid,
cid,
)?)
.await?;
self.fetch_json(&url).await
}
/// Fetch one Bilibili danmaku segment and decode its protobuf payload.
///
/// # Errors
///
/// Returns an error when the upstream request fails or the protobuf body
/// cannot be decoded.
#[doc(alias = "fetchVideoDanmaku")]
pub async fn fetch_video_danmaku(
&self,
cid: u64,
segment_index: Option<u32>,
) -> Result<BilibiliDanmakuList, AppError> {
let url = requests::video_danmaku(self.api_base_url.as_ref(), cid, segment_index)?;
let bytes = self.send_bytes_request(&url, None).await?;
let elems = parse_dm_seg_mobile_reply(&bytes)?;
Ok(BilibiliDanmakuList {
code: 0,
message: "success".to_owned(),
ttl: None,
data: BilibiliDanmakuData {
elems: elems.clone(),
},
upstream_payload: serde_json::to_value(BilibiliDanmakuData { elems })
.unwrap_or(Value::Null),
})
}
/// Fetch the Bilibili emoji catalog.
///
/// # Errors
///
/// Returns an error when the upstream request fails or the response body
/// contains a non-zero Bilibili API status code.
#[doc(alias = "fetchEmojiList")]
pub async fn fetch_emoji_list(&self) -> Result<BilibiliEmojiList, AppError> {
self.fetch_json(&requests::emoji_list(self.api_base_url.as_ref())?)
.await
}
/// Fetch the content payload for one Bilibili article.
///
/// # Errors
///
/// Returns an error when the upstream request fails or the response body
/// contains a non-zero Bilibili API status code.
#[doc(alias = "fetchArticleContent")]
pub async fn fetch_article_content(
&self,
article_id: &str,
) -> Result<BilibiliArticleContent, AppError> {
self.fetch_json(&requests::article_content(
self.api_base_url.as_ref(),
article_id,
)?)
.await
}
/// Fetch display-card payloads for a list of Bilibili article-related ids.
///
/// # Errors
///
/// Returns an error when no ids are provided, the upstream request fails,
/// or the response body contains a non-zero Bilibili API status code.
#[doc(alias = "fetchArticleCards")]
pub async fn fetch_article_cards<I, S>(&self, ids: I) -> Result<BilibiliArticleCards, AppError>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let joined_ids = ids
.into_iter()
.map(|value| value.as_ref().to_owned())
.filter(|value| !value.is_empty())
.collect::<Vec<_>>()
.join(",");
if joined_ids.is_empty() {
return Err(AppError::InvalidRequestConfig(
"bilibili article cards requires at least one id".to_owned(),
));
}
self.fetch_json(&requests::article_cards(
self.api_base_url.as_ref(),
&joined_ids,
)?)
.await
}
/// Fetch metadata for one Bilibili article.
///
/// # Errors
///
/// Returns an error when the upstream request fails or the response body
/// contains a non-zero Bilibili API status code.
#[doc(alias = "fetchArticleInfo")]
pub async fn fetch_article_info(
&self,
article_id: &str,
) -> Result<BilibiliArticleInfo, AppError> {
self.fetch_json(&requests::article_info(
self.api_base_url.as_ref(),
article_id,
)?)
.await
}
/// Fetch metadata for one Bilibili article collection.
///
/// # Errors
///
/// Returns an error when the upstream request fails or the response body
/// contains a non-zero Bilibili API status code.
#[doc(alias = "fetchArticleListInfo")]
pub async fn fetch_article_list_info(
&self,
list_id: &str,
) -> Result<BilibiliArticleListInfo, AppError> {
self.fetch_json(&requests::article_list_info(
self.api_base_url.as_ref(),
list_id,
)?)
.await
}
/// Fetch metadata for one Bilibili bangumi season or episode.
///
/// The input follows the TypeScript implementation and accepts either an
/// `ep...` identifier or an `ss...` identifier. Bare numeric ids are
/// treated as `season_id`.
///
/// # Errors
///
/// Returns an error when the upstream request fails or the response body
/// contains a non-zero Bilibili API status code.
#[doc(alias = "fetchBangumiInfo")]
pub async fn fetch_bangumi_info(
&self,
bangumi_id: &str,
) -> Result<BilibiliBangumiInfo, AppError> {
self.fetch_json(&requests::bangumi_info(
self.api_base_url.as_ref(),
bangumi_id,
)?)
.await
}
/// Fetch stream URLs for one Bilibili bangumi episode.
///
/// # Errors
///
/// Returns an error when the upstream request fails, the WBI signature
/// cannot be derived for authenticated requests, or the response body
/// contains a non-zero Bilibili API status code.
#[doc(alias = "fetchBangumiStream")]
#[doc(alias = "fetchBangumiStreamUrl")]
pub async fn fetch_bangumi_stream(
&self,
ep_id: &str,
cid: u64,
) -> Result<BilibiliBangumiStream, AppError> {
let url = self
.build_playurl_request(&requests::bangumi_stream(
self.api_base_url.as_ref(),
cid,
ep_id,
)?)
.await?;
self.fetch_json(&url).await
}
/// Request a captcha challenge from one Bilibili `v_voucher`.
///
/// # Errors
///
/// Returns an error when the upstream request fails or the response body
/// contains a non-zero Bilibili API status code.
#[doc(alias = "requestCaptchaFromVoucher")]
pub async fn request_captcha_from_voucher(
&self,
v_voucher: &str,
csrf: Option<&str>,
) -> Result<BilibiliCaptchaFromVoucher, AppError> {
let url = requests::captcha_from_voucher(self.api_base_url.as_ref())?;
let mut body =
Map::from_iter([("v_voucher".to_owned(), Value::String(v_voucher.to_owned()))]);
if let Some(csrf) = csrf.filter(|value| !value.is_empty()) {
body.insert("csrf".to_owned(), Value::String(csrf.to_owned()));
}
self.post_json(&url, &Value::Object(body)).await
}
/// Validate a Bilibili captcha challenge result.
///
/// # Errors
///
/// Returns an error when the upstream request fails or the response body
/// contains a non-zero Bilibili API status code.
#[doc(alias = "validateCaptchaResult")]
pub async fn validate_captcha_result(
&self,
challenge: &str,
token: &str,
validate: &str,
seccode: &str,
csrf: Option<&str>,
) -> Result<BilibiliValidateCaptcha, AppError> {
let url = requests::validate_captcha(self.api_base_url.as_ref())?;
let mut body = Map::from_iter([
("challenge".to_owned(), Value::String(challenge.to_owned())),
("token".to_owned(), Value::String(token.to_owned())),
("validate".to_owned(), Value::String(validate.to_owned())),
("seccode".to_owned(), Value::String(seccode.to_owned())),
]);
if let Some(csrf) = csrf.filter(|value| !value.is_empty()) {
body.insert("csrf".to_owned(), Value::String(csrf.to_owned()));
}
self.post_json(&url, &Value::Object(body)).await
}
/// Convert a numeric AV identifier into its BV representation.
#[doc(alias = "convertAvToBv")]
pub fn convert_av_to_bv(&self, aid: u64) -> BilibiliAvToBv {
let bvid = av_to_bv(aid);
BilibiliAvToBv {
code: 0,
message: "success".to_owned(),
data: BilibiliAvToBvData { bvid: bvid.clone() },
upstream_payload: Value::Object(Map::from_iter([(
"bvid".to_owned(),
Value::String(bvid),
)])),
}
}
/// Convert a BV identifier into its AV representation.
///
/// # Errors
///
/// Returns an error when the provided BV identifier is invalid.
#[doc(alias = "convertBvToAv")]
pub fn convert_bv_to_av(&self, bvid: &str) -> Result<BilibiliBvToAv, AppError> {
let aid = format!("av{}", bv_to_av(bvid)?);
Ok(BilibiliBvToAv {
code: 0,
message: "success".to_owned(),
data: BilibiliBvToAvData { aid: aid.clone() },
upstream_payload: Value::Object(Map::from_iter([(
"aid".to_owned(),
Value::String(aid),
)])),
})
}
}