From d84a4c7ce91342e3f1620c48d3854a9f5d5c304c Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Fri, 10 Jun 2022 22:23:01 +0200 Subject: [PATCH] convert monthly marseybux into a cron task --- files/helpers/cron.py | 7 +++- files/routes/admin.py | 69 ++++++++++++++----------------- files/templates/admin/awards.html | 5 --- 3 files changed, 37 insertions(+), 44 deletions(-) diff --git a/files/helpers/cron.py b/files/helpers/cron.py index 9012ecdde..be6247d12 100644 --- a/files/helpers/cron.py +++ b/files/helpers/cron.py @@ -8,12 +8,14 @@ import files.helpers.stats as stats import files.helpers.awards as awards 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 @app.cli.command('cron', help='Run scheduled tasks.') @click.option('--every-5m', is_flag=True, help='Call every 5 minutes.') @click.option('--every-1h', is_flag=True, help='Call every 1 hour.') @click.option('--every-1d', is_flag=True, help='Call every 1 day.') -def cron(every_5m, every_1h, every_1d): +@click.option('--every-1mo', is_flag=True, help='Call every 1 month.') +def cron(every_5m, every_1h, every_1d, every_1mo): g.db = db_session() if every_5m: @@ -29,4 +31,5 @@ def cron(every_5m, every_1h, every_1d): awards.award_timers_bots_task() sub_inactive_purge_task() - + if every_1mo: + give_monthly_marseybux_task() \ No newline at end of file diff --git a/files/routes/admin.py b/files/routes/admin.py index 628b45786..ac55c4577 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -22,6 +22,38 @@ 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.id in (DAD_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 + + if SITE == 'pcmemes.net': + u = g.db.query(User).filter_by(id=KIPPY_ID).one() + u.procoins += 50000 + g.db.add(u) + + ma = ModAction( + kind="monthly", + user_id=v.id + ) + g.db.add(ma) + + g.db.commit() + + return True + @app.get('/admin/loggedin') @admin_level_required(2) def loggedin_list(v): @@ -368,43 +400,6 @@ def club_ban(v, username): return {"message": f"@{username} has been kicked from the {CC_TITLE}. Deserved."} -@app.post("/admin/monthly") -@limiter.limit("1/day") -@admin_level_required(3) -def monthly(v): - if SITE_NAME == 'rDrama' and v.id != AEVANN_ID: abort (403) - - 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.id in (DAD_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"@{v.username} 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 - - if request.host == 'pcmemes.net': - u = g.db.query(User).filter_by(id=KIPPY_ID).one() - u.procoins += 50000 - g.db.add(u) - - ma = ModAction( - kind="monthly", - user_id=v.id - ) - g.db.add(ma) - - g.db.commit() - - return {"message": "Monthly marseybux granted"} - - @app.get("/admin/shadowbanned") @auth_required def shadowbanned(v): diff --git a/files/templates/admin/awards.html b/files/templates/admin/awards.html index 114e46b94..d62c8d19b 100644 --- a/files/templates/admin/awards.html +++ b/files/templates/admin/awards.html @@ -61,9 +61,4 @@ - -

-	{% if SITE_NAME != 'rDrama' or v.id == AEVANN_ID %}
-		
Grant Monthly Marseybux
- {% endif %} {% endblock %} \ No newline at end of file