reddit-magnet/src/reddit_client.rs

55 lines
1.6 KiB
Rust

use crate::config::types::RedditUsername;
use chrono::{DateTime, TimeZone, Utc};
use color_eyre::eyre::{Result, WrapErr};
use roux::util::FeedOption;
use roux::User;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct RedditPost {
pub title: String,
pub body: String,
pub subreddit: String,
pub created: DateTime<Utc>,
}
/// A client for interacting with Reddit API
pub struct RedditClient;
impl RedditClient {
/// Create a new RedditClient
pub fn new() -> Self {
RedditClient
}
/// Fetch submissions for a user
///
/// # Arguments
///
/// * `username` - The Reddit username to fetch submissions for
/// * `post_count` - The maximum number of posts to fetch
pub async fn fetch_user_submissions(
&self,
username: &RedditUsername,
post_count: u32,
) -> Result<Vec<RedditPost>> {
let user = User::new(username.as_str());
let feed_option = FeedOption::new().limit(post_count);
let submissions = user
.submitted(Some(feed_option))
.await
.context(format!("Failed to fetch submissions for user {}", username))?;
let mut children = Vec::new();
for post in submissions.data.children.iter() {
children.push(RedditPost {
title: post.data.title.clone(),
body: post.data.selftext.clone(),
subreddit: post.data.subreddit.clone(),
created: Utc.timestamp_opt(post.data.created_utc as i64, 0).unwrap(),
});
}
Ok(children)
}
}