Add age to posts

This commit is contained in:
John Stephani 2025-12-28 00:11:07 -06:00
parent cc96622c71
commit 6ad0b5673e
2 changed files with 19 additions and 8 deletions

View File

@ -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'):

View File

@ -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 %}
</h5>
{% endif %}
</span>