caveman/cave/src/feed.rs

95 lines
2.1 KiB
Rust
Raw Normal View History

2022-11-02 22:42:43 +01:00
use chrono::{DateTime, FixedOffset};
2022-11-02 21:12:16 +01:00
#[derive(Debug, serde::Deserialize)]
pub struct Account {
pub username: String,
pub display_name: String,
pub url: String,
pub bot: bool,
pub avatar: String,
pub header: String,
}
impl Account {
pub fn host(&self) -> Option<String> {
reqwest::Url::parse(&self.url)
.ok()
.and_then(|url| url.domain()
2022-11-03 17:37:06 +01:00
.map(|s| s.to_lowercase())
2022-11-02 21:12:16 +01:00
)
}
}
#[derive(Debug, serde::Deserialize)]
pub struct Tag {
pub name: String,
}
#[derive(Debug, serde::Deserialize)]
pub struct Application {
pub name: String,
pub website: Option<String>,
2022-11-02 21:12:16 +01:00
}
2022-11-03 03:42:13 +01:00
#[derive(Debug, serde::Deserialize)]
pub struct Mention {
pub username: Option<String>,
pub url: String,
pub acct: Option<String>,
}
impl Mention {
pub fn user_host(&self) -> Option<String> {
reqwest::Url::parse(&self.url)
.ok()
.and_then(|url| url.domain()
2022-11-03 17:37:06 +01:00
.map(|host| host.to_lowercase())
2022-11-03 03:42:13 +01:00
)
}
}
2022-11-02 21:12:16 +01:00
#[derive(Debug, serde::Deserialize)]
pub struct Post {
pub created_at: String,
2022-11-04 15:50:00 +01:00
pub uri: String,
2022-11-02 21:12:16 +01:00
pub content: String,
pub account: Account,
pub tags: Vec<Tag>,
pub application: Option<Application>,
pub sensitive: Option<bool>,
2022-11-03 03:42:13 +01:00
pub mentions: Vec<Mention>,
2022-11-03 17:13:03 +01:00
pub language: Option<String>,
2022-11-02 21:12:16 +01:00
}
impl Post {
2022-11-04 15:50:00 +01:00
pub fn uri_host(&self) -> Option<String> {
reqwest::Url::parse(&self.uri)
2022-11-02 21:49:37 +01:00
.ok()
2022-11-04 15:50:00 +01:00
.and_then(|uri| uri.domain()
2022-11-03 16:17:04 +01:00
.map(|host| host.to_owned())
)
2022-11-02 21:49:37 +01:00
}
2022-11-02 22:42:43 +01:00
pub fn timestamp(&self) -> Option<DateTime<FixedOffset>> {
DateTime::parse_from_rfc3339(&self.created_at)
.ok()
}
2022-11-02 21:12:16 +01:00
}
#[derive(Debug)]
pub struct Feed {
pub posts: Vec<Post>,
}
impl Feed {
pub async fn fetch(client: &reqwest::Client, url: &str) -> Result<Self, reqwest::Error> {
let posts: Vec<Post> = client.get(url)
.send()
.await?
.json()
.await?;
2022-11-03 20:59:36 +01:00
log::trace!("{} {} posts", url, posts.len());
2022-11-02 21:12:16 +01:00
Ok(Feed { posts })
}
}