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 from flask_session import Session 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 import wownero app = Flask(__name__) app.config.from_envvar("FLASK_SECRETS") app.secret_key = app.config["SECRET_KEY"] Session(app) app.register_blueprint(post.bp) app.register_blueprint(auth.bp) app.register_blueprint(profile.bp) app.register_blueprint(comment.bp) app.register_blueprint(leaderboard.bp) @app.route("/") def index(): itp = 20 page = request.args.get("page", 1) try: page = int(page) except: flash("Wow, wtf hackerman. Cool it.") page = 1 posts = Post.select().order_by(Post.timestamp.desc()).paginate(page, itp) total_pages = Post.select().count() / itp return render_template("index.html", posts=posts, page=page, total_pages=total_pages) @app.route("/about") def about(): return render_template("about.html") @app.errorhandler(404) def not_found(error): flash("nothing there, brah") return redirect(url_for("index")) @app.cli.command("init") def init(): # create subdirs for i in ["uploads", "db", "wallet"]: makedirs(f"{config.DATA_FOLDER}/{i}", exist_ok=True) # init db db.create_tables([Post, Profile, Comment, Notification]) @app.cli.command("create_accounts") def create_accounts(): wallet = wownero.Wallet() for post in Post.select(): if post.account_index not in wallet.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() print(f"Posted #{post.id} to Reddit - {reddit_post.url}") 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() print(f"Posted #{post.id} to Reddit - {reddit_post.url}") return True @app.cli.command("payout_users") def payout_users(): wallet = wownero.Wallet() for post in Post.select(): submitter = Profile.get(username=post.submitter) balances = wallet.balances(post.account_index) if balances[1] > 0: print(f"Post #{post.id} has {balances[1]} funds unlocked and ready to send. Sweeping all funds to user's address ({submitter.address}).") sweep = wallet.sweep_all(account=post.account_index, dest_address=submitter.address) print(sweep) if __name__ == "__main__": app.run()