forked from rDrama/rDrama
move 2 cron functions from routes files to .cron
parent
83a931343e
commit
cfa6c13eaa
|
@ -1,16 +1,82 @@
|
||||||
from files.cli import g, app, db_session
|
from files.cli import g, app, db_session
|
||||||
import click
|
import click
|
||||||
import files.helpers.const as const
|
from files.helpers.const import *
|
||||||
|
|
||||||
import files.helpers.lottery as lottery
|
import files.helpers.lottery as lottery
|
||||||
import files.helpers.offsitementions as offsitementions
|
import files.helpers.offsitementions as offsitementions
|
||||||
import files.helpers.stats as stats
|
import files.helpers.stats as stats
|
||||||
import files.helpers.awards as awards
|
import files.helpers.awards as awards
|
||||||
import files.routes.static as route_static
|
import files.routes.static as route_static
|
||||||
from files.routes.subs import sub_inactive_purge_task
|
|
||||||
from files.routes.admin import give_monthly_marseybux_task
|
|
||||||
|
|
||||||
from sys import stdout
|
from sys import stdout
|
||||||
|
import datetime
|
||||||
|
import requests
|
||||||
|
|
||||||
|
|
||||||
|
def sub_inactive_purge_task():
|
||||||
|
if not HOLE_INACTIVITY_DELETION:
|
||||||
|
return False
|
||||||
|
|
||||||
|
one_week_ago = time.time() - 604800
|
||||||
|
active_holes = [x[0] for x in g.db.query(Submission.sub).distinct() \
|
||||||
|
.filter(Submission.sub != None, Submission.created_utc > one_week_ago).all()]
|
||||||
|
|
||||||
|
dead_holes = g.db.query(Sub).filter(Sub.name.notin_(active_holes)).all()
|
||||||
|
names = [x.name for x in dead_holes]
|
||||||
|
|
||||||
|
mods = g.db.query(Mod).filter(Mod.sub.in_(names)).all()
|
||||||
|
for x in mods:
|
||||||
|
send_repeatable_notification(x.user_id, f":marseyrave: /h/{x.sub} has been deleted for inactivity after one week without new posts. All posts in it have been moved to the main feed :marseyrave:")
|
||||||
|
|
||||||
|
admins = [x[0] for x in g.db.query(User.id).filter(User.admin_level > 1).all()]
|
||||||
|
for name in names:
|
||||||
|
for admin in admins:
|
||||||
|
send_repeatable_notification(admin, f":marseyrave: /h/{name} has been deleted for inactivity after one week without new posts. All posts in it have been moved to the main feed :marseyrave:")
|
||||||
|
|
||||||
|
posts = g.db.query(Submission).filter(Submission.sub.in_(names)).all()
|
||||||
|
for post in posts:
|
||||||
|
post.sub = None
|
||||||
|
g.db.add(post)
|
||||||
|
|
||||||
|
to_delete = mods \
|
||||||
|
+ g.db.query(Exile).filter(Exile.sub.in_(names)).all() \
|
||||||
|
+ g.db.query(SubBlock).filter(SubBlock.sub.in_(names)).all() \
|
||||||
|
+ g.db.query(SubSubscription).filter(SubSubscription.sub.in_(names)).all()
|
||||||
|
|
||||||
|
for x in to_delete:
|
||||||
|
g.db.delete(x)
|
||||||
|
g.db.flush()
|
||||||
|
|
||||||
|
for x in dead_holes:
|
||||||
|
g.db.delete(x)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def give_monthly_marseybux_task():
|
||||||
|
month = datetime.datetime.now() + datetime.timedelta(days=5)
|
||||||
|
month = month.strftime('%B')
|
||||||
|
|
||||||
|
data = {'access_token': GUMROAD_TOKEN}
|
||||||
|
|
||||||
|
emails = [x['email'] for x in requests.get(f'https://api.gumroad.com/v2/products/{GUMROAD_ID}/subscribers', data=data, timeout=5).json()["subscribers"]]
|
||||||
|
|
||||||
|
for u in g.db.query(User).filter(User.patron > 0, User.patron_utc == 0).all():
|
||||||
|
g.db.add(u)
|
||||||
|
if u.admin_level or u.id == A_ID or (u.email and u.email.lower() in emails):
|
||||||
|
procoins = procoins_li[u.patron]
|
||||||
|
u.procoins += procoins
|
||||||
|
send_repeatable_notification(u.id, f"@Snappy has given you {procoins} Marseybux for the month of {month}! You can use them to buy awards in the [shop](/shop).")
|
||||||
|
else: u.patron = 0
|
||||||
|
|
||||||
|
ma = ModAction(
|
||||||
|
kind="monthly",
|
||||||
|
user_id=SNAPPY_ID,
|
||||||
|
)
|
||||||
|
g.db.add(ma)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
@app.cli.command('cron', help='Run scheduled tasks.')
|
@app.cli.command('cron', help='Run scheduled tasks.')
|
||||||
@click.option('--every-5m', is_flag=True, help='Call every 5 minutes.')
|
@click.option('--every-5m', is_flag=True, help='Call every 5 minutes.')
|
||||||
|
@ -28,7 +94,7 @@ def cron(every_5m, every_1h, every_1d, every_1mo):
|
||||||
awards.award_timers_bots_task()
|
awards.award_timers_bots_task()
|
||||||
|
|
||||||
if every_1d:
|
if every_1d:
|
||||||
stats.generate_charts_task(const.SITE)
|
stats.generate_charts_task(SITE)
|
||||||
route_static.stats_cached()
|
route_static.stats_cached()
|
||||||
sub_inactive_purge_task()
|
sub_inactive_purge_task()
|
||||||
|
|
||||||
|
|
|
@ -20,35 +20,6 @@ import datetime
|
||||||
import requests
|
import requests
|
||||||
from urllib.parse import quote, urlencode
|
from urllib.parse import quote, urlencode
|
||||||
|
|
||||||
GUMROAD_ID = environ.get("GUMROAD_ID", "tfcvri").strip()
|
|
||||||
GUMROAD_TOKEN = environ.get("GUMROAD_TOKEN", "").strip()
|
|
||||||
|
|
||||||
|
|
||||||
def give_monthly_marseybux_task():
|
|
||||||
month = datetime.datetime.now() + datetime.timedelta(days=5)
|
|
||||||
month = month.strftime('%B')
|
|
||||||
|
|
||||||
data = {'access_token': GUMROAD_TOKEN}
|
|
||||||
|
|
||||||
emails = [x['email'] for x in requests.get(f'https://api.gumroad.com/v2/products/{GUMROAD_ID}/subscribers', data=data, timeout=5).json()["subscribers"]]
|
|
||||||
|
|
||||||
for u in g.db.query(User).filter(User.patron > 0, User.patron_utc == 0).all():
|
|
||||||
g.db.add(u)
|
|
||||||
if u.admin_level or u.id == A_ID or (u.email and u.email.lower() in emails):
|
|
||||||
procoins = procoins_li[u.patron]
|
|
||||||
u.procoins += procoins
|
|
||||||
send_repeatable_notification(u.id, f"@Snappy has given you {procoins} Marseybux for the month of {month}! You can use them to buy awards in the [shop](/shop).")
|
|
||||||
else: u.patron = 0
|
|
||||||
|
|
||||||
ma = ModAction(
|
|
||||||
kind="monthly",
|
|
||||||
user_id=SNAPPY_ID,
|
|
||||||
)
|
|
||||||
g.db.add(ma)
|
|
||||||
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
@app.post('/kippy')
|
@app.post('/kippy')
|
||||||
@admin_level_required(3)
|
@admin_level_required(3)
|
||||||
|
|
|
@ -442,45 +442,6 @@ def subs(v):
|
||||||
subs = g.db.query(Sub, func.count(Submission.sub)).outerjoin(Submission, Sub.name == Submission.sub).group_by(Sub.name).order_by(func.count(Submission.sub).desc()).all()
|
subs = g.db.query(Sub, func.count(Submission.sub)).outerjoin(Submission, Sub.name == Submission.sub).group_by(Sub.name).order_by(func.count(Submission.sub).desc()).all()
|
||||||
return render_template('sub/subs.html', v=v, subs=subs)
|
return render_template('sub/subs.html', v=v, subs=subs)
|
||||||
|
|
||||||
def sub_inactive_purge_task():
|
|
||||||
if not HOLE_INACTIVITY_DELETION:
|
|
||||||
return False
|
|
||||||
|
|
||||||
one_week_ago = time.time() - 604800
|
|
||||||
active_holes = [x[0] for x in g.db.query(Submission.sub).distinct() \
|
|
||||||
.filter(Submission.sub != None, Submission.created_utc > one_week_ago).all()]
|
|
||||||
|
|
||||||
dead_holes = g.db.query(Sub).filter(Sub.name.notin_(active_holes)).all()
|
|
||||||
names = [x.name for x in dead_holes]
|
|
||||||
|
|
||||||
mods = g.db.query(Mod).filter(Mod.sub.in_(names)).all()
|
|
||||||
for x in mods:
|
|
||||||
send_repeatable_notification(x.user_id, f":marseyrave: /h/{x.sub} has been deleted for inactivity after one week without new posts. All posts in it have been moved to the main feed :marseyrave:")
|
|
||||||
|
|
||||||
admins = [x[0] for x in g.db.query(User.id).filter(User.admin_level > 1).all()]
|
|
||||||
for name in names:
|
|
||||||
for admin in admins:
|
|
||||||
send_repeatable_notification(admin, f":marseyrave: /h/{name} has been deleted for inactivity after one week without new posts. All posts in it have been moved to the main feed :marseyrave:")
|
|
||||||
|
|
||||||
posts = g.db.query(Submission).filter(Submission.sub.in_(names)).all()
|
|
||||||
for post in posts:
|
|
||||||
post.sub = None
|
|
||||||
g.db.add(post)
|
|
||||||
|
|
||||||
to_delete = mods \
|
|
||||||
+ g.db.query(Exile).filter(Exile.sub.in_(names)).all() \
|
|
||||||
+ g.db.query(SubBlock).filter(SubBlock.sub.in_(names)).all() \
|
|
||||||
+ g.db.query(SubSubscription).filter(SubSubscription.sub.in_(names)).all()
|
|
||||||
|
|
||||||
for x in to_delete:
|
|
||||||
g.db.delete(x)
|
|
||||||
g.db.flush()
|
|
||||||
|
|
||||||
for x in dead_holes:
|
|
||||||
g.db.delete(x)
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
@app.post("/hole_pin/<pid>")
|
@app.post("/hole_pin/<pid>")
|
||||||
@auth_required
|
@auth_required
|
||||||
def hole_pin(v, pid):
|
def hole_pin(v, pid):
|
||||||
|
|
Loading…
Reference in New Issue