caveman/src/feed.rs
2022-11-02 21:12:26 +01:00

65 lines
1.3 KiB
Rust

#[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()
.map(|s| s.to_owned())
)
}
}
#[derive(Debug, serde::Deserialize)]
pub struct Tag {
pub name: String,
}
#[derive(Debug, serde::Deserialize)]
pub struct Application {
pub name: String,
pub website: String,
}
#[derive(Debug, serde::Deserialize)]
pub struct Post {
pub created_at: String,
pub url: String,
pub content: String,
pub account: Account,
pub tags: Vec<Tag>,
pub application: Option<Application>,
pub sensitive: Option<bool>,
}
impl Post {
// fn time
// fn text_content
}
#[derive(Debug)]
pub struct Feed {
pub posts: Vec<Post>,
}
impl Feed {
pub async fn fetch(client: &reqwest::Client, url: &str) -> Result<Self, reqwest::Error> {
println!("GET {}", url);
let posts: Vec<Post> = client.get(url)
.send()
.await?
.json()
.await?;
println!("{}: {} posts", url, posts.len());
Ok(Feed { posts })
}
}