suchwow/suchwow/reddit.py

62 lines
1.9 KiB
Python
Raw Permalink Normal View History

2020-10-15 08:14:50 +01:00
import praw
from flask import url_for
from suchwow import config, wownero
class Reddit(object):
def __init__(self):
2020-10-15 08:14:50 +01:00
self.reddit = praw.Reddit(
2020-10-15 08:00:51 +01:00
client_id=config.PRAW_CLIENT_ID,
client_secret=config.PRAW_CLIENT_SECRET,
user_agent=config.PRAW_USER_AGENT,
username=config.PRAW_USERNAME,
password=config.PRAW_PASSWORD
)
self.subreddit = "wownero"
self.meme_flair_id = "2527d518-a96c-11ea-ba87-0e32c68cff8f"
2020-10-15 08:14:50 +01:00
def post(self, title, url):
try:
2020-10-15 08:14:50 +01:00
submission = self.reddit.subreddit(self.subreddit).submit(
title=title,
url=url,
resubmit=False,
flair_id=self.meme_flair_id
)
return submission
except:
return False
def comment(self, submission, comment):
try:
_comment = submission.reply(comment)
return _comment
except:
return False
def make_post(post):
2021-05-14 22:17:37 +01:00
if post.to_reddit:
print(f"Already posted #{post.id} to Reddit")
return False
wallet = wownero.Wallet()
title = f"SuchWow #{post.id} - {post.title}"
2020-12-10 21:59:35 +00:00
url = url_for('post.uploaded_file', filename=post.image_name, _external=True)
_comment = [
2020-10-15 23:00:46 +01:00
f"Submitter: {post.submitter}\n\n",
f"Timestamp (UTC): {post.timestamp}\n\n",
"Show this poster some love by sending WOW to the following address:\n\n",
2020-10-15 23:02:43 +01:00
f"`{wallet.get_address(account=post.account_index)}`\n\n\n\n",
f"[View Post]({url_for('post.read', id=post.id, _external=True)})"
]
comment = "".join(_comment)
reddit_post = Reddit().post(title, url)
reddit_comment = Reddit().comment(reddit_post, comment)
2020-10-18 05:39:19 +01:00
if reddit_post:
post.to_reddit = True
post.save()
print(f"Posted #{post.id} to Reddit")
return True
else:
print(f"Unable to post #{post.id} to Reddit")
return False