From 232efac2c10e5be869dc497a01be96984970fb82 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Thu, 15 Oct 2020 14:51:00 -0700 Subject: [PATCH] refactor to use shared function for posting and commenting --- suchwow/app.py | 26 +++----------------------- suchwow/reddit.py | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/suchwow/app.py b/suchwow/app.py index 8e7f060..6bca046 100644 --- a/suchwow/app.py +++ b/suchwow/app.py @@ -9,7 +9,7 @@ from suchwow import config from suchwow.models import Post, Profile, Comment, Notification, db from suchwow.routes import auth, comment, post, profile, leaderboard from suchwow.utils.decorators import login_required -from suchwow.reddit import Reddit +from suchwow.reddit import make_post from suchwow import wownero @@ -72,13 +72,7 @@ def reddit_recent(): diff = datetime.now() - post.timestamp recent_post = diff < timedelta(hours=2) if recent_post: - title = f"[SuchWow!][{post.submitter}][#{post.id}] {post.title}" - url = url_for("post.read", id=post.id) - reddit_post = Reddit().post(title, url) - post.reddit_url = reddit_post.url - post.save() - print(f"Posted #{post.id} to Reddit - {reddit_post.url}") - return True + make_post(post) @app.cli.command("reddit_random") def reddit_random(): @@ -86,21 +80,7 @@ def reddit_random(): wallet = wownero.Wallet() all_posts = Post.select().where(Post.reddit_url == None) post = choice(all_posts) - title = f"SuchWow #{post.id} - {post.title}" - url = url_for('post.uploaded_file', filename=post.image_name) - _comment = [ - f"'{post.text}'\n\nSubmitter: {post.submitter}\n", - f"Timestamp (UTC): {post.timestamp}\n", - "\nShow this poster some love by sending WOW to the following address: ", - f"{wallet.get_address(account=post.account_index)}" - ] - comment = "".join(_comment) - reddit_post = Reddit().post(title, url) - reddit_comment = Reddit().comment(reddit_post, comment) - post.reddit_url = reddit_post.url - post.save() - print(f"Posted #{post.id} to Reddit - {reddit_post.url}") - return True + make_post(post) @app.cli.command("payout_users") def payout_users(): diff --git a/suchwow/reddit.py b/suchwow/reddit.py index eaebc4a..24e6b54 100644 --- a/suchwow/reddit.py +++ b/suchwow/reddit.py @@ -1,5 +1,5 @@ import praw -from suchwow import config +from suchwow import config, wownero class Reddit(object): @@ -30,3 +30,21 @@ class Reddit(object): return _comment except: return False + +def make_post(post): + wallet = wownero.Wallet() + title = f"SuchWow #{post.id} - {post.title}" + url = url_for('post.uploaded_file', filename=post.image_name) + _comment = [ + f"Submitter: {post.submitter}\n", + f"Timestamp (UTC): {post.timestamp}\n", + "\nShow this poster some love by sending WOW to the following address:\n\n", + f"{wallet.get_address(account=post.account_index)}" + ] + comment = "".join(_comment) + reddit_post = Reddit().post(title, url) + reddit_comment = Reddit().comment(reddit_post, comment) + post.reddit_url = reddit_post.url + post.save() + print(f"Posted #{post.id} to Reddit - {reddit_post.url}") + return True