2022-11-15 09:19:08 +00:00
import datetime
import time
2023-02-25 19:18:40 +00:00
import os
2022-11-15 09:19:08 +00:00
from sys import stdout
2023-02-24 23:14:18 +00:00
from shutil import make_archive
from hashlib import md5
2023-02-25 19:18:40 +00:00
2022-06-07 10:57:26 +00:00
import click
2022-11-15 09:19:08 +00:00
import requests
2022-06-07 14:42:24 +00:00
2022-11-15 09:19:08 +00:00
import files . helpers . awards as awards
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-09-22 05:15:55 +00:00
import files . routes . static as route_static
2022-11-15 09:19:08 +00:00
from files . __main__ import cache
from files . classes import *
from files . helpers . alerts import send_repeatable_notification
2022-12-11 23:44:34 +00:00
from files . helpers . config . const import *
2022-11-15 09:19:08 +00:00
from files . helpers . get import *
from files . helpers . lottery import check_if_end_lottery_task
from files . helpers . roulette import spin_roulette_wheel
from files . helpers . useractions import *
from files . cli import app , db_session , g
2022-07-08 16:21:39 +00:00
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 ( )
2023-03-11 07:36:41 +00:00
g . v = None
2022-07-21 18:11:04 +00:00
if every_5m :
2022-10-08 00:31:46 +00:00
if FEATURES [ ' GAMBLING ' ] :
2022-11-15 09:19:08 +00:00
check_if_end_lottery_task ( )
2022-10-08 00:31:46 +00:00
spin_roulette_wheel ( )
2022-11-13 11:00:05 +00:00
offsitementions . offsite_mentions_task ( cache )
2022-07-21 18:11:04 +00:00
if every_1h :
awards . award_timers_bots_task ( )
2023-03-02 01:54:06 +00:00
_generate_emojis_zip ( )
2022-07-21 18:11:04 +00:00
if every_1d :
stats . generate_charts_task ( SITE )
2022-11-15 09:19:08 +00:00
_sub_inactive_purge_task ( )
2022-12-20 02:30:42 +00:00
site_stats = stats . stats ( SITE_NAME )
2022-12-20 23:44:53 +00:00
cache . set ( f ' { SITE } _stats ' , site_stats )
2022-07-21 18:11:04 +00:00
g . db . commit ( )
g . db . close ( )
2022-11-15 15:39:00 +00:00
del g . db
2022-07-21 18:11:04 +00:00
stdout . flush ( )
2022-07-08 16:21:39 +00:00
2022-11-15 09:19:08 +00:00
def _sub_inactive_purge_task ( ) :
2022-07-08 16:21:39 +00:00
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 ( ) ]
2023-02-06 04:30:21 +00:00
active_holes . extend ( [ ' changelog ' , ' countryclub ' , ' masterbaiters ' ] ) # holes 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-10-06 04:40:02 +00:00
admins = [ x [ 0 ] for x in g . db . query ( User . id ) . filter ( User . admin_level > = PERMS [ ' NOTIFICATIONS_HOLE_INACTIVITY_DELETION ' ] ) . all ( ) ]
2022-08-14 06:12:34 +00:00
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 ,
2022-12-24 18:55:26 +00:00
description = f ' Let a hole they owned die (/h/ { name } ) '
2022-08-24 20:30:31 +00:00
)
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 :
2022-12-10 14:00:58 +00:00
if post . sub == ' programming ' :
post . sub = ' slackernews '
else :
post . sub = None
2022-11-18 19:58:12 +00:00
post . hole_pinned = None
2022-07-08 16:21:39 +00:00
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-11-06 05:07:56 +00:00
+ g . db . query ( SubSubscription ) . filter ( SubSubscription . sub . in_ ( names ) ) . all ( ) \
+ g . db . query ( SubAction ) . filter ( SubAction . sub . in_ ( names ) ) . all ( )
2022-07-08 16:21:39 +00:00
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
2023-02-24 23:14:18 +00:00
def _generate_emojis_zip ( ) :
make_archive ( ' files/assets/emojis ' , ' zip ' , ' files/assets/images/emojis ' )
m = md5 ( )
with open ( ' files/assets/emojis.zip ' , " rb " ) as f :
data = f . read ( )
m . update ( data )
cache . set ( ' emojis_hash ' , m . hexdigest ( ) )
2023-02-25 19:18:40 +00:00
count = str ( len ( os . listdir ( ' files/assets/images/emojis ' ) ) )
cache . set ( ' emojis_count ' , count )
size = str ( int ( os . stat ( ' files/assets/emojis.zip ' ) . st_size / 1024 / 1024 ) ) + ' MB '
cache . set ( ' emojis_size ' , size )