From 6ad0b5673e094aad47e65f61908f213f084e5cf3 Mon Sep 17 00:00:00 2001 From: John Stephani Date: Sun, 28 Dec 2025 00:11:07 -0600 Subject: [PATCH] Add age to posts --- app/app.py | 17 +++++++++++++++-- app/templates/index.html | 10 ++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/app.py b/app/app.py index bbbdaa3..dd6b9b8 100755 --- a/app/app.py +++ b/app/app.py @@ -7,6 +7,7 @@ import json import re import sqlite3 import subprocess +import time app = Flask(__name__) @@ -238,7 +239,7 @@ def get_saved(): WHERE saved = ? ORDER BY - score desc + created_utc desc LIMIT ? """ binds = [True, config.posts_per_page_load] @@ -306,6 +307,7 @@ def get_posts_from_select(cursor, select, binds): posts = [json.loads(post[0]) for post in results] add_media_html_to_posts(posts) add_subreddits_to_posts(posts) + add_age_to_posts(posts) return posts def add_media_html_to_posts(posts): @@ -325,7 +327,18 @@ def add_subreddits_to_posts(posts): if "subreddit" not in post: m = re.search(r"\/r\/([a-zA-Z0-9_]+)\/.*", post["permalink"]) post["subreddit"] = m.group(1) - + +def add_age_to_posts(posts): + seconds = time.time() + for post in posts: + diff = seconds - int(post["created_utc"]) + if diff < 3600: + post["age"] = str(int(diff//60))+'m' + elif diff < (3600 * 24): + post["age"] = str(int(diff//3600))+'h' + else: + post["age"] = str(int(diff//(3600*24)))+'d' + def get_media_html(file, priority=False): if file.endswith('.jpg') or file.endswith('.jpeg') or file.endswith('.png') or file.endswith('.gif'): diff --git a/app/templates/index.html b/app/templates/index.html index 995f534..f8e8cee 100755 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -23,6 +23,7 @@ } img, video { max-width: 100%; + max-height: 80vh; width: auto; height: auto; } @@ -50,9 +51,6 @@ } /* desktop */ @media (min-aspect-ratio: 1) { - img, video { - max-height: 80vh; - } div.post { width: 70vw; } @@ -71,9 +69,6 @@ } /* phone */ @media (max-aspect-ratio: 1) { - img, video { - max-height: 100vh; - } div.post { width: calc(100vw - 50px); margin-top: 10px; @@ -195,6 +190,9 @@ {% if post.author %} — {{ post.author }} {% endif %} + {% if post.age %} + — {{ post.age }} + {% endif %} {% endif %}