2020-07-15 08:18:13 +00:00
|
|
|
import json
|
2020-11-22 20:29:29 +00:00
|
|
|
import click
|
2021-04-30 02:27:21 +00:00
|
|
|
import arrow
|
2021-04-16 21:23:59 +00:00
|
|
|
from math import ceil
|
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
|
2021-11-24 05:55:38 +00:00
|
|
|
from suchwow.models import Post, Profile, Comment, Notification, db, Moderator, Ban
|
2021-01-05 22:01:51 +00:00
|
|
|
from suchwow.routes import auth, comment, post, profile, leaderboard, api
|
2020-12-04 07:17:58 +00:00
|
|
|
from suchwow.utils.decorators import login_required, moderator_required
|
2021-05-06 16:53:18 +00:00
|
|
|
from suchwow.utils.helpers import post_webhook, get_latest_tipped_posts
|
2021-06-08 15:31:11 +00:00
|
|
|
from suchwow.utils.helpers import get_top_posters, get_top_posts
|
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
|
2021-04-30 02:27:21 +00:00
|
|
|
from suchwow import wownero, filters
|
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)
|
2021-01-05 22:01:51 +00:00
|
|
|
app.register_blueprint(api.bp)
|
2021-04-30 02:27:21 +00:00
|
|
|
app.register_blueprint(filters.bp)
|
2020-07-15 08:18:13 +00:00
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def index():
|
2021-04-16 21:23:59 +00:00
|
|
|
itp = 15
|
2020-08-08 06:12:33 +00:00
|
|
|
page = request.args.get("page", 1)
|
2020-10-25 07:00:20 +00:00
|
|
|
submitter = request.args.get("submitter", None)
|
2021-04-30 03:55:34 +00:00
|
|
|
content = request.args.get("content", None)
|
|
|
|
|
2021-05-06 17:46:15 +00:00
|
|
|
if content == 'latest_tipped':
|
2021-05-06 16:53:18 +00:00
|
|
|
posts = get_latest_tipped_posts()
|
|
|
|
return render_template(
|
|
|
|
"index.html",
|
|
|
|
posts=posts[0:30],
|
|
|
|
title="Latest Tipped Memes"
|
|
|
|
)
|
|
|
|
|
2020-08-08 06:12:33 +00:00
|
|
|
try:
|
|
|
|
page = int(page)
|
|
|
|
except:
|
2021-11-08 09:04:58 +00:00
|
|
|
flash("Wow, wtf hackerman. Cool it.", "is-danger")
|
2020-07-16 07:41:19 +00:00
|
|
|
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:
|
2021-04-30 03:55:34 +00:00
|
|
|
posts = posts.where(Post.submitter==submitter)
|
2021-04-16 21:23:59 +00:00
|
|
|
|
|
|
|
paginated_posts = posts.paginate(page, itp)
|
|
|
|
total_pages = ceil(posts.count() / itp)
|
|
|
|
return render_template(
|
|
|
|
"index.html",
|
|
|
|
posts=paginated_posts,
|
|
|
|
page=page,
|
2021-05-06 16:53:18 +00:00
|
|
|
total_pages=total_pages,
|
|
|
|
title="Latest Memes"
|
2021-04-16 21:23:59 +00:00
|
|
|
)
|
2020-07-15 08:18:13 +00:00
|
|
|
|
2021-05-06 16:53:18 +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
|
|
|
|
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
|
2021-11-24 05:55:38 +00:00
|
|
|
db.create_tables([Post, Profile, Comment, Notification, Moderator, Ban])
|
2020-07-15 08:18:13 +00:00
|
|
|
|
2021-05-14 07:40:01 +00:00
|
|
|
@app.cli.command("post_reddit")
|
|
|
|
@click.argument('last_hours')
|
|
|
|
def post_reddit(last_hours):
|
|
|
|
posts = Post.select().where(
|
|
|
|
Post.approved==True,
|
|
|
|
Post.to_reddit==False
|
|
|
|
).order_by(Post.timestamp.asc())
|
|
|
|
for p in posts:
|
|
|
|
if p.hours_elapsed() < int(last_hours):
|
2021-05-15 16:03:26 +00:00
|
|
|
if not p.to_reddit:
|
|
|
|
_p = make_post(p)
|
|
|
|
if _p:
|
2021-05-15 16:04:14 +00:00
|
|
|
p.to_reddit = True
|
|
|
|
p.save()
|
2021-05-15 19:19:37 +00:00
|
|
|
return
|
2021-05-14 07:40:01 +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-08-14 06:42:43 +00:00
|
|
|
@app.cli.command("payout_users")
|
|
|
|
def payout_users():
|
|
|
|
wallet = wownero.Wallet()
|
2020-12-14 06:28:23 +00:00
|
|
|
_fa = wownero.from_atomic
|
|
|
|
_aw = wownero.as_wownero
|
2020-08-14 06:42:43 +00:00
|
|
|
for post in Post.select():
|
|
|
|
submitter = Profile.get(username=post.submitter)
|
|
|
|
balances = wallet.balances(post.account_index)
|
2020-12-14 06:28:23 +00:00
|
|
|
url = url_for('post.read', id=post.id, _external=True)
|
2021-07-08 19:20:54 +00:00
|
|
|
if balances[1] > 0.1:
|
2020-08-14 06:42:43 +00:00
|
|
|
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-12-14 06:28:23 +00:00
|
|
|
if "tx_hash_list" in sweep:
|
|
|
|
amount = 0
|
|
|
|
for amt in sweep["amount_list"]:
|
|
|
|
amount += int(amt)
|
2021-05-07 13:34:13 +00:00
|
|
|
# post_webhook(f"Paid out :moneybag: {_aw(_fa(amount))} WOW to `{post.submitter}` for post [{post.id}]({url})")
|
|
|
|
# god damn you eimernase, why'd you ruin this for me? :P
|
2020-11-22 20:29:29 +00:00
|
|
|
|
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}")
|
2020-12-14 06:28:23 +00:00
|
|
|
post_webhook(f"Moderator `{username}` added :ship_it_parrot: by console :black_flag:")
|
2020-12-04 07:17:58 +00:00
|
|
|
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}")
|
2020-12-14 06:28:23 +00:00
|
|
|
post_webhook(f"Moderator `{username}` removed :excuseme: by console :black_flag:")
|
2020-12-04 07:17:58 +00:00
|
|
|
else:
|
|
|
|
print("That moderator doesn't exist")
|
|
|
|
|
2021-11-24 05:55:38 +00:00
|
|
|
@app.cli.command("ban_user")
|
|
|
|
@click.argument("username")
|
|
|
|
@click.argument("reason", nargs=-1)
|
|
|
|
def ban_user(username, reason):
|
|
|
|
u = Profile.filter(username=username).first()
|
|
|
|
b = Ban.filter(user=u).first()
|
|
|
|
if b:
|
|
|
|
print("User already banned")
|
|
|
|
return False
|
|
|
|
if u:
|
|
|
|
b = Ban(user=u, reason=' '.join(reason))
|
|
|
|
b.save()
|
|
|
|
print(f"Banned {username}")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
print("That user doesn't exist")
|
|
|
|
return
|
|
|
|
|
|
|
|
@app.cli.command("unban_user")
|
|
|
|
@click.argument("username")
|
|
|
|
def ban_user(username):
|
|
|
|
u = Profile.filter(username=username).first()
|
|
|
|
if not u:
|
|
|
|
print("That user doesn't exist")
|
|
|
|
return False
|
|
|
|
b = Ban.filter(user=u).first()
|
|
|
|
if b:
|
|
|
|
b.delete_instance()
|
|
|
|
print(f"Unbanned {username}")
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
print("That user isn't banned")
|
|
|
|
return False
|
|
|
|
|
2021-04-15 17:37:15 +00:00
|
|
|
@app.cli.command("show")
|
|
|
|
@click.argument("post_id")
|
|
|
|
def post_id(post_id):
|
|
|
|
p = Post.filter(id=post_id).first()
|
|
|
|
if p:
|
|
|
|
print(p.show())
|
|
|
|
else:
|
|
|
|
print("That post doesn't exist")
|
|
|
|
|
2021-06-08 15:31:11 +00:00
|
|
|
@app.cli.command("load_cache")
|
|
|
|
def load_cache():
|
|
|
|
app.logger.info('loading top posters into cache')
|
|
|
|
get_top_posters()
|
|
|
|
app.logger.info('done')
|
2021-07-08 19:41:15 +00:00
|
|
|
app.logger.info('loading latest tipped into cache')
|
|
|
|
get_latest_tipped_posts()
|
|
|
|
app.logger.info('done')
|
2021-06-08 15:31:11 +00:00
|
|
|
for i in [1, 3, 7, 30]:
|
|
|
|
app.logger.info(f'loading top posts last {i} days into cache')
|
|
|
|
get_top_posts(i)
|
|
|
|
app.logger.info('done')
|
|
|
|
|
2021-04-30 02:27:21 +00:00
|
|
|
|
2020-07-15 08:18:13 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run()
|