From bc0d83b832316c493cccbd61897dbb044c5b0658 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Wed, 14 Oct 2020 23:51:53 -0700 Subject: [PATCH] setup praw and commands to post to reddit --- requirements.txt | 1 + suchwow/app.py | 29 +++++++++++++++++++++++++++++ suchwow/config.example.py | 6 ++++++ suchwow/models.py | 2 +- suchwow/reddit.py | 31 +++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 suchwow/reddit.py diff --git a/requirements.txt b/requirements.txt index ae2d8ea..d0006cc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ flask-session peewee gunicorn six +praw diff --git a/suchwow/app.py b/suchwow/app.py index 7786852..ea3ecaf 100644 --- a/suchwow/app.py +++ b/suchwow/app.py @@ -1,4 +1,6 @@ import json +from datetime import datetime, timedelta +from random import choice from os import makedirs from flask import Flask, request, session, redirect from flask import render_template, flash, url_for @@ -61,6 +63,33 @@ def create_accounts(): account = wallet.new_account() print(f"Created account {account}") +@app.cli.command("reddit_recent") +def reddit_recent(): + # run every 5 mins + all_posts = Post.select().order_by(Post.timestamp.desc()).where(Post.reddit_url == None) + for post in all_posts: + 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() + return True + +@app.cli.command("reddit_random") +def reddit_random(): + # run every 8 hours + all_posts = Post.select().where(Post.reddit_url == None) + post = choice(all_posts) + 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() + return True + @app.cli.command("payout_users") def payout_users(): wallet = wownero.Wallet() diff --git a/suchwow/config.example.py b/suchwow/config.example.py index 95931a0..f888b4c 100644 --- a/suchwow/config.example.py +++ b/suchwow/config.example.py @@ -12,3 +12,9 @@ WALLET_PORT = 8888 WALLET_PROTO = 'http' WALLET_USER = 'suchwow' WALLET_PASS = 'zzzzzzzzzzzzzzz' +PRAW_CLIENT_SECRET = 'xxxxxxxx' +PRAW_CLIENT_ID = 'xxxxxxxx' +PRAW_USER_AGENT = 'suchwow-yyyy-python' +PRAW_USERNAME = 'xxxxxxxx' +PRAW_PASSWORD = 'xxxxxxxx' +SERVER_NAME = 'suchwow.xyz' diff --git a/suchwow/models.py b/suchwow/models.py index 640ac0e..fe5d5de 100644 --- a/suchwow/models.py +++ b/suchwow/models.py @@ -9,7 +9,6 @@ class Post(Model): id = AutoField() title = CharField() text = CharField() - # submitter = ForeignKeyField(Profile, field=Profile.username) submitter = CharField() image_name = CharField() readonly = BooleanField(default=False) @@ -17,6 +16,7 @@ class Post(Model): account_index = IntegerField() address_index = IntegerField() timestamp = DateTimeField(default=datetime.now) + reddit_url = CharField(null=True) class Meta: database = db diff --git a/suchwow/reddit.py b/suchwow/reddit.py new file mode 100644 index 0000000..63d3bcd --- /dev/null +++ b/suchwow/reddit.py @@ -0,0 +1,31 @@ +from praw import Reddit +from suchwow import config + + +class Reddit(object): + def __init__(self): + self.reddit = Reddit( + client_id=config.get("PRAW_CLIENT_ID"), + client_secret=config.get("PRAW_CLIENT_SECRET"), + user_agent=config.get("PRAW_USER_AGENT"), + username=config.get("PRAW_USERNAME"), + password=config.get("PRAW_PASSWORD") + ) + self.subreddit = "wownero" + + def post(title, url): + try: + submission = reddit.subreddit(self.subreddit).submit( + title=title, + url=url, + resubmit=False, + ) + return submission + except: + return False + + def comment(submission, post): + try: + submission.reply(f"Show this post love by sending WOW: {post.id}") + except: + return False