From 1c36549134f55642d0f19f4d8607e1fbc92b1637 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Tue, 9 Mar 2021 07:22:17 -0800 Subject: [PATCH] Fix #146 --- src/post.rs | 10 ++++++---- src/search.rs | 2 +- src/subreddit.rs | 6 +++--- src/user.rs | 4 ++-- src/utils.rs | 6 +++--- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/post.rs b/src/post.rs index e4d357c..a4f325e 100644 --- a/src/post.rs +++ b/src/post.rs @@ -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()), }; diff --git a/src/search.rs b/src/search.rs index 1d42336..3970482 100644 --- a/src/search.rs +++ b/src/search.rs @@ -84,7 +84,7 @@ async fn search_subreddits(q: &str) -> Vec { 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::new(), diff --git a/src/subreddit.rs b/src/subreddit.rs index 67ed4d4..70861ca 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -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 { // 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::>()[0]); diff --git a/src/user.rs b/src/user.rs index 79c758a..28a50d1 100644 --- a/src/user.rs +++ b/src/user.rs @@ -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 { // 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(); diff --git a/src/utils.rs b/src/utils.rs index dae78ec..5aaa8fc 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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