bpi-rs 0.2.1

Bilibili API client library for Rust
Documentation
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
use crate::request::BilibiliRequest;
use crate::{BpiClient, BpiResult};

use super::collection::{
    GetSeasonsArchivesData, GetSeasonsSeriesData, GetSeriesArchivesData, GetSeriesData,
    HOME_SEASONS_SERIES_ENDPOINT, SEASONS_ARCHIVES_LIST_ENDPOINT, SEASONS_SERIES_LIST_ENDPOINT,
    SERIES_ARCHIVES_ENDPOINT, SERIES_INFO_ENDPOINT, VideoCollectionHomeSeasonsSeriesParams,
    VideoCollectionSeasonsArchivesParams, VideoCollectionSeasonsSeriesParams,
    VideoCollectionSeriesArchivesParams, VideoCollectionSeriesInfoParams,
};
use super::interact_video::{INTERACTIVE_INFO_ENDPOINT, InteractiveVideoInfoResponseData};
use super::model::{VideoDetail, VideoPage, VideoView};
use super::online::{ONLINE_TOTAL_ENDPOINT, OnlineTotalResponseData};
use super::params::{
    InteractiveVideoInfoParams, VideoAiSummaryParams, VideoDescParams, VideoDetailParams,
    VideoHomepageRecommendationsParams, VideoOnlineTotalParams, VideoPageListParams,
    VideoPlayUrlParams, VideoPlayerInfoParams, VideoRelatedParams, VideoTagsParams,
    VideoViewParams,
};
use super::player::{PLAYER_INFO_V2_ENDPOINT, PlayerInfoResponseData};
use super::recommend::{
    HOMEPAGE_RECOMMENDATIONS_ENDPOINT, RELATED_VIDEOS_ENDPOINT, RcmdFeedResponseData, RelatedVideo,
};
use super::summary::{AI_SUMMARY_ENDPOINT, AiSummaryResponseData};
use super::tags::{TAGS_ENDPOINT, VideoTag};
use super::videostream_url::{PLAY_URL_ENDPOINT, PlayUrlResponseData};

const DESC_ENDPOINT: &str = "https://api.bilibili.com/x/web-interface/archive/desc";
const DETAIL_ENDPOINT: &str = "https://api.bilibili.com/x/web-interface/view/detail";
const PAGELIST_ENDPOINT: &str = "https://api.bilibili.com/x/player/pagelist";
const VIEW_ENDPOINT: &str = "https://api.bilibili.com/x/web-interface/view";

/// 视频领域 API 客户端。
#[derive(Clone, Copy)]
pub struct VideoClient<'a> {
    pub(crate) client: &'a BpiClient,
}

impl<'a> VideoClient<'a> {
    pub(crate) fn new(client: &'a BpiClient) -> Self {
        Self { client }
    }

