From bac2311bc746af3bf47ebab9395ca10a001c02a6 Mon Sep 17 00:00:00 2001 From: xenofem Date: Sun, 20 Feb 2022 05:41:25 -0500 Subject: [PATCH] spread out posts within the same minute --- src/main.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 558ef20..b81ac63 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,9 @@ use actix_web::{ }; use lazy_static::lazy_static; use scraper::{Html, Selector}; -use time::{format_description::well_known::Rfc3339, OffsetDateTime, PrimitiveDateTime, UtcOffset}; +use time::{ + format_description::well_known::Rfc3339, Duration, OffsetDateTime, PrimitiveDateTime, UtcOffset, +}; lazy_static! { static ref FIRST_TEXT_SEL: Selector = Selector::parse("#delform > blockquote").unwrap(); @@ -106,6 +108,8 @@ async fn get_posts(thread_id: &str) -> Option> { .and_then(extract_time) .unwrap_or(OffsetDateTime::UNIX_EPOCH); + let mut last_time = first_time; + let mut posts = vec![Post { text: first_text, image: first_image, @@ -137,11 +141,22 @@ async fn get_posts(thread_id: &str) -> Option> { None } }); - let time = element + + let mut time = element .select(&TIME_SEL) .next() .and_then(extract_time) .unwrap_or(OffsetDateTime::UNIX_EPOCH); + // Spread out posts within the same minute so they'll be sorted properly + if last_time - (last_time.second() * Duration::SECOND) == time { + if last_time.second() < 59 { + last_time += Duration::SECOND; + } + time = last_time; + } else { + last_time = time; + } + posts.push(Post { text, image,