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(),
});
}

View File

@ -326,6 +326,24 @@ input[type="submit"]:hover { color: var(--accent); }
font-size: 16px;
}
#post_footer {
display: flex;
justify-content: space-between;
opacity: 0.5;
font-size: 14px;
}
#post_links {
display: flex;
list-style: none;
padding: 0;
font-weight: bold;
}
#post_links > li {
margin-right: 15px;
}
.nsfw {
color: #FF5C5D;
margin-top: 20px;

View File

@ -48,7 +48,7 @@
{% endif %}
<span class="datetime">{{ post.time }}</span>
</p>
<a href="{{ post.url }}" class="post_title">
<a href="{{ post.permalink }}" class="post_title">
{{ post.title }}
{% if post.flair.0 != "" %}
<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
@ -62,6 +62,13 @@
<a id="post_url" href="{{ post.media }}">{{ post.media }}</a>
{% endif %}
<div class="post_body">{{ post.body }}</div>
<div id="post_footer">
<ul id="post_links">
<li><a href="/{{ post.id }}">permalink</a></li>
<li><a href="https://reddit.com/{{ post.id }}">reddit</a></li>
</ul>
<p>{{ post.upvote_ratio }}% Upvoted</p>
</div>
</div>
</div>
<form id="sort">
@ -83,7 +90,7 @@
{% for reply3 in reply2.replies %}
{% call comment(reply3) %}
{% if reply3.replies.len() > 0 %}
<a class="deeper_replies" href="{{ post.url }}{{ reply3.id }}">&rarr; More replies</a>
<a class="deeper_replies" href="{{ post.permalink }}{{ reply3.id }}">&rarr; More replies</a>
{% endif %}
</details></div>
{% endfor %}

View File

@ -39,7 +39,7 @@
{% if post.flair.0 != "" %}
<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
{% endif %}
<a href="{{ post.url }}">{{ post.title }}</a>
<a href="{{ post.permalink }}">{{ post.title }}</a>
</p>
</div>
<img class="post_thumbnail" src="{{ post.media }}">
@ -52,7 +52,7 @@
</div>
<details class="comment_right" open>
<summary class="comment_data">
<a class="comment_link" href="{{ post.url }}">COMMENT</a>
<a class="comment_link" href="{{ post.permalink }}">COMMENT</a>
<span class="datetime">{{ post.time }}</span>
</summary>
<p class="comment_body">{{ post.body }}</p>

View File

@ -46,7 +46,7 @@
{% if post.flair.0 != "" %}
<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
{% endif %}
<a href="{{ post.url }}">{{ post.title }}</a>
<a href="{{ post.permalink }}">{{ post.title }}</a>
</p>
</div>
<img class="post_thumbnail" src="{{ post.media }}">

View File

@ -38,7 +38,7 @@
{% else %}
<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
{% endif %}
<a href="{{ post.url }}">{{ post.title }}</a>
<a href="{{ post.permalink }}">{{ post.title }}</a>
</p>
</div>
<img class="post_thumbnail" src="{{ post.media }}">
@ -51,7 +51,7 @@
</div>
<details class="comment_right" open>
<summary class="comment_data">
<a class="comment_link" href="{{ post.url }}">COMMENT</a>
<a class="comment_link" href="{{ post.permalink }}">COMMENT</a>
<span class="datetime">{{ post.time }}</span>
</summary>
<p class="comment_body">{{ post.body }}</p>