diff --git a/suchwow/app.py b/suchwow/app.py index d79ce59..d3eaa54 100644 --- a/suchwow/app.py +++ b/suchwow/app.py @@ -37,9 +37,6 @@ def index(): page = request.args.get("page", 1) submitter = request.args.get("submitter", None) content = request.args.get("content", None) - if content == "trending": - posts = get_activity() - return render_template("index.html", posts=posts) try: page = int(page) diff --git a/suchwow/models.py b/suchwow/models.py index db7c2b2..e9843c5 100644 --- a/suchwow/models.py +++ b/suchwow/models.py @@ -54,10 +54,15 @@ class Post(Model): amounts = [amt['amount'] for amt in it['transfers'] if 'transfers' in it] return wownero.as_wownero(wownero.from_atomic(sum(amounts))) else: - return '0' + return 0 except: return '?' + def hours_elapsed(self): + now = datetime.utcnow() + diff = now - self.timestamp + return diff.total_seconds() / 60 / 60 + def show(self): return { 'id': self.id, @@ -77,6 +82,8 @@ class Post(Model): 'to_reddit': self.to_reddit, 'to_discord': self.to_discord, 'approved': self.approved, + 'received_wow': self.get_received_wow(), + 'hours_elapsed': self.hours_elapsed() } class Meta: diff --git a/suchwow/routes/leaderboard.py b/suchwow/routes/leaderboard.py index 22dba58..dbea893 100644 --- a/suchwow/routes/leaderboard.py +++ b/suchwow/routes/leaderboard.py @@ -1,15 +1,17 @@ +from datetime import datetime, timedelta from os import path from flask import render_template, Blueprint, request, session, flash from flask import send_from_directory, redirect, url_for, current_app from werkzeug.utils import secure_filename from suchwow import wownero from suchwow.models import Post +from suchwow.utils.helpers import rw_cache bp = Blueprint("leaderboard", "leaderboard") -@bp.route("/leaderboard") -def leaderboard(): +@bp.route("/leaderboards/top_posters") +def top_posters(): top_posters = {} posts = Post.select().where(Post.approved==True) for post in posts: @@ -26,3 +28,36 @@ def leaderboard(): top_posters[post.submitter]["posts"].append(post) return render_template("leaderboard.html", posters=top_posters) + +@bp.route("/leaderboards/top_posts") +def top_posts(): + top_posts = [] + days = request.args.get('days', 1) + try: + days = int(days) + except: + days = 1 + + if days not in [1, 3, 7, 30]: + days = 7 + + hours = 24 * days + diff = datetime.now() - timedelta(hours=hours) + key_name = f'top_posts_{str(hours)}' + + posts = rw_cache(key_name) + if not posts: + posts = Post.select().where( + Post.approved==True, + Post.timestamp > diff + ).order_by( + Post.timestamp.desc() + ) + for post in posts: + p = post.show() + if isinstance(p['received_wow'], float): + top_posts.append(p) + + posts = rw_cache(key_name, top_posts) + + return render_template("post/top.html", posts=posts, days=days) diff --git a/suchwow/routes/post.py b/suchwow/routes/post.py index d1270b3..bf949dd 100644 --- a/suchwow/routes/post.py +++ b/suchwow/routes/post.py @@ -1,5 +1,4 @@ -import pickle -from datetime import datetime +from datetime import datetime, timedelta from os import path, remove from io import BytesIO from base64 import b64encode @@ -12,53 +11,14 @@ from suchwow import wownero from suchwow import config from suchwow.models import Post, Comment from suchwow.utils.decorators import login_required, profile_required, moderator_required -from suchwow.utils.helpers import allowed_file, is_moderator, get_session_user, post_webhook +from suchwow.utils.helpers import allowed_file, is_moderator, get_session_user +from suchwow.utils.helpers import rw_cache, post_webhook from suchwow.reddit import make_post from suchwow.discord import post_discord_webhook bp = Blueprint("post", "post") -@bp.route("/posts/top") -def top(): - top_posts = {} - pickle_file = path.join(config.DATA_FOLDER, 'top_posts.pkl') - - try: - mtime_ts = path.getmtime(pickle_file) - mtime = datetime.fromtimestamp(mtime_ts) - now = datetime.now() - diff = now - mtime - - # If pickled data file is less than an hour old, load it and render page - # Otherwise, determine balances, build json, store pickled data, and render page - if diff.seconds < 3600: - with open(pickle_file, 'rb') as f: - print('Loading pickled data for /posts/top') - pickled_data = pickle.load(f) - return render_template("post/top.html", posts=pickled_data) - except: - pass - - print('Generating and pickling new data for /posts/top') - posts = Post.select().where(Post.approved==True) - for post in posts: - transfers = [] - incoming = wownero.Wallet().incoming_transfers(post.account_index) - if "transfers" in incoming: - for xfer in incoming["transfers"]: - transfers.append(xfer["amount"]) - total = wownero.from_atomic(sum(transfers)) - if total > 0: - top_posts[float(total)] = post - - _t = sorted(top_posts.items(), reverse=True)[0:10] - - with open(pickle_file, 'wb') as f: - f.write(pickle.dumps(_t)) - - return render_template("post/top.html", posts=_t) - @bp.route("/post/") def read(id): _address_qr = BytesIO() diff --git a/suchwow/templates/about.html b/suchwow/templates/about.html index 8e32dcd..1226c88 100644 --- a/suchwow/templates/about.html +++ b/suchwow/templates/about.html @@ -7,6 +7,11 @@

About

Post memes! Have fun! Comment and talk shit! Earn WOW!

Quit your day job and become a full-time shitposter and memer!

+

Commiserate with the gang in IRC, get high, and let your responsibilities fall to the wayside.

+

Join the revolution!

+
+
+
diff --git a/suchwow/templates/index.html b/suchwow/templates/index.html index db090c9..b65c41d 100644 --- a/suchwow/templates/index.html +++ b/suchwow/templates/index.html @@ -4,26 +4,20 @@
- - -

Latest Posts

+

Latest Memes

{% if posts %} {% for row in posts | batch(4) %}
{% for post in row %} + {% set post = post.show() %}
-
- Placeholder image + Placeholder image -
@@ -37,9 +31,9 @@
{{ post.text | truncate(60) }} - -

{{ post.get_received_wow() }} WOW received

+

{{ post.received_wow }} WOW received

+

({{ post.timestamp | humanize }})

@@ -52,16 +46,15 @@ {% endif %}
-{% if total_pages %} - -{% endif %} - + {% if total_pages %} + + {% endif %}
diff --git a/suchwow/templates/navbar.html b/suchwow/templates/navbar.html index f2a9417..f14602d 100755 --- a/suchwow/templates/navbar.html +++ b/suchwow/templates/navbar.html @@ -14,6 +14,17 @@