Rich flairs
This commit is contained in:
parent
41c4661bbb
commit
b0540d2c57
@ -83,7 +83,7 @@ async fn parse_post(json: &serde_json::Value) -> Post {
|
|||||||
body: rewrite_url(&val(post, "selftext_html")),
|
body: rewrite_url(&val(post, "selftext_html")),
|
||||||
author: val(post, "author"),
|
author: val(post, "author"),
|
||||||
author_flair: Flair{
|
author_flair: Flair{
|
||||||
flair_parts: parse_rich_flair(post["data"]["author_flair_richtext"].as_array().unwrap()),
|
flair_parts: parse_rich_flair(post["data"]["author_flair_richtext"].as_array()),
|
||||||
background_color: val(post, "author_flair_background_color"),
|
background_color: val(post, "author_flair_background_color"),
|
||||||
foreground_color: val(post, "author_flair_text_color"),
|
foreground_color: val(post, "author_flair_text_color"),
|
||||||
},
|
},
|
||||||
@ -93,7 +93,7 @@ async fn parse_post(json: &serde_json::Value) -> Post {
|
|||||||
post_type,
|
post_type,
|
||||||
thumbnail: format_url(val(post, "thumbnail").as_str()),
|
thumbnail: format_url(val(post, "thumbnail").as_str()),
|
||||||
flair: Flair{
|
flair: Flair{
|
||||||
flair_parts: parse_rich_flair(post["data"]["link_flair_richtext"].as_array().unwrap()),
|
flair_parts: parse_rich_flair(post["data"]["link_flair_richtext"].as_array()),
|
||||||
background_color: val(post, "link_flair_background_color"),
|
background_color: val(post, "link_flair_background_color"),
|
||||||
foreground_color: if val(post, "link_flair_text_color") == "dark" {
|
foreground_color: if val(post, "link_flair_text_color") == "dark" {
|
||||||
"black".to_string()
|
"black".to_string()
|
||||||
@ -146,7 +146,7 @@ async fn parse_comments(json: &serde_json::Value) -> Vec<Comment> {
|
|||||||
time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"),
|
time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"),
|
||||||
replies,
|
replies,
|
||||||
flair: Flair{
|
flair: Flair{
|
||||||
flair_parts: parse_rich_flair(comment["data"]["author_flair_richtext"].as_array().unwrap()),
|
flair_parts: parse_rich_flair(comment["data"]["author_flair_richtext"].as_array()),
|
||||||
background_color: val(&comment, "author_flair_background_color"),
|
background_color: val(&comment, "author_flair_background_color"),
|
||||||
foreground_color: val(&comment, "author_flair_text_color"),
|
foreground_color: val(&comment, "author_flair_text_color"),
|
||||||
},
|
},
|
||||||
|
10
src/utils.rs
10
src/utils.rs
@ -195,9 +195,10 @@ pub async fn media(data: &serde_json::Value) -> (String, String) {
|
|||||||
(post_type.to_string(), url)
|
(post_type.to_string(), url)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_rich_flair(rich_flair: &Vec<Value>) -> Vec<FlairPart> {
|
pub fn parse_rich_flair(rich_flair: Option<&Vec<Value>>) -> Vec<FlairPart> {
|
||||||
let mut result: Vec<FlairPart> = Vec::new();
|
let mut result: Vec<FlairPart> = Vec::new();
|
||||||
for part in rich_flair {
|
if !rich_flair.is_none() {
|
||||||
|
for part in rich_flair.unwrap() {
|
||||||
let flair_part_type = part["e"].as_str().unwrap_or_default().to_string();
|
let flair_part_type = part["e"].as_str().unwrap_or_default().to_string();
|
||||||
let value = if flair_part_type == "text" {
|
let value = if flair_part_type == "text" {
|
||||||
part["t"].as_str().unwrap_or_default().to_string()
|
part["t"].as_str().unwrap_or_default().to_string()
|
||||||
@ -212,6 +213,7 @@ pub fn parse_rich_flair(rich_flair: &Vec<Value>) -> Vec<FlairPart> {
|
|||||||
value,
|
value,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -269,7 +271,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
|
|||||||
body: rewrite_url(&val(post, "body_html")),
|
body: rewrite_url(&val(post, "body_html")),
|
||||||
author: val(post, "author"),
|
author: val(post, "author"),
|
||||||
author_flair: Flair{
|
author_flair: Flair{
|
||||||
flair_parts: parse_rich_flair(post["data"]["author_flair_richtext"].as_array().unwrap()),
|
flair_parts: parse_rich_flair(post["data"]["author_flair_richtext"].as_array()),
|
||||||
background_color: val(post, "author_flair_background_color"),
|
background_color: val(post, "author_flair_background_color"),
|
||||||
foreground_color: val(post, "author_flair_text_color"),
|
foreground_color: val(post, "author_flair_text_color"),
|
||||||
},
|
},
|
||||||
@ -280,7 +282,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
|
|||||||
media,
|
media,
|
||||||
domain: val(post, "domain"),
|
domain: val(post, "domain"),
|
||||||
flair: Flair{
|
flair: Flair{
|
||||||
flair_parts: parse_rich_flair(post["data"]["link_flair_richtext"].as_array().unwrap()),
|
flair_parts: parse_rich_flair(post["data"]["link_flair_richtext"].as_array()),
|
||||||
background_color: val(post, "link_flair_background_color"),
|
background_color: val(post, "link_flair_background_color"),
|
||||||
foreground_color: if val(post, "link_flair_text_color") == "dark" {
|
foreground_color: if val(post, "link_flair_text_color") == "dark" {
|
||||||
"black".to_string()
|
"black".to_string()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user