diff --git a/src/subreddit.rs b/src/subreddit.rs index dd887e7..025b2cb 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -13,7 +13,7 @@ struct SubredditTemplate { sub: Subreddit, posts: Vec, sort: String, - ends: (String, String), + ends: (String, String) } // SERVICES @@ -60,7 +60,7 @@ pub async fn render(sub_name: String, sort: Option, ends: (Option Result { // Otherwise, grab the JSON output from the request let res = req.unwrap(); - let icon: String = String::from(res["data"]["community_icon"].as_str().unwrap()); //val(&data, "community_icon"); - let icon_split: std::str::Split<&str> = icon.split("?"); - let icon_parts: Vec<&str> = icon_split.collect(); + let members = res["data"]["subscribers"].as_u64().unwrap_or(0); + let active = res["data"]["accounts_active"].as_u64().unwrap_or(0); let sub = Subreddit { name: val(&res, "display_name").await, title: val(&res, "title").await, description: val(&res, "public_description").await, - icon: String::from(icon_parts[0]), + icon: val(&res, "icon_img").await, + members: if members > 1000 { format!("{}k", members / 1000) } else { members.to_string() }, + active: if active > 1000 { format!("{}k", active / 1000) } else { active.to_string() }, }; Ok(sub) diff --git a/src/utils.rs b/src/utils.rs index 1582ab0..addee1d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -21,7 +21,7 @@ pub struct Post { pub score: String, pub media: String, pub time: String, - pub flair: Flair, + pub flair: Flair } #[allow(dead_code)] @@ -30,7 +30,7 @@ pub struct Comment { pub body: String, pub author: String, pub score: String, - pub time: String, + pub time: String } #[allow(dead_code)] @@ -40,7 +40,7 @@ pub struct User { pub icon: String, pub karma: i64, pub banner: String, - pub description: String, + pub description: String } #[allow(dead_code)] @@ -50,6 +50,8 @@ pub struct Subreddit { pub title: String, pub description: String, pub icon: String, + pub members: String, + pub active: String } // Parser for query params, used in sorting (eg. /r/rust/?sort=hot) @@ -57,14 +59,14 @@ pub struct Subreddit { pub struct Params { pub sort: Option, pub after: Option, - pub before: Option, + pub before: Option } // Error template #[derive(askama::Template)] #[template(path = "error.html", escape = "none")] pub struct ErrorTemplate { - pub message: String, + pub message: String } //