From d9affcdefc3ed979fa3fc7d31e8cea165bc51143 Mon Sep 17 00:00:00 2001 From: robrobinbin <> Date: Wed, 13 Jan 2021 08:23:48 +0100 Subject: [PATCH] Rich flairs --- src/post.rs | 6 +++--- src/utils.rs | 13 +++++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/post.rs b/src/post.rs index 9100e0c..6f0f38f 100644 --- a/src/post.rs +++ b/src/post.rs @@ -83,7 +83,7 @@ async fn parse_post(json: &serde_json::Value) -> Post { body: rewrite_url(&val(post, "selftext_html")), author: val(post, "author"), author_flair: Flair{ - flair_parts: parse_rich_flair(post["data"]["author_flair_richtext"].as_array()), + flair_parts: parse_rich_flair(val(post, "author_flair_type"), post["data"]["author_flair_richtext"].as_array(), post["data"]["author_flair_text"].as_str()), background_color: val(post, "author_flair_background_color"), foreground_color: val(post, "author_flair_text_color"), }, @@ -93,7 +93,7 @@ async fn parse_post(json: &serde_json::Value) -> Post { post_type, thumbnail: format_url(val(post, "thumbnail").as_str()), flair: Flair{ - flair_parts: parse_rich_flair(post["data"]["link_flair_richtext"].as_array()), + flair_parts: parse_rich_flair(val(post, "link_flair_type"), post["data"]["link_flair_richtext"].as_array(), post["data"]["link_flair_text"].as_str()), background_color: val(post, "link_flair_background_color"), foreground_color: if val(post, "link_flair_text_color") == "dark" { "black".to_string() @@ -146,7 +146,7 @@ async fn parse_comments(json: &serde_json::Value) -> Vec { time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"), replies, flair: Flair{ - flair_parts: parse_rich_flair(comment["data"]["author_flair_richtext"].as_array()), + flair_parts: parse_rich_flair(val(&comment, "author_flair_type"), comment["data"]["author_flair_richtext"].as_array(), comment["data"]["author_flair_text"].as_str()), background_color: val(&comment, "author_flair_background_color"), foreground_color: val(&comment, "author_flair_text_color"), }, diff --git a/src/utils.rs b/src/utils.rs index 2524d16..5725435 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -195,9 +195,9 @@ pub async fn media(data: &serde_json::Value) -> (String, String) { (post_type.to_string(), url) } -pub fn parse_rich_flair(rich_flair: Option<&Vec>) -> Vec { +pub fn parse_rich_flair(flair_type: String, rich_flair: Option<&Vec>, text_flair: Option<&str>) -> Vec { let mut result: Vec = Vec::new(); - if !rich_flair.is_none() { + if flair_type == "richtext" && !rich_flair.is_none() { for part in rich_flair.unwrap() { let flair_part_type = part["e"].as_str().unwrap_or_default().to_string(); let value = if flair_part_type == "text" { @@ -213,6 +213,11 @@ pub fn parse_rich_flair(rich_flair: Option<&Vec>) -> Vec { value, }); } + } else if flair_type == "text" && !text_flair.is_none() { + result.push(FlairPart { + flair_part_type: "text".to_string(), + value: text_flair.unwrap().to_string(), + }); } result } @@ -271,7 +276,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec Result<(Vec