This commit is contained in:
Marc Plano-Lesay 2025-04-24 22:27:11 +10:00
commit 2892a0d272
Signed by: kernald
GPG key ID: 66A41B08CC62A6CF
11 changed files with 2641 additions and 0 deletions

43
src/reddit_client.rs Normal file
View file

@ -0,0 +1,43 @@
use chrono::{DateTime, TimeZone, Utc};
use color_eyre::eyre::{Result, WrapErr};
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
pub async fn fetch_user_submissions(&self, username: &str) -> Result<Vec<RedditPost>> {
let user = User::new(username);
let submissions = user
.submitted(None)
.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)
}
}