Skip to content

Commit

Permalink
Add a new field time to struct Post
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Feb 16, 2025
1 parent 7fe0471 commit 9b0ddfb
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 22 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ publish = false
[dependencies]
anyhow = "1.0.89"
bytes = "1.7.2"
chrono = "0.4.39"
clap = { version = "4.5.19", features = ["derive"] }
futures = "0.3.31"
headless_chrome = "1.0.15"
Expand Down
12 changes: 12 additions & 0 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod platform;
use std::{fmt, fmt::Display, future::Future, pin::Pin, slice, vec};

use anyhow::ensure;
use chrono::{DateTime, Local};
pub use content::*;

use crate::platform::{PlatformMetadata, PlatformTrait};
Expand Down Expand Up @@ -146,6 +147,7 @@ pub struct Post {
pub user: Option<User>,
pub content: PostContent,
urls: PostUrls,
pub time: DateTime<Local>,
pub repost_from: Option<RepostFrom>,
attachments: Vec<PostAttachment>,
}
Expand Down Expand Up @@ -503,6 +505,7 @@ mod tests {
user: None,
content: PostContent::plain("content1"),
urls: PostUrls::new(PostUrl::Identity("id1".into())),
time: DateTime::UNIX_EPOCH.into(),
repost_from: None,
attachments: vec![],
}])),
Expand All @@ -525,13 +528,15 @@ mod tests {
user: None,
content: PostContent::plain("content1"),
urls: PostUrls::new(PostUrl::Identity("id1".into())),
time: DateTime::UNIX_EPOCH.into(),
repost_from: None,
attachments: vec![],
},
Post {
user: None,
content: PostContent::plain("content2"),
urls: PostUrls::new(PostUrl::Identity("id2".into())),
time: DateTime::UNIX_EPOCH.into(),
repost_from: None,
attachments: vec![],
},
Expand All @@ -549,13 +554,15 @@ mod tests {
user: None,
content: PostContent::plain("content1"),
urls: PostUrls::new(PostUrl::Identity("id1".into())),
time: DateTime::UNIX_EPOCH.into(),
repost_from: None,
attachments: vec![],
},
Post {
user: None,
content: PostContent::plain("content2"),
urls: PostUrls::new(PostUrl::Identity("id2".into())),
time: DateTime::UNIX_EPOCH.into(),
repost_from: None,
attachments: vec![],
},
Expand Down Expand Up @@ -588,6 +595,7 @@ mod tests {
user: None,
content: PostContent::plain("content3"),
urls: PostUrls::new(PostUrl::Identity("id3".into())),
time: DateTime::UNIX_EPOCH.into(),
repost_from: None,
attachments: vec![],
}])),
Expand All @@ -607,20 +615,23 @@ mod tests {
user: None,
content: PostContent::plain("content1"),
urls: PostUrls::new(PostUrl::Identity("id1".into())),
time: DateTime::UNIX_EPOCH.into(),
repost_from: None,
attachments: vec![],
},
Post {
user: None,
content: PostContent::plain("content2"),
urls: PostUrls::new(PostUrl::Identity("id2".into())),
time: DateTime::UNIX_EPOCH.into(),
repost_from: None,
attachments: vec![],
},
Post {
user: None,
content: PostContent::plain("content3"),
urls: PostUrls::new(PostUrl::Identity("id3".into())),
time: DateTime::UNIX_EPOCH.into(),
repost_from: None,
attachments: vec![],
},
Expand Down Expand Up @@ -673,6 +684,7 @@ mod tests {
user: None,
content: PostContent::plain("content1"),
urls: PostUrls::new(PostUrl::Identity("id1".into())),
time: DateTime::UNIX_EPOCH.into(),
repost_from: None,
attachments: vec![],
}])),
Expand Down
28 changes: 27 additions & 1 deletion src/source/platform/bilibili/space.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{collections::HashSet, fmt, fmt::Display, future::Future, ops::DerefMut, pin::Pin};

use anyhow::{anyhow, bail, Ok};
use anyhow::{anyhow, bail, ensure};
use chrono::DateTime;
use once_cell::sync::Lazy;
use reqwest::header::{self, HeaderValue};
use serde::Deserialize;
Expand Down Expand Up @@ -81,6 +82,16 @@ mod data {
Pgc(ModuleAuthorPgc),
}

impl ModuleAuthor {
pub fn pub_time(&self) -> Option<u64> {
let pub_ts = match self {
ModuleAuthor::Normal(normal) => normal.pub_ts,
ModuleAuthor::Pgc(pgc) => pgc.pub_ts,
};
(pub_ts != 0).then_some(pub_ts)
}
}

