2020-07-15 08:18:13 +00:00
|
|
|
import json
|
2020-11-22 20:29:29 +00:00
|
|
|
import click
|
2020-10-15 06:51:53 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from random import choice
|
2020-11-22 20:29:29 +00:00
|
|
|
from os import makedirs, path, remove
|
2020-09-24 05:13:28 +00:00
|
|
|
from flask import Flask, request, session, redirect
|
|
|
|
from flask import render_template, flash, url_for
|
2020-07-15 08:18:13 +00:00
|
|
|
from flask_session import Session
|
2020-08-10 05:47:14 +00:00
|
|
|
from suchwow import config
|
2020-12-04 07:17:58 +00:00
|
|
|
from suchwow.models import Post, Profile, Comment, Notification, db, Moderator
|
2020-09-16 18:32:16 +00:00
|
|
|
from suchwow.routes import auth, comment, post, profile, leaderboard
|
2020-12-04 07:17:58 +00:00
|
|
|
from suchwow.utils.decorators import login_required, moderator_required
|
2020-10-15 21:51:00 +00:00
|
|
|
from suchwow.reddit import make_post
|
2020-10-29 07:57:08 +00:00
|
|
|
from suchwow.discord import post_discord_webhook
|
2020-08-14 06:42:43 +00:00
|
|
|
from suchwow import wownero
|
2020-07-15 08:18:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.config.from_envvar("FLASK_SECRETS")
|
|
|
|
app.secret_key = app.config["SECRET_KEY"]
|
|
|
|
Session(app)
|
|
|
|
|
2020-07-29 00:24:59 +00:00
|
|
|
app.register_blueprint(post.bp)
|
2020-07-30 08:36:31 +00:00
|
|
|
app.register_blueprint(auth.bp)
|
2020-08-10 06:59:45 +00:00
|
|
|
app.register_blueprint(profile.bp)
|
2020-08-10 16:54:44 +00:00
|
|
|
app.register_blueprint(comment.bp)
|
2020-09-16 18:32:16 +00:00
|
|
|
app.register_blueprint(leaderboard.bp)
|
2020-07-15 08:18:13 +00:00
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def index():
|
2020-08-08 06:12:33 +00:00
|
|
|
itp = 20
|
|
|
|
page = request.args.get("page", 1)
|
2020-10-25 07:00:20 +00:00
|
|
|
submitter = request.args.get("submitter", None)
|
2020-08-08 06:12:33 +00:00
|
|
|
try:
|
|
|
|
page = int(page)
|
|
|
|
except:
|
2020-07-16 07:41:19 +00:00
|
|
|
flash("Wow, wtf hackerman. Cool it.")
|
|
|
|
page = 1
|
|
|
|
|
2020-12-04 07:17:58 +00:00
|
|
|
posts = Post.select().where(Post.approved==True).order_by(Post.timestamp.desc())
|
2020-10-25 07:00:20 +00:00
|
|
|
if submitter:
|
|
|
|
posts = posts.where(Post.submitter == submitter)
|
|
|
|
posts = posts.paginate(page, itp)
|
2020-09-17 17:43:08 +00:00
|
|
|
total_pages = Post.select().count() / itp
|
2020-08-08 06:12:33 +00:00
|
|
|
return render_template("index.html", posts=posts, page=page, total_pages=total_pages)
|
2020-07-15 08:18:13 +00:00
|
|
|
|
2020-12-04 07:17:58 +00:00
|
|
|
@app.route("/mod")
|
|
|
|
@moderator_required
|
|
|
|
def mod_queue():
|
|
|
|
posts = Post.select().where(Post.approved==False).order_by(Post.timestamp.asc())
|
|
|
|
return render_template("index.html", posts=posts)
|
|
|
|
|
2020-08-08 06:12:33 +00:00
|
|
|
@app.route("/about")
|
|
|
|
def about():
|
|
|
|
return render_template("about.html")
|
2020-07-15 08:18:13 +00:00
|
|
|
|
|
|
|
@app.errorhandler(404)
|
|
|
|
def not_found(error):
|
2020-09-24 05:13:28 +00:00
|
|
|
flash("nothing there, brah")
|
|
|
|
return redirect(url_for("index"))
|
2020-07-15 08:18:13 +00:00
|
|
|
|
2020-08-10 05:47:14 +00:00
|
|
|
@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
|
2020-12-04 07:17:58 +00:00
|
|
|
db.create_tables([Post, Profile, Comment, Notification, Moderator])
|
2020-07-15 08:18:13 +00:00
|
|
|
|
2020-10-15 04:26:42 +00:00
|
|
|
@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}")
|
|
|
|
|
2020-10-29 07:57:08 +00:00
|
|
|
@app.cli.command("post_new_memes")
|
|
|
|
def post_new_memes():
|
2020-10-15 06:51:53 +00:00
|
|
|
# run every 5 mins
|
2020-10-29 08:24:55 +00:00
|
|
|
all_posts = Post.select().order_by(Post.timestamp.desc()).where(
|
|
|
|
(Post.to_reddit == False) | (Post.to_discord == False)
|
|
|
|
)
|
2020-10-15 06:51:53 +00:00
|
|
|
for post in all_posts:
|
|
|
|
diff = datetime.now() - post.timestamp
|
|
|
|
recent_post = diff < timedelta(hours=2)
|
|
|
|
if recent_post:
|
2020-10-29 08:24:55 +00:00
|
|
|
if not post.to_reddit:
|
|
|
|
make_post(post)
|
|
|
|
if not post.to_discord:
|
|
|
|
post_discord_webhook(post)
|
2020-10-18 05:00:13 +00:00
|
|
|
return
|
2020-10-15 06:51:53 +00:00
|
|
|
|
2020-12-04 18:57:34 +00:00
|
|
|
@app.cli.command("post_meme")
|
|
|
|
@click.argument("post_id")
|
2020-12-10 21:12:21 +00:00
|
|
|
def post_meme(post_id):
|
2020-12-04 18:57:34 +00:00
|
|
|
post = Post.get(id=post_id)
|
2020-12-10 21:23:26 +00:00
|
|
|
make_post(post)
|
|
|
|
post_discord_webhook(post)
|
2020-12-04 18:57:34 +00:00
|
|
|
return
|
|
|
|
|
2020-10-15 06:51:53 +00:00
|
|
|
@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)
|
2020-10-15 21:51:00 +00:00
|
|
|
make_post(post)
|
2020-10-15 06:51:53 +00:00
|
|
|
|
2020-08-14 06:42:43 +00:00
|
|
|
@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)
|
|
|
|
|
2020-11-22 20:29:29 +00:00
|
|
|
@app.cli.command("delete_post")
|
|
|
|
@click.argument("post_id")
|
2020-12-04 07:17:58 +00:00
|
|
|
def _delete_post(post_id):
|
2020-11-22 20:29:29 +00:00
|
|
|
post = Post.get(id=post_id)
|
|
|
|
save_path_base = path.join(app.config["DATA_FOLDER"], "uploads")
|
|
|
|
save_path = path.join(save_path_base, post.image_name)
|
|
|
|
post.delete_instance()
|
|
|
|
remove(save_path)
|
|
|
|
print(f"Deleted post {post_id} and image {save_path}")
|
|
|
|
|
2020-12-04 07:17:58 +00:00
|
|
|
@app.cli.command("add_admin")
|
|
|
|
@click.argument("username")
|
|
|
|
def add_admin(username):
|
|
|
|
if not Moderator.filter(username=username):
|
|
|
|
m = Moderator(username=username)
|
|
|
|
m.save()
|
|
|
|
print(f"Added {username}")
|
|
|
|
else:
|
|
|
|
print("That moderator already exists")
|
|
|
|
|
|
|
|
@app.cli.command("remove_admin")
|
|
|
|
@click.argument("username")
|
2020-12-04 07:21:23 +00:00
|
|
|
def remove_admin(username):
|
2020-12-04 07:17:58 +00:00
|
|
|
m = Moderator.filter(username=username).first()
|
|
|
|
if m:
|
|
|
|
m.delete_instance()
|
|
|
|
print(f"Deleted {username}")
|
|
|
|
else:
|
|
|
|
print("That moderator doesn't exist")
|
|
|
|
|
2020-07-15 08:18:13 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run()
|