Refactor code
This commit is contained in:
parent
5ab88567de
commit
09c98c8da6
26
src/post.rs
26
src/post.rs
@ -1,5 +1,5 @@
|
|||||||
// CRATES
|
// CRATES
|
||||||
use crate::utils::{cookie, error, format_num, format_url, media, parse_rich_flair, param, prefs, request, rewrite_url, val, Comment, Flags, Flair, Post, Preferences};
|
use crate::utils::{cookie, error, format_num, format_url, media, param, parse_rich_flair, prefs, request, rewrite_url, val, Comment, Flags, Flair, Post, Preferences};
|
||||||
use actix_web::{HttpRequest, HttpResponse};
|
use actix_web::{HttpRequest, HttpResponse};
|
||||||
|
|
||||||
use async_recursion::async_recursion;
|
use async_recursion::async_recursion;
|
||||||
@ -82,8 +82,12 @@ async fn parse_post(json: &serde_json::Value) -> Post {
|
|||||||
community: val(post, "subreddit"),
|
community: val(post, "subreddit"),
|
||||||
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(val(post, "author_flair_type"), post["data"]["author_flair_richtext"].as_array(), post["data"]["author_flair_text"].as_str()),
|
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"),
|
background_color: val(post, "author_flair_background_color"),
|
||||||
foreground_color: val(post, "author_flair_text_color"),
|
foreground_color: val(post, "author_flair_text_color"),
|
||||||
},
|
},
|
||||||
@ -92,8 +96,12 @@ async fn parse_post(json: &serde_json::Value) -> Post {
|
|||||||
upvote_ratio: ratio as i64,
|
upvote_ratio: ratio as i64,
|
||||||
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(val(post, "link_flair_type"), post["data"]["link_flair_richtext"].as_array(), post["data"]["link_flair_text"].as_str()),
|
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"),
|
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()
|
||||||
@ -145,8 +153,12 @@ async fn parse_comments(json: &serde_json::Value) -> Vec<Comment> {
|
|||||||
score: format_num(score),
|
score: format_num(score),
|
||||||
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(val(&comment, "author_flair_type"), comment["data"]["author_flair_richtext"].as_array(), comment["data"]["author_flair_text"].as_str()),
|
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"),
|
background_color: val(&comment, "author_flair_background_color"),
|
||||||
foreground_color: val(&comment, "author_flair_text_color"),
|
foreground_color: val(&comment, "author_flair_text_color"),
|
||||||
},
|
},
|
||||||
|
32
src/utils.rs
32
src/utils.rs
@ -7,20 +7,20 @@ use base64::encode;
|
|||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use serde_json::{from_str, Value};
|
use serde_json::{from_str, Value};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use time::{OffsetDateTime, Duration};
|
use time::{Duration, OffsetDateTime};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
//
|
//
|
||||||
// STRUCTS
|
// STRUCTS
|
||||||
//
|
//
|
||||||
// Post flair with content, background color and foreground color
|
// Post flair with content, background color and foreground color
|
||||||
pub struct Flair{
|
pub struct Flair {
|
||||||
pub flair_parts: Vec<FlairPart>,
|
pub flair_parts: Vec<FlairPart>,
|
||||||
pub background_color: String,
|
pub background_color: String,
|
||||||
pub foreground_color: String,
|
pub foreground_color: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FlairPart{
|
pub struct FlairPart {
|
||||||
pub flair_part_type: String,
|
pub flair_part_type: String,
|
||||||
pub value: String,
|
pub value: String,
|
||||||
}
|
}
|
||||||
@ -183,11 +183,11 @@ pub async fn media(data: &serde_json::Value) -> (String, String) {
|
|||||||
Some(gif) => {
|
Some(gif) => {
|
||||||
post_type = "gif";
|
post_type = "gif";
|
||||||
format_url(gif["source"]["url"].as_str().unwrap_or_default())
|
format_url(gif["source"]["url"].as_str().unwrap_or_default())
|
||||||
},
|
}
|
||||||
None => {
|
None => {
|
||||||
post_type = "image";
|
post_type = "image";
|
||||||
format_url(preview["source"]["url"].as_str().unwrap_or_default())
|
format_url(preview["source"]["url"].as_str().unwrap_or_default())
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
} else if data["is_self"].as_bool().unwrap_or_default() {
|
} else if data["is_self"].as_bool().unwrap_or_default() {
|
||||||
post_type = "self";
|
post_type = "self";
|
||||||
@ -207,16 +207,12 @@ pub fn parse_rich_flair(flair_type: String, rich_flair: Option<&Vec<Value>>, tex
|
|||||||
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()
|
||||||
|
|
||||||
} else if flair_part_type == "emoji" {
|
} else if flair_part_type == "emoji" {
|
||||||
format_url(part["u"].as_str().unwrap_or_default())
|
format_url(part["u"].as_str().unwrap_or_default())
|
||||||
} else {
|
} else {
|
||||||
"".to_string()
|
"".to_string()
|
||||||
};
|
};
|
||||||
result.push(FlairPart {
|
result.push(FlairPart { flair_part_type, value });
|
||||||
flair_part_type,
|
|
||||||
value,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} else if flair_type == "text" && !text_flair.is_none() {
|
} else if flair_type == "text" && !text_flair.is_none() {
|
||||||
result.push(FlairPart {
|
result.push(FlairPart {
|
||||||
@ -292,8 +288,12 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
|
|||||||
community: val(post, "subreddit"),
|
community: val(post, "subreddit"),
|
||||||
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(val(post, "author_flair_type"), post["data"]["author_flair_richtext"].as_array(), post["data"]["author_flair_text"].as_str()),
|
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"),
|
background_color: val(post, "author_flair_background_color"),
|
||||||
foreground_color: val(post, "author_flair_text_color"),
|
foreground_color: val(post, "author_flair_text_color"),
|
||||||
},
|
},
|
||||||
@ -303,8 +303,12 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
|
|||||||
thumbnail: format_url(val(post, "thumbnail").as_str()),
|
thumbnail: format_url(val(post, "thumbnail").as_str()),
|
||||||
media,
|
media,
|
||||||
domain: val(post, "domain"),
|
domain: val(post, "domain"),
|
||||||
flair: Flair{
|
flair: Flair {
|
||||||
flair_parts: parse_rich_flair(val(post, "link_flair_type"), post["data"]["link_flair_richtext"].as_array(), post["data"]["link_flair_text"].as_str()),
|
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"),
|
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…
Reference in New Issue
Block a user