2022-06-07 10:57:26 +00:00
from files . cli import g , app , db_session
import click
2022-07-08 16:21:39 +00:00
from files . helpers . const import *
2022-07-29 19:12:56 +00:00
from files . helpers . alerts import send_repeatable_notification
2022-09-13 01:07:39 +00:00
from files . helpers . roulette import spin_roulette_wheel
2022-08-24 20:38:44 +00:00
from files . helpers . get import *
from files . helpers . actions import *
2022-07-21 18:28:52 +00:00
from files . classes import *
2022-09-22 05:08:58 +00:00
from files . __main__ import cache
2022-06-07 14:42:24 +00:00
2022-06-07 11:36:55 +00:00
import files . helpers . lottery as lottery
2022-06-07 14:42:24 +00:00
import files . helpers . offsitementions as offsitementions
2022-06-07 12:31:24 +00:00
import files . helpers . stats as stats
2022-06-10 09:47:41 +00:00
import files . helpers . awards as awards
2022-09-22 05:15:55 +00:00
import files . routes . static as route_static
2022-06-07 10:57:26 +00:00
2022-06-27 19:02:24 +00:00
from sys import stdout
2022-07-08 16:21:39 +00:00
import datetime
2022-07-21 18:11:04 +00:00
import time
2022-07-08 16:21:39 +00:00
import requests
2022-07-21 18:11:04 +00:00
@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. ' )
@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 :
lottery . check_if_end_lottery_task ( )
2022-09-13 01:07:39 +00:00
spin_roulette_wheel ( )
2022-09-13 02:00:02 +00:00
offsitementions . offsite_mentions_task ( )
2022-09-25 04:11:06 +00:00
if SITE == ' pcmemes.net ' :
route_static . live_cached ( )
2022-07-21 18:11:04 +00:00
if every_1h :
awards . award_timers_bots_task ( )
if every_1d :
stats . generate_charts_task ( SITE )
sub_inactive_purge_task ( )
2022-09-22 05:17:16 +00:00
cache . delete_memoized ( route_static . stats_cached )
route_static . stats_cached ( )
2022-07-21 18:11:04 +00:00
if every_1mo :
2022-09-20 02:49:46 +00:00
if KOFI_LINK : give_monthly_marseybux_task_kofi ( )
else : give_monthly_marseybux_task ( )
2022-07-21 18:11:04 +00:00
g . db . commit ( )
g . db . close ( )
stdout . flush ( )
2022-07-08 16:21:39 +00:00
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 ( ) \
2022-09-07 22:29:40 +00:00
. filter ( Submission . sub != None , Submission . created_utc > one_week_ago ,
Submission . private == False , Submission . is_banned == False ,
Submission . deleted_utc == 0 ) . all ( ) ]
2022-07-29 19:12:56 +00:00
active_holes . append ( ' changelog ' ) # system hole immune from deletion
2022-07-08 16:21:39 +00:00
dead_holes = g . db . query ( Sub ) . filter ( Sub . name . notin_ ( active_holes ) ) . all ( )
names = [ x . name for x in dead_holes ]
2022-08-14 06:12:34 +00:00
admins = [ x [ 0 ] for x in g . db . query ( User . id ) . filter ( User . admin_level > 1 ) . all ( ) ]
2022-07-08 16:21:39 +00:00
mods = g . db . query ( Mod ) . filter ( Mod . sub . in_ ( names ) ) . all ( )
for x in mods :
2022-08-14 06:12:34 +00:00
if x . user_id in admins : continue
2022-07-08 16:21:39 +00:00
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: " )
for name in names :
2022-08-24 20:30:31 +00:00
first_mod_id = g . db . query ( Mod . user_id ) . filter_by ( sub = name ) . order_by ( Mod . created_utc ) . first ( )
if first_mod_id :
2022-08-24 20:38:44 +00:00
first_mod = get_account ( first_mod_id [ 0 ] )
2022-08-24 20:30:31 +00:00
badge_grant (
user = first_mod ,
badge_id = 156 ,
description = f ' Brought a Hole into this world, only to let it die (/h/ { name } ) '
)
2022-08-24 20:26:41 +00:00
2022-07-08 16:21:39 +00:00
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 ( ) \
2022-09-07 22:29:40 +00:00
+ g . db . query ( SubJoin ) . filter ( SubJoin . sub . in_ ( names ) ) . all ( ) \
2022-07-08 16:21:39 +00:00
+ g . db . query ( SubSubscription ) . filter ( SubSubscription . sub . in_ ( names ) ) . all ( )
for x in to_delete :
g . db . delete ( x )
2022-08-25 15:04:33 +00:00
g . db . flush ( )
2022-07-08 16:21:39 +00:00
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 }
2022-09-26 04:01:25 +00:00
emails = [ x [ ' email ' ] for x in requests . get ( f ' https://api.gumroad.com/v2/products/ { GUMROAD_ID } /subscribers ' , data = data , timeout = 5 ) . json ( ) [ " subscribers " ] ]
2022-07-08 16:21:39 +00:00
for u in g . db . query ( User ) . filter ( User . patron > 0 , User . patron_utc == 0 ) . all ( ) :
g . db . add ( u )
2022-09-02 00:48:15 +00:00
if u . admin_level or u . id in GUMROAD_MESSY or ( u . email and u . email . lower ( ) in emails ) :
2022-07-08 16:21:39 +00:00
procoins = procoins_li [ u . patron ]
u . procoins + = procoins
2022-07-08 19:03:04 +00:00
send_repeatable_notification ( u . id , f " @AutoJanny has given you { procoins } Marseybux for the month of { month } ! You can use them to buy awards in the [shop](/shop). " )
2022-07-08 16:21:39 +00:00
else : u . patron = 0
ma = ModAction (
kind = " monthly " ,
2022-09-20 02:49:46 +00:00
user_id = AUTOJANNY_ID ,
2022-07-08 16:21:39 +00:00
)
g . db . add ( ma )
return True
2022-09-20 02:49:46 +00:00
def give_monthly_marseybux_task_kofi ( ) :
month = datetime . datetime . now ( ) + datetime . timedelta ( days = 5 )
month = month . strftime ( ' % B ' )
2022-09-20 02:55:38 +00:00
tx_emails = [ x [ 0 ] for x in g . db . query ( Transaction . email ) . distinct ( ) . all ( ) ]
2022-09-20 02:49:46 +00:00
for u in g . db . query ( User ) . filter ( User . patron > 0 , User . patron_utc == 0 ) . all ( ) :
2022-09-20 02:55:38 +00:00
g . db . add ( u )
if not ( u . is_activated and u . email in tx_emails ) :
u . patron = 0
continue
2022-09-20 02:49:46 +00:00
procoins = procoins_li [ u . patron ]
u . procoins + = procoins
send_repeatable_notification ( u . id , f " @AutoJanny has given you { procoins } Marseybux for the month of { month } ! You can use them to buy awards in the [shop](/shop). " )
ma = ModAction (
kind = " monthly " ,
user_id = AUTOJANNY_ID ,
)
g . db . add ( ma )
2022-09-29 05:43:29 +00:00
return True