-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[bilibili.video] Add support for the source platform
- Loading branch information
Showing
7 changed files
with
173 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod live; | ||
pub mod space; | ||
pub mod video; | ||
|
||
use serde::Deserialize; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
use std::{fmt, future::Future, pin::Pin}; | ||
|
||
use anyhow::{anyhow, ensure}; | ||
use serde::Deserialize; | ||
use serde_json as json; | ||
|
||
use super::Response; | ||
use crate::source::{ | ||
FetcherTrait, Post, PostAttachment, PostAttachmentImage, Posts, SourcePlatformName, Status, | ||
StatusKind, StatusSource, | ||
}; | ||
|
||
#[derive(Clone, Debug, PartialEq, Deserialize)] | ||
pub struct ConfigParams { | ||
pub user_id: u64, | ||
pub series_id: u64, | ||
} | ||
|
||
impl fmt::Display for ConfigParams { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!( | ||
f, | ||
"bilibili.video:{},series{}", | ||
self.user_id, self.series_id | ||
) | ||
} | ||
} | ||
|
||
mod data { | ||
use super::*; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct SeriesArchives { | ||
pub aids: Vec<u64>, | ||
pub archives: Vec<Archive>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct Archive { | ||
pub aid: u64, | ||
pub title: String, | ||
pub pic: String, // Image URL | ||
pub bvid: String, | ||
} | ||
} | ||
|
||
pub struct Fetcher { | ||
params: ConfigParams, | ||
} | ||
|
||
impl FetcherTrait for Fetcher { | ||
fn fetch_status(&self) -> Pin<Box<dyn Future<Output = anyhow::Result<Status>> + Send + '_>> { | ||
Box::pin(self.fetch_status_impl()) | ||
} | ||
} | ||
|
||
impl fmt::Display for Fetcher { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.params) | ||
} | ||
} | ||
|
||
impl Fetcher { | ||
pub fn new(params: ConfigParams) -> Self { | ||
Self { params } | ||
} | ||
|
||
async fn fetch_status_impl(&self) -> anyhow::Result<Status> { | ||
let videos = fetch_series_archives(self.params.user_id, self.params.series_id).await?; | ||
|
||
Ok(Status { | ||
kind: StatusKind::Posts(videos), | ||
source: StatusSource { | ||
platform_name: SourcePlatformName::BilibiliVideo, | ||
user: None, | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
async fn fetch_series_archives(user_id: u64, series_id: u64) -> anyhow::Result<Posts> { | ||
let resp = reqwest::Client::new() | ||
.get(format!( | ||
"https://api.bilibili.com/x/series/archives?mid={user_id}&series_id={series_id}" | ||
)) | ||
.send() | ||
.await | ||
.map_err(|err| anyhow!("failed to send request: {err}"))?; | ||
|
||
let status = resp.status(); | ||
ensure!( | ||
status.is_success(), | ||
"response status is not success: {resp:?}" | ||
); | ||
|
||
let text = resp | ||
.text() | ||
.await | ||
.map_err(|err| anyhow!("failed to obtain text from response: {err}"))?; | ||
|
||
let resp: Response<data::SeriesArchives> = json::from_str(&text) | ||
.map_err(|err| anyhow!("failed to deserialize response: {err}, text: {text}"))?; | ||
ensure!(resp.code == 0, "response code is not 0. text: {text}"); | ||
|
||
parse_response(resp.data.unwrap()) | ||
} | ||
|
||
fn parse_response(resp: data::SeriesArchives) -> anyhow::Result<Posts> { | ||
let videos = resp | ||
.archives | ||
.into_iter() | ||
.map(|archive| Post { | ||
user: None, | ||
content: archive.title, | ||
url: format!("https://www.bilibili.com/video/{}", archive.bvid), | ||
repost_from: None, | ||
attachments: vec![PostAttachment::Image(PostAttachmentImage { | ||
media_url: archive.pic, | ||
})], | ||
}) | ||
.collect(); | ||
|
||
Ok(Posts(videos)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn deser() { | ||
let videos = fetch_series_archives(522384919, 3747026).await.unwrap(); | ||
|
||
assert!(videos.0.iter().all(|post| !post.url.is_empty())); | ||
assert!(videos.0.iter().all(|post| !post.content.is_empty())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters