Skip to content

Commit

Permalink
[bilibili.space] Recognize and handle RICH_TEXT_NODE_TYPE_VIEW_PICTURE
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Dec 19, 2024
1 parent 06d0189 commit 4cca18a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/notify/platform/qq/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl Notifier {
}

let images = post
.attachments_recursive()
.attachments_recursive(true)
.into_iter()
.filter_map(|attachment| match attachment {
PostAttachment::Image(image) => Some(image.media_url.as_str()),
Expand Down
2 changes: 1 addition & 1 deletion src/notify/platform/telegram/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ impl Notifier {

const DISABLE_NOTIFICATION: bool = true; // TODO: Make it configurable

let attachments = post.attachments_recursive();
let attachments = post.attachments_recursive(true);
let num_attachments = attachments.len();

let resp = match num_attachments {
Expand Down
3 changes: 3 additions & 0 deletions src/notify/platform/telegram/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,9 @@ impl<'a> Text<'a> {
content.parts().for_each(|part| match part {
PostContentPart::Plain(text) => self.push_plain(text),
PostContentPart::Link { display, url } => self.push_link(display, url),
PostContentPart::InlineAttachment(_) => {
// Ignore, we handle it in post.attachments
}
});
}

Expand Down
7 changes: 7 additions & 0 deletions src/source/content.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::PostAttachment;

#[derive(Clone, Debug, PartialEq)]
pub struct PostContent(Vec<PostContentPart>);

Expand All @@ -16,6 +18,10 @@ impl PostContent {
.map(|part| match part {
PostContentPart::Plain(text) => text.as_str(),
PostContentPart::Link { url, .. } => url.as_str(),
PostContentPart::InlineAttachment(attachment) => match attachment {
PostAttachment::Image(attachment) => attachment.media_url.as_str(),
PostAttachment::Video(attachment) => attachment.media_url.as_str(),
},
})
.collect::<String>()
}
Expand Down Expand Up @@ -65,4 +71,5 @@ impl PostContent {
pub enum PostContentPart {
Plain(String),
Link { display: String, url: String },
InlineAttachment(PostAttachment),
}
14 changes: 12 additions & 2 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,21 @@ pub enum RepostFrom {
}

impl Post {
pub fn attachments_recursive(&self) -> Vec<&PostAttachment> {
pub fn attachments_recursive(&self, include_inlined: bool) -> Vec<&PostAttachment> {
if let Some(RepostFrom::Recursion(repost_from)) = &self.repost_from {
self.attachments
.iter()
.chain(repost_from.attachments_recursive())
.chain(self.content.parts().into_iter().filter_map(|content| {
if !include_inlined {
return None;
}
if let PostContentPart::InlineAttachment(attachment) = content {
Some(attachment)
} else {
None
}
}))
.chain(repost_from.attachments_recursive(include_inlined))
.collect()
} else {
self.attachments.iter().collect()
Expand Down
30 changes: 30 additions & 0 deletions src/source/platform/bilibili/space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,22 @@ mod data {
display: node.text.clone(),
url: format!("https://www.bilibili.com/video/{rid}"),
},
RichTextNodeKind::ViewPicture { pics, .. } => {
if pics.len() != 1 {
warn!(
"bilibili rich text view-pic node has pics.len() = {}",
pics.len()
);
PostContentPart::Plain(node.orig_text.clone())
} else {
PostContentPart::InlineAttachment(PostAttachment::Image(
PostAttachmentImage {
media_url: pics.get(0).unwrap().src.clone(),
has_spoiler: false,
},
))
}
}
// We treat these nodes as plain text
RichTextNodeKind::Emoji { .. } | RichTextNodeKind::Lottery { .. } => {
PostContentPart::Plain(node.orig_text.clone())
Expand Down Expand Up @@ -335,6 +351,12 @@ mod data {
Lottery { rid: String },
#[serde(rename = "RICH_TEXT_NODE_TYPE_BV")]
Bv { jump_url: String, rid: String },
#[serde(rename = "RICH_TEXT_NODE_TYPE_VIEW_PICTURE")]
ViewPicture {
jump_url: String,
pics: Vec<RichTextNodeViewPicturePic>,
rid: String,
},
#[serde(untagged)]
Unknown(json::Value),
}
Expand All @@ -346,6 +368,14 @@ mod data {
// pub size: u64
// pub type: u64
}

#[derive(Debug, Deserialize)]
pub struct RichTextNodeViewPicturePic {
pub src: String,
// pub height: u64
// pub width: u64
// pub size: u64
}
}

pub struct Fetcher {
Expand Down
2 changes: 1 addition & 1 deletion src/source/platform/twitter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ fn parse_tweet(tweet: data::Tweet) -> Post {
.filter(|attachment| {
if let Some(RepostFrom::Recursion(repost_from)) = &repost_from {
let is_contained_in_repost = repost_from
.attachments_recursive()
.attachments_recursive(true)
.iter()
.any(|sub_attachment| *sub_attachment == attachment);
!is_contained_in_repost
Expand Down

0 comments on commit 4cca18a

Please sign in to comment.