This commit is contained in:
spikecodes 2021-03-09 07:22:17 -08:00
parent 5fb88d4744
commit 1c36549134
No known key found for this signature in database
GPG Key ID: 004CECFF9B463BCB
5 changed files with 15 additions and 13 deletions

View File

@ -1,5 +1,7 @@
// CRATES
use crate::utils::{Author, Comment, Flags, Flair, FlairPart, Media, Post, Preferences, cookie, error, format_num, format_url, param, request, rewrite_urls, template, time, val};
use crate::utils::{
cookie, error, format_num, format_url, param, request, rewrite_urls, template, time, val, Author, Comment, Flags, Flair, FlairPart, Media, Post, Preferences,
};
use tide::Request;
use async_recursion::async_recursion;
@ -68,7 +70,7 @@ async fn parse_post(json: &serde_json::Value) -> Post {
let post: &serde_json::Value = &json["data"]["children"][0];
// Grab UTC time as unix timestamp
let (rel_time, created) = time(post["data"]["created_utc"].as_i64().unwrap_or_default());
let (rel_time, created) = time(post["data"]["created_utc"].as_f64().unwrap_or_default());
// Parse post score and upvote ratio
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;
@ -149,10 +151,10 @@ async fn parse_comments(json: &serde_json::Value, post_link: &str, post_author:
let kind = comment["kind"].as_str().unwrap_or_default().to_string();
let data = &comment["data"];
let unix_time = data["created_utc"].as_i64().unwrap_or_default();
let unix_time = data["created_utc"].as_f64().unwrap_or_default();
let (rel_time, created) = time(unix_time);
let edited = match data["edited"].as_i64() {
let edited = match data["edited"].as_f64() {
Some(stamp) => time(stamp),
None => (String::new(), String::new()),
};

View File

@ -84,7 +84,7 @@ async fn search_subreddits(q: &str) -> Vec<Subreddit> {
name: val(subreddit, "display_name_prefixed"),
url: val(subreddit, "url"),
description: val(subreddit, "public_description"),
subscribers: subreddit["data"]["subscribers"].as_i64().unwrap_or_default(),
subscribers: subreddit["data"]["subscribers"].as_f64().unwrap_or_default() as i64,
})
.collect::<Vec<Subreddit>>(),
_ => Vec::new(),

View File

@ -1,5 +1,5 @@
// CRATES
use crate::utils::{Post, Preferences, Subreddit, cookie, error, format_num, format_url, param, redirect, request, rewrite_urls, template, val};
use crate::utils::{cookie, error, format_num, format_url, param, redirect, request, rewrite_urls, template, val, Post, Preferences, Subreddit};
use askama::Template;
use tide::{http::Cookie, Request};
use time::{Duration, OffsetDateTime};
@ -159,8 +159,8 @@ async fn subreddit(sub: &str) -> Result<Subreddit, String> {
// If success, receive JSON in response
Ok(res) => {
// Metadata regarding the subreddit
let members: i64 = res["data"]["subscribers"].as_i64().unwrap_or_default();
let active: i64 = res["data"]["accounts_active"].as_i64().unwrap_or_default();
let members: i64 = res["data"]["subscribers"].as_u64().unwrap_or_default() as i64;
let active: i64 = res["data"]["accounts_active"].as_u64().unwrap_or_default() as i64;
// Fetch subreddit icon either from the community_icon or icon_img value
let community_icon: &str = res["data"]["community_icon"].as_str().map_or("", |s| s.split('?').collect::<Vec<&str>>()[0]);

View File

@ -1,5 +1,5 @@
// CRATES
use crate::utils::{Post, Preferences, User, error, format_url, param, request, template};
use crate::utils::{error, format_url, param, request, template, Post, Preferences, User};
use askama::Template;
use tide::Request;
use time::OffsetDateTime;
@ -55,7 +55,7 @@ async fn user(name: &str) -> Result<User, String> {
// If success, receive JSON in response
Ok(res) => {
// Grab creation date as unix timestamp
let created: i64 = res["data"]["created"].as_i64().unwrap_or(0);
let created: i64 = res["data"]["created"].as_f64().unwrap_or(0.0).round() as i64;
// nested_val function used to parse JSON from Reddit APIs
let about = |item| res["data"]["subreddit"][item].as_str().unwrap_or_default().to_string();

View File

@ -221,7 +221,7 @@ impl Post {
for post in post_list {
let data = &post["data"];
let (rel_time, created) = time(data["created_utc"].as_i64().unwrap_or_default());
let (rel_time, created) = time(data["created_utc"].as_f64().unwrap_or_default());
let score = data["score"].as_i64().unwrap_or_default();
let ratio: f64 = data["upvote_ratio"].as_f64().unwrap_or(1.0) * 100.0;
let title = val(post, "title");
@ -459,8 +459,8 @@ pub fn format_num(num: i64) -> String {
}
// Parse a relative and absolute time from a UNIX timestamp
pub fn time(created: i64) -> (String, String) {
let time = OffsetDateTime::from_unix_timestamp(created);
pub fn time(created: f64) -> (String, String) {
let time = OffsetDateTime::from_unix_timestamp(created.round() as i64);
let time_delta = OffsetDateTime::now_utc() - time;
// If the time difference is more than a month, show full date