diff --git a/suchwow/app.py b/suchwow/app.py index d3eaa54..35f54ba 100644 --- a/suchwow/app.py +++ b/suchwow/app.py @@ -12,7 +12,7 @@ from suchwow import config from suchwow.models import Post, Profile, Comment, Notification, db, Moderator from suchwow.routes import auth, comment, post, profile, leaderboard, api from suchwow.utils.decorators import login_required, moderator_required -from suchwow.utils.helpers import post_webhook, get_activity +from suchwow.utils.helpers import post_webhook, get_latest_tipped_posts from suchwow.reddit import make_post from suchwow.discord import post_discord_webhook from suchwow import wownero, filters @@ -38,6 +38,14 @@ def index(): submitter = request.args.get("submitter", None) content = request.args.get("content", None) + if content != 'feed': + posts = get_latest_tipped_posts() + return render_template( + "index.html", + posts=posts[0:30], + title="Latest Tipped Memes" + ) + try: page = int(page) except: @@ -54,9 +62,11 @@ def index(): "index.html", posts=paginated_posts, page=page, - total_pages=total_pages + total_pages=total_pages, + title="Latest Memes" ) + @app.route("/mod") @moderator_required def mod_queue(): diff --git a/suchwow/templates/index.html b/suchwow/templates/index.html index b65c41d..db61d46 100644 --- a/suchwow/templates/index.html +++ b/suchwow/templates/index.html @@ -4,7 +4,13 @@
-

Latest Memes

+

{% if title %}{{ title }}{% else %}Latest Memes{% endif %}

+ + {% if request.args.content != 'feed' %} + View Latest Memes + {% else %} + View Latest Tipped Posts + {% endif %}
{% if posts %} @@ -25,7 +31,7 @@

{{ post.title }}

-

{{ post.submitter }}

+

{{ post.submitter }}

@@ -50,7 +56,7 @@ diff --git a/suchwow/templates/post/read.html b/suchwow/templates/post/read.html index c05e3c3..ff778d5 100644 --- a/suchwow/templates/post/read.html +++ b/suchwow/templates/post/read.html @@ -24,7 +24,7 @@ {% endif %} -

Submitted by {{ post.submitter }} at {{ post.timestamp }}

+

Submitted by {{ post.submitter }} at {{ post.timestamp }}

diff --git a/suchwow/utils/helpers.py b/suchwow/utils/helpers.py index a470b76..d25c3c7 100644 --- a/suchwow/utils/helpers.py +++ b/suchwow/utils/helpers.py @@ -41,26 +41,36 @@ def post_webhook(msg): except: return False -def get_activity(): - posts = Post.select() - w = Wallet() - data = {} - for p in posts: - data[p.timestamp] = {'type': 'post', 'post': p} - for tx in w.incoming_transfers(p.account_index): - if 'timestamp' in tx: - data[tx['timestamp']] = {'type': 'tx', 'post': p} +def get_latest_tipped_posts(): + key_name = 'latest_tips' + tipped_posts = rw_cache(key_name, None, 1200) - dates = sorted(data, reverse=True) - new_data = [] - for d in dates: - new_data.append(data[d]['post']) + if not tipped_posts: + new_data = [] + w = Wallet() + data = {} + for acc in w.accounts(): + txes = w.transfers(acc) + if 'in' in txes: + for tx in txes['in']: + p = Post.select().where( + Post.account_index==acc + ).first() + if p: + data[tx['timestamp']] = p - return new_data[0:20] + dates = sorted(data, reverse=True) + for d in dates: + if not data[d] in new_data: + new_data.append(data[d]) + + tipped_posts = rw_cache(key_name, new_data, 1200) + + return tipped_posts # Use hacky filesystem cache since i dont feel like shipping redis -def rw_cache(key_name, data=None): +def rw_cache(key_name, data=None, diff_seconds=3600): pickle_file = path.join(config.DATA_FOLDER, f'{key_name}.pkl') try: if path.isfile(pickle_file): @@ -70,7 +80,7 @@ def rw_cache(key_name, data=None): 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: + if diff.seconds < diff_seconds: print(f'unpickling {key_name}') with open(pickle_file, 'rb') as f: pickled_data = pickle.load(f) diff --git a/suchwow/wownero.py b/suchwow/wownero.py index 61d9af0..9c7cfd5 100644 --- a/suchwow/wownero.py +++ b/suchwow/wownero.py @@ -53,15 +53,8 @@ class Wallet(object): return self.make_wallet_rpc('query_key', {'key_type': 'mnemonic'})['key'] def accounts(self): - accounts = [] _accounts = self.make_wallet_rpc('get_accounts') - idx = 0 - self.master_address = _accounts['subaddress_accounts'][0]['base_address'] - for _acc in _accounts['subaddress_accounts']: - assert idx == _acc['account_index'] - accounts.append(_acc['account_index']) - idx += 1 - return accounts + return [i['account_index'] for i in _accounts['subaddress_accounts']] def new_account(self, label=None): _account = self.make_wallet_rpc('create_account', {'label': label})