From 0dd114c166d4f39e61a86434caee52c2352c4e78 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Sun, 3 Jan 2021 13:06:49 -0800 Subject: [PATCH] Post upvote ratio, permalink and reddit link --- src/post.rs | 45 ++++++++++++++++++++-------------------- src/utils.rs | 9 ++++++-- static/style.css | 18 ++++++++++++++++ templates/post.html | 11 ++++++++-- templates/search.html | 4 ++-- templates/subreddit.html | 2 +- templates/user.html | 4 ++-- 7 files changed, 62 insertions(+), 31 deletions(-) diff --git a/src/post.rs b/src/post.rs index 5098edf..9616bbc 100644 --- a/src/post.rs +++ b/src/post.rs @@ -64,48 +64,49 @@ async fn media(data: &serde_json::Value) -> (String, String) { // POSTS async fn parse_post(json: &serde_json::Value) -> Result { // 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 diff --git a/src/utils.rs b/src/utils.rs index 171d422..df0e5bd 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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 Result<(Vec Result<(Vec li { + margin-right: 15px; +} + .nsfw { color: #FF5C5D; margin-top: 20px; diff --git a/templates/post.html b/templates/post.html index bf72a2c..ac34848 100644 --- a/templates/post.html +++ b/templates/post.html @@ -48,7 +48,7 @@ {% endif %} {{ post.time }}

- + {{ post.title }} {% if post.flair.0 != "" %} {{ post.flair.0 }} @@ -62,6 +62,13 @@ {{ post.media }} {% endif %}
{{ post.body }}
+
+ +

{{ post.upvote_ratio }}% Upvoted

+
@@ -83,7 +90,7 @@ {% for reply3 in reply2.replies %} {% call comment(reply3) %} {% if reply3.replies.len() > 0 %} - → More replies + → More replies {% endif %} {% endfor %} diff --git a/templates/search.html b/templates/search.html index 8b2735d..f6cb4e7 100644 --- a/templates/search.html +++ b/templates/search.html @@ -39,7 +39,7 @@ {% if post.flair.0 != "" %} {{ post.flair.0 }} {% endif %} - {{ post.title }} + {{ post.title }}

@@ -52,7 +52,7 @@
- COMMENT + COMMENT {{ post.time }}

{{ post.body }}

diff --git a/templates/subreddit.html b/templates/subreddit.html index f1fd952..d319b24 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -46,7 +46,7 @@ {% if post.flair.0 != "" %} {{ post.flair.0 }} {% endif %} - {{ post.title }} + {{ post.title }}

diff --git a/templates/user.html b/templates/user.html index 54c1844..4dc00d6 100644 --- a/templates/user.html +++ b/templates/user.html @@ -38,7 +38,7 @@ {% else %} {{ post.flair.0 }} {% endif %} - {{ post.title }} + {{ post.title }}

@@ -51,7 +51,7 @@
- COMMENT + COMMENT {{ post.time }}

{{ post.body }}