    #[cfg(test)]
    pub(crate) fn endpoint(&self) -> &'static str {
        VIEW_ENDPOINT
    }

    #[cfg(test)]
    pub(crate) fn detail_endpoint(&self) -> &'static str {
        DETAIL_ENDPOINT
    }

    #[cfg(test)]
    pub(crate) fn page_list_endpoint(&self) -> &'static str {
        PAGELIST_ENDPOINT
    }

    #[cfg(test)]
    pub(crate) fn desc_endpoint(&self) -> &'static str {
        DESC_ENDPOINT
    }

    /// 按 AV ID 或 BV ID 获取 Web 视频详情。
    pub async fn view(&self, params: VideoViewParams) -> BpiResult<VideoView> {
        self.client
            .get(VIEW_ENDPOINT)
            .query(&params.query_pairs())
            .send_bpi_payload("video.view")
            .await
    }

    /// 获取 Web 视频详情,包括标签和相关视频。
    pub async fn detail(&self, params: VideoDetailParams) -> BpiResult<VideoDetail> {
        self.client
            .get(DETAIL_ENDPOINT)
            .query(&params.query_pairs())
            .send_bpi_payload("video.detail")
            .await
    }

    /// 获取视频的分 P/内容 ID。
    pub async fn page_list(&self, params: VideoPageListParams) -> BpiResult<Vec<VideoPage>> {
        self.client
            .get(PAGELIST_ENDPOINT)
            .query(&params.query_pairs())
            .send_bpi_payload("video.pagelist")
            .await
    }

    /// 获取纯文本视频简介。
    pub async fn desc(&self, params: VideoDescParams) -> BpiResult<String> {
        self.client
            .get(DESC_ENDPOINT)
            .query(&params.query_pairs())
            .send_bpi_payload("video.desc")
            .await
    }

    /// 按 AV ID 或 BV ID 以及分 P/内容 ID 获取已签名的 Web 播放 URL。
    pub async fn play_url(&self, params: VideoPlayUrlParams) -> BpiResult<PlayUrlResponseData> {
        let params = self.client.get_wbi_sign2(params.query_pairs()).await?;

        self.client
            .get(PLAY_URL_ENDPOINT)
            .with_bilibili_headers()
            .query(&params)
            .send_bpi_payload("video.play_url")
            .await
    }

    /// 获取指定视频合集中的视频。
    pub async fn seasons_archives_list(
        &self,
        params: VideoCollectionSeasonsArchivesParams,
    ) -> BpiResult<GetSeasonsArchivesData> {
        let params = self.client.get_wbi_sign2(params.query_pairs()).await?;

        self.client
            .get(SEASONS_ARCHIVES_LIST_ENDPOINT)
            .with_bilibili_headers()
            .query(&params)
            .send_bpi_payload("video.collection.seasons_archives_list")
            .await
    }

    /// 获取用户主页的合集和系列列表。
    pub async fn home_seasons_series(
        &self,
        params: VideoCollectionHomeSeasonsSeriesParams,
    ) -> BpiResult<GetSeasonsSeriesData> {
        let params = self.client.get_wbi_sign2(params.query_pairs()).await?;

        self.client
            .get(HOME_SEASONS_SERIES_ENDPOINT)
            .query(&params)
            .send_bpi_payload("video.collection.home_seasons_series")
            .await
    }

    /// 分页获取用户的合集和系列列表。
    pub async fn seasons_series_list(
        &self,
        params: VideoCollectionSeasonsSeriesParams,
    ) -> BpiResult<GetSeasonsSeriesData> {
        let params = self.client.get_wbi_sign2(params.query_pairs()).await?;

        self.client
            .get(SEASONS_SERIES_LIST_ENDPOINT)
            .query(&params)
            .send_bpi_payload("video.collection.seasons_series_list")
            .await
    }

    /// 获取指定视频系列的元数据。
    pub async fn series_info(
        &self,
        params: VideoCollectionSeriesInfoParams,
    ) -> BpiResult<GetSeriesData> {
        self.client
            .get(SERIES_INFO_ENDPOINT)
            .query(&params.query_pairs())
            .send_bpi_payload("video.collection.series_info")
            .await
    }

    /// 获取指定视频系列中的视频。
    pub async fn series_archives(
        &self,
        params: VideoCollectionSeriesArchivesParams,
    ) -> BpiResult<GetSeriesArchivesData> {
        self.client
            .get(SERIES_ARCHIVES_ENDPOINT)
            .query(&params.query_pairs())
            .send_bpi_payload("video.collection.series_archives")
            .await
    }

    /// 获取视频分 P 的在线人数计数。
    pub async fn online_total(
        &self,
        params: VideoOnlineTotalParams,
    ) -> BpiResult<OnlineTotalResponseData> {
        self.client
            .get(ONLINE_TOTAL_ENDPOINT)
            .query(&params.query_pairs())
            .send_bpi_payload("video.online_total")
            .await
    }

    /// 获取视频分 P 的 Web 播放器元数据。
    pub async fn player_info_v2(
        &self,
        params: VideoPlayerInfoParams,
    ) -> BpiResult<PlayerInfoResponseData> {
        let params = self.client.get_wbi_sign2(params.query_pairs()).await?;

        self.client
            .get(PLAYER_INFO_V2_ENDPOINT)
            .query(&params)
            .send_bpi_payload("video.player_info_v2")
            .await
    }

    /// 获取与某个视频相关的视频。
    pub async fn related_videos(&self, params: VideoRelatedParams) -> BpiResult<Vec<RelatedVideo>> {
        self.client
            .get(RELATED_VIDEOS_ENDPOINT)
            .query(&params.query_pairs())
            .send_bpi_payload("video.related_videos")
            .await
    }

    /// 获取首页视频推荐。
    pub async fn homepage_recommendations(
        &self,
        params: VideoHomepageRecommendationsParams,
    ) -> BpiResult<RcmdFeedResponseData> {
        let params = self.client.get_wbi_sign2(params.query_pairs()).await?;

        self.client
            .get(HOMEPAGE_RECOMMENDATIONS_ENDPOINT)
            .query(&params)
            .send_bpi_payload("video.homepage_recommendations")
            .await
    }

    /// 获取视频的 AI 总结。
    pub async fn ai_summary(
        &self,
        params: VideoAiSummaryParams,
    ) -> BpiResult<AiSummaryResponseData> {
        let params = self.client.get_wbi_sign2(params.query_pairs()).await?;

        self.client
            .get(AI_SUMMARY_ENDPOINT)
            .query(&params)
            .send_bpi_payload("video.ai_summary")
            .await
    }

    /// 获取视频关联的标签。
    pub async fn tags(&self, params: VideoTagsParams) -> BpiResult<Vec<VideoTag>> {
        self.client
            .get(TAGS_ENDPOINT)
            .query(&params.query_pairs())
            .send_bpi_payload("video.tags")
            .await
    }

    /// 获取互动视频节点的元数据。
    pub async fn interactive_video_info(
        &self,
        params: InteractiveVideoInfoParams,
    ) -> BpiResult<InteractiveVideoInfoResponseData> {
        self.client
            .get(INTERACTIVE_INFO_ENDPOINT)
            .query(&params.query_pairs())
            .send_bpi_payload("video.interactive_video_info")
            .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        ApiEnvelope, BpiClient, BpiError, BpiResult,
        ids::{Aid, Cid, Mid, SeasonId},
        probe::{contract::HttpMethod, endpoint_contract::EndpointContract},
        video::params::VideoHomepageRecommendationsParams,
        video::{
            InteractiveVideoInfoParams, VideoAiSummaryParams,
            VideoCollectionHomeSeasonsSeriesParams, VideoCollectionSeasonsArchivesParams,
            VideoCollectionSeasonsSeriesParams, VideoCollectionSeriesArchivesParams,
            VideoCollectionSeriesInfoParams, VideoOnlineTotalParams, VideoPlayerInfoParams,
            VideoRelatedParams, VideoTagsParams,
        },
    };
    use serde::de::DeserializeOwned;

    fn contract(endpoint: &str) -> BpiResult<EndpointContract> {
        let bytes: &[u8] = match endpoint {
            "view" => include_bytes!("../../tests/contracts/video/info-read/view/contract.json"),
            "detail" => {
                include_bytes!("../../tests/contracts/video/info-read/detail/contract.json")
            }
            "pagelist" => {
                include_bytes!("../../tests/contracts/video/info-read/pagelist/contract.json")
            }
            "desc" => include_bytes!("../../tests/contracts/video/info-read/desc/contract.json"),
            _ => {
                return Err(BpiError::invalid_parameter(
                    "endpoint",
                    "unknown video contract",
                ));
            }
        };

        EndpointContract::from_slice(bytes)
    }

    fn local_probe_body(endpoint: &str, profile: &str) -> Option<serde_json::Value> {
        let path =
            format!("target/bpi-probe-runs/video/info-read/{endpoint}/{profile}.response.json");
        let bytes = std::fs::read(path).ok()?;
        let value: serde_json::Value = serde_json::from_slice(&bytes).ok()?;
        value
            .get("response")
            .and_then(|response| response.get("body"))
            .cloned()
    }

    fn parse_local_probe_outputs<T>(endpoint: &str, profiles: &[&str]) -> BpiResult<()>
    where
        T: DeserializeOwned,
    {
        for profile in profiles {
            let Some(body) = local_probe_body(endpoint, profile) else {
                continue;
            };

            let _payload = serde_json::from_value::<ApiEnvelope<T>>(body)?.into_payload()?;
        }

        Ok(())
    }

    #[test]
    fn video_client_borrows_root_client() -> Result<(), crate::BpiError> {
        let client = BpiClient::new()?;
        let video = client.video();

        assert_eq!(
            video.endpoint(),
            "https://api.bilibili.com/x/web-interface/view"
        );
        Ok(())
    }

    #[test]
    fn video_client_exposes_info_read_endpoints() -> Result<(), crate::BpiError> {
        let client = BpiClient::new()?;
        let video = client.video();

        assert_eq!(
            video.detail_endpoint(),
            "https://api.bilibili.com/x/web-interface/view/detail"
        );
        assert_eq!(
            video.page_list_endpoint(),
            "https://api.bilibili.com/x/player/pagelist"
        );
        assert_eq!(
            video.desc_endpoint(),
            "https://api.bilibili.com/x/web-interface/archive/desc"
        );
        Ok(())
    }

    #[test]
    fn video_client_methods_use_payload_request_helpers() {
        let source = include_str!("client.rs");
        let payload_helper = concat!(".send_", "bpi_payload");
        let legacy_envelope_helper = concat!(".send_", "bpi::<");
        let legacy_flat_playurl = concat!(".video_", "playurl(");

        assert!(
            source.matches(payload_helper).count() >= 5,
            "VideoClient read methods should return decoded payloads directly"
        );
        assert!(
            !source.contains(legacy_envelope_helper),
            "VideoClient should not use legacy envelope-returning request helpers"
        );
        assert!(
            !source.contains(legacy_flat_playurl),
            "VideoClient::play_url should be implemented as a payload-helper-backed domain method"
        );
    }

    #[test]
    fn video_client_exposes_collection_and_player_read_methods() -> BpiResult<()> {
        let client = BpiClient::new()?;
        let video = client.video();

        std::mem::drop(
            video.seasons_archives_list(VideoCollectionSeasonsArchivesParams::new(
                Mid::new(1000001)?,
                SeasonId::new(4294056)?,
            )),
        );
        std::mem::drop(
            video.home_seasons_series(VideoCollectionHomeSeasonsSeriesParams::new(Mid::new(
                1000001,
            )?)),
        );
        std::mem::drop(
            video.seasons_series_list(VideoCollectionSeasonsSeriesParams::new(Mid::new(1000001)?)),
        );
        std::mem::drop(video.series_info(VideoCollectionSeriesInfoParams::new(250285)?));
        std::mem::drop(
            video.series_archives(VideoCollectionSeriesArchivesParams::new(
                Mid::new(1000001)?,
                250285,
            )?),
        );
        std::mem::drop(video.online_total(VideoOnlineTotalParams::from_bvid(
            "BV1xx411c7mD".parse()?,
            Cid::new(62131)?,
        )));
        std::mem::drop(video.player_info_v2(VideoPlayerInfoParams::from_bvid(
            "BV1xx411c7mD".parse()?,
            Cid::new(62131)?,
        )));
        std::mem::drop(
            video.related_videos(VideoRelatedParams::from_bvid("BV1xx411c7mD".parse()?)),
        );
        std::mem::drop(video.homepage_recommendations(VideoHomepageRecommendationsParams::new()));
        std::mem::drop(video.ai_summary(VideoAiSummaryParams::from_bvid(
            "BV1xx411c7mD".parse()?,
            Cid::new(62131)?,
            928123,
        )?));
        std::mem::drop(
            video.tags(VideoTagsParams::from_bvid("BV1xx411c7mD".parse()?).cid(Cid::new(62131)?)),
        );
        std::mem::drop(
            video.interactive_video_info(InteractiveVideoInfoParams::from_aid(
                Aid::new(114347430905959)?,
                1273647,
            )?),
        );

        let source = include_str!("client.rs");
        let payload_helper = concat!(".send_", "bpi_payload");

        assert!(
            source.matches(payload_helper).count() >= 17,
            "VideoClient should use payload helpers for info, playurl, collection, and player read methods"
        );
        Ok(())
    }

    #[test]
    fn video_info_read_contracts_match_endpoint_requests() -> BpiResult<()> {
        let expectations = [
            (
                "view",
                "video.view",
                VIEW_ENDPOINT,
                &[("bvid", "BV1xx411c7mD")][..],
                "VideoView",
            ),
            (
                "detail",
                "video.detail",
                DETAIL_ENDPOINT,
                &[("bvid", "BV1xx411c7mD"), ("need_elec", "0")][..],
                "VideoDetail",
            ),
            (
                "pagelist",
                "video.pagelist",
                PAGELIST_ENDPOINT,
                &[("bvid", "BV1xx411c7mD")][..],
                "Vec<VideoPage>",
            ),
            (
                "desc",
                "video.desc",
                DESC_ENDPOINT,
                &[("bvid", "BV1xx411c7mD")][..],
                "String",
            ),
        ];

        for (endpoint, name, url, query_pairs, rust_model) in expectations {
            let contract = contract(endpoint)?;

            assert_eq!(contract.name, name);
            assert_eq!(contract.request.method, HttpMethod::Get);
            assert_eq!(contract.request.url.as_str(), url);
            assert_eq!(contract.cases.len(), 3);
            assert!(
                contract
                    .cases
                    .iter()
                    .all(|case| case.response.api_code == Some(0)),
                "{endpoint} should have successful anonymous, normal, and vip cases"
            );
            assert!(
                contract
                    .cases
                    .iter()
                    .any(|case| case.response.rust_model.as_deref() == Some(rust_model)),
                "{endpoint} should declare {rust_model}"
            );

            for &(key, value) in query_pairs {
                assert_eq!(
                    contract.request.query.get(key).map(String::as_str),
                    Some(value)
                );
            }
        }

        Ok(())
    }

    #[test]
    fn video_info_read_response_fixtures_parse_declared_models() -> BpiResult<()> {
        let view = ApiEnvelope::<VideoView>::from_slice(include_bytes!(
            "../../tests/contracts/video/info-read/view/responses/success.json"
        ))?
        .into_payload()?;
        let detail = ApiEnvelope::<VideoDetail>::from_slice(include_bytes!(
            "../../tests/contracts/video/info-read/detail/responses/success.json"
        ))?
        .into_payload()?;
        let pagelist = ApiEnvelope::<Vec<VideoPage>>::from_slice(include_bytes!(
            "../../tests/contracts/video/info-read/pagelist/responses/success.json"
        ))?
        .into_payload()?;
        let desc = ApiEnvelope::<String>::from_slice(include_bytes!(
            "../../tests/contracts/video/info-read/desc/responses/success.json"
        ))?
        .into_payload()?;

        assert_eq!(view.bvid.as_str(), "BV1xx411c7mD");
        assert_eq!(detail.view.bvid.as_str(), "BV1xx411c7mD");
        assert_eq!(pagelist.len(), 1);
        assert_eq!(desc, "www");
        Ok(())
    }

    #[test]
    fn video_info_read_models_match_local_probe_outputs_when_available() -> BpiResult<()> {
        parse_local_probe_outputs::<VideoView>("view", &["anonymous", "normal", "vip"])?;
        parse_local_probe_outputs::<VideoDetail>("detail", &["anonymous", "normal", "vip"])?;
        parse_local_probe_outputs::<Vec<VideoPage>>("pagelist", &["anonymous", "normal", "vip"])?;
        parse_local_probe_outputs::<String>("desc", &["anonymous", "normal", "vip"])?;

        Ok(())
    }
}