impl From<ModuleAuthor> for User {
fn from(value: ModuleAuthor) -> Self {
match value {
Expand Down Expand Up @@ -111,6 +122,7 @@ mod data {
pub face: String, // URL
pub mid: u64,
pub name: String,
pub pub_ts: u64, // Always 0?
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -644,10 +656,24 @@ fn parse_response(resp: data::SpaceHistory, blocked: &mut BlockedPostIds) -> any
})
.unwrap_or(major_url);

let time = item
.modules
.author
.pub_time()
.or(parent_item.and_then(|p| p.modules.author.pub_time()));
ensure!(
time.is_some(),
"bilibili space found a post with no time: '{item:?}'"
);
let time = DateTime::from_timestamp(time.unwrap() as i64, 0)
.ok_or_else(|| anyhow!("invalid pub time, url={url:?}"))?
.into();

Ok(Post {
user: Some(item.modules.author.clone().into()),
content,
urls: PostUrls::new(url),
time,
repost_from: original.map(|original| RepostFrom::Recursion(Box::new(original))),
attachments: item
.modules
Expand Down
37 changes: 23 additions & 14 deletions src/source/platform/bilibili/video.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{fmt, future::Future, pin::Pin};

use anyhow::{anyhow, ensure};
use chrono::DateTime;
use serde::Deserialize;
use serde_json as json;

Expand Down Expand Up @@ -42,6 +43,8 @@ mod data {
pub struct Archive {
pub aid: u64,
pub title: String,
pub pubdate: u64,
pub ctime: u64,
pub pic: String, // Image URL
pub bvid: String,
}
Expand Down Expand Up @@ -120,21 +123,27 @@ fn parse_response(resp: data::SeriesArchives) -> anyhow::Result<Posts> {
let videos = resp
.archives
.into_iter()
.map(|archive| Post {
user: None,
content: PostContent::plain(archive.title),
urls: PostUrl::new_clickable(
format!("https://www.bilibili.com/video/{}", archive.bvid),
"查看视频",
)
.into(),
repost_from: None,
attachments: vec![PostAttachment::Image(PostAttachmentImage {
media_url: upgrade_to_https(&archive.pic),
has_spoiler: false,
})],
.map(|archive| -> anyhow::Result<Post> {
let time = DateTime::from_timestamp(archive.ctime as i64, 0)
.ok_or_else(|| anyhow!("invalid ctime {}, aid={}", archive.ctime, archive.aid))?
.into();
Ok(Post {
user: None,
content: PostContent::plain(archive.title),
urls: PostUrl::new_clickable(
format!("https://www.bilibili.com/video/{}", archive.bvid),
"查看视频",
)
.into(),
time,
repost_from: None,
attachments: vec![PostAttachment::Image(PostAttachmentImage {
media_url: upgrade_to_https(&archive.pic),
has_spoiler: false,
})],
})
})
.collect();
.collect::<Result<_, _>>()?;

Ok(Posts(videos))
}
Expand Down
26 changes: 21 additions & 5 deletions src/source/platform/twitter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
};

use anyhow::{anyhow, bail};
use chrono::DateTime;
use request::*;
use serde::Deserialize;
use spdlog::prelude::*;
Expand Down Expand Up @@ -463,13 +464,13 @@ impl FetcherInner {
})
.map(|result| result.result.into_tweet())
.map(parse_tweet)
.collect::<Vec<_>>();
.collect::<Result<Vec<_>, _>>()?;

Ok(Posts(posts))
}
}

fn parse_tweet(tweet: data::Tweet) -> Post {
fn parse_tweet(tweet: data::Tweet) -> anyhow::Result<Post> {
let content = if tweet.legacy.retweeted_status_result.is_none() {
Some(replace_entities(
tweet.legacy.full_text,
Expand All @@ -492,7 +493,12 @@ fn parse_tweet(tweet: data::Tweet) -> Post {
} else {
tweet.quoted_status_result.and_then(|q| q.into_option())
}
.map(|result| RepostFrom::Recursion(Box::new(parse_tweet(result.result.into_tweet()))));
.map(|result| -> anyhow::Result<RepostFrom> {
Ok(RepostFrom::Recursion(Box::new(parse_tweet(
result.result.into_tweet(),
)?)))
})
.transpose()?;

let possibly_sensitive = tweet.legacy.possibly_sensitive.unwrap_or(false);

Expand Down Expand Up @@ -591,13 +597,23 @@ fn parse_tweet(tweet: data::Tweet) -> Post {
})
.collect();

Post {
let time = DateTime::parse_from_str(&tweet.legacy.created_at, "%a %b %d %H:%M:%S %z %Y")
.map_err(|err| {
anyhow!(
"failed to parse tweet time '{}', err: {err}, urls={urls:?}",
tweet.legacy.created_at
)
})?
.into();

Ok(Post {
user: Some(tweet.core.user_results.result.into()),
content: PostContent::plain(content.unwrap_or_else(|| "".into())),
urls,
time,
repost_from,
attachments,
}
})
}

enum ReplaceKind<'a> {
Expand Down

0 comments on commit 9b0ddfb

Please sign in to comment.