You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
suchwow/suchwow/cli.py

138 lines
4.5 KiB

from os import makedirs, getenv
from random import choice
import lorem
from flask import Blueprint
from suchwow._models import db, User, Post, AuditEvent, TipSent, TipReceived, Vote
from suchwow.models import Post as OldPost
from suchwow import wownero
from suchwow import config
bp = Blueprint('cli', 'cli', cli_group=None)
@bp.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([User, Post, AuditEvent, TipSent, TipReceived, Vote])
@bp.cli.command('generate_data')
def generate_data():
if getenv('FLASK_DEBUG', 0) == '1':
users = ['lza_menace', 'wowario', 'jwinterm', 'dsc', 'asymptotically']
for user in users:
moderator = False
if not User.select().where(User.username == user):
if user == 'lza_menace':
moderator = True
User.create(
username=user,
moderator=moderator
)
print(f'Created user {user}')
for i in range(1, 5):
wallet = wownero.Wallet()
address_idx, address = wallet.new_address(config.WALLET_ACCOUNT)
wallet.store()
Post.create(
title=lorem.sentence(),
text=lorem.sentence(),
user=choice(list(User.select())),
image_name='test.jpg',
account_index=config.WALLET_ACCOUNT,
address_index=address_idx,
address=address
)
@bp.cli.command('rescan')
def rescan():
wallet = wownero.Wallet()
wallet.make_wallet_rpc('rescan_blockchain')
@bp.cli.command('save')
def rescan():
wallet = wownero.Wallet()
wallet.make_wallet_rpc('store')
print('Saved wallet.')
@bp.cli.command("create_accounts")
def create_accounts():
wallet = wownero.Wallet()
highest_account = OldPost.select().order_by(OldPost.timestamp.desc()).first().account_index
print(f'Highest post account index is {highest_account} but highest wallet account is {wallet.accounts()[-1]}. Generating new accounts!')
while wallet.accounts()[-1] < highest_account:
account = wallet.new_account()
print(f"Created account {account}")
wallet.make_wallet_rpc('store')
# @bp.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):
# if not p.to_reddit:
# _p = make_post(p)
# if _p:
# p.to_reddit = True
# p.save()
# return
# @bp.cli.command("payout_users")
# def payout_users():
# wallet = wownero.Wallet()
# _fa = wownero.from_atomic
# _aw = wownero.as_wownero
# for post in Post.select():
# try:
# submitter = Profile.get(username=post.submitter)
# balances = wallet.balances(post.account_index)
# url = url_for('post.read', id=post.id, _external=True)
# if balances[1] > 0.05:
# 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 "tx_hash_list" in sweep:
# amount = 0
# for amt in sweep["amount_list"]:
# amount += int(amt)
# except Exception as e:
# print(f"Failed because: {e}")
# @bp.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")
# @bp.cli.command("load_cache")
# def load_cache():
# current_app.logger.info('loading top posters into cache')
# get_top_posters()
# current_app.logger.info('done')
# current_app.logger.info('loading latest tipped into cache')
# get_latest_tipped_posts()
# current_app.logger.info('done')
# for i in [1, 3, 7, 30, 9999]:
# current_app.logger.info(f'loading top posts last {i} days into cache')
# get_top_posts(i)
# current_app.logger.info('done')