Post upvote ratio, permalink and reddit link

This commit is contained in:
spikecodes
2021-01-03 13:06:49 -08:00
parent 67090e9b08
commit 0dd114c166
7 changed files with 62 additions and 31 deletions

View File

@ -64,48 +64,49 @@ async fn media(data: &serde_json::Value) -> (String, String) {
// POSTS
async fn parse_post(json: &serde_json::Value) -> Result<Post, &'static str> {
// Retrieve post (as opposed to comments) from JSON
let post_data: &serde_json::Value = &json["data"]["children"][0];
let post: &serde_json::Value = &json["data"]["children"][0];
// Grab UTC time as unix timestamp
let unix_time: i64 = post_data["data"]["created_utc"].as_f64().unwrap().round() as i64;
// Parse post score
let score = post_data["data"]["score"].as_i64().unwrap();
let unix_time: i64 = post["data"]["created_utc"].as_f64().unwrap().round() as i64;
// Parse post score and upvote ratio
let score = post["data"]["score"].as_i64().unwrap();
let ratio: f64 = post["data"]["upvote_ratio"].as_f64().unwrap_or(1.0) * 100.0;
// Determine the type of media along with the media URL
let media = media(&post_data["data"]).await;
let media = media(&post["data"]).await;
// Build a post using data parsed from Reddit post API
let post = Post {
title: val(post_data, "title"),
community: val(post_data, "subreddit"),
body: rewrite_url(&val(post_data, "selftext_html")),
author: val(post_data, "author"),
Ok(Post {
id: val(post, "id"),
title: val(post, "title"),
community: val(post, "subreddit"),
body: rewrite_url(&val(post, "selftext_html")),
author: val(post, "author"),
author_flair: Flair(
val(post_data, "author_flair_text"),
val(post_data, "author_flair_background_color"),
val(post_data, "author_flair_text_color"),
val(post, "author_flair_text"),
val(post, "author_flair_background_color"),
val(post, "author_flair_text_color"),
),
url: val(post_data, "permalink"),
permalink: val(post, "permalink"),
score: format_num(score),
upvote_ratio: ratio as i64,
post_type: media.0,
flair: Flair(
val(post_data, "link_flair_text"),
val(post_data, "link_flair_background_color"),
if val(post_data, "link_flair_text_color") == "dark" {
val(post, "link_flair_text"),
val(post, "link_flair_background_color"),
if val(post, "link_flair_text_color") == "dark" {
"black".to_string()
} else {
"white".to_string()
},
),
flags: Flags {
nsfw: post_data["data"]["over_18"].as_bool().unwrap_or(false),
stickied: post_data["data"]["stickied"].as_bool().unwrap_or(false),
nsfw: post["data"]["over_18"].as_bool().unwrap_or(false),
stickied: post["data"]["stickied"].as_bool().unwrap_or(false),
},
media: media.1,
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(),
};
Ok(post)
})
}
// COMMENTS

View File

@ -25,13 +25,15 @@ pub struct Flags {
// Post containing content, metadata and media
pub struct Post {
pub id: String,
pub title: String,
pub community: String,
pub body: String,
pub author: String,
pub author_flair: Flair,
pub url: String,
pub permalink: String,
pub score: String,
pub upvote_ratio: i64,
pub post_type: String,
pub flair: Flair,
pub flags: Flags,
@ -191,9 +193,11 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
};
let unix_time: i64 = post["data"]["created_utc"].as_f64().unwrap_or_default().round() as i64;
let score = post["data"]["score"].as_i64().unwrap_or_default();
let ratio: f64 = post["data"]["upvote_ratio"].as_f64().unwrap_or(1.0) * 100.0;
let title = val(post, "title");
posts.push(Post {
id: val(post, "id"),
title: if title.is_empty() { fallback_title.to_owned() } else { title },
community: val(post, "subreddit"),
body: rewrite_url(&val(post, "body_html")),
@ -204,6 +208,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
val(post, "author_flair_text_color"),
),
score: format_num(score),
upvote_ratio: ratio as i64,
post_type: "link".to_string(),
media: img,
flair: Flair(
@ -219,7 +224,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
nsfw: post["data"]["over_18"].as_bool().unwrap_or(false),
stickied: post["data"]["stickied"].as_bool().unwrap_or(false),
},
url: val(post, "permalink"),
permalink: val(post, "permalink"),
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(),
});
}