def ingest_log(subreddit, database): subreddit.mod_log = [] for log_item in subreddit.sub_object.mod.log(limit=None): if database.session.query(LogItem).filter_by(id=log_item.id).count() > 0: break subreddit.mod_log.append(log_item) counters.mod_actions.labels(moderator=log_item.mod.name, subreddit=subreddit.name).inc() warning_type = None warning_items = [] if log_item.mod.name not in subreddit.moderators: warning_type = "1" elif subreddit.warning_log_types is not None and log_item.action in subreddit.warning_log_types: sub_filters = subreddit.warning_log_types[log_item.action] for item, value in sub_filters.items(): if item == "print": for field_name in value: warning_items.append(getattr(log_item, field_name)) continue log_item_value = getattr(log_item, item) if value.startswith("!"): if value == "!": if log_item_value is None: warning_type = "5" else: warning_type = None break elif log_item_value != value[1:]: warning_type = "4" else: warning_type = None break elif value.startswith("~"): if value[1:] in log_item_value: warning_type = "6" else: warning_type = None break elif log_item_value == value: warning_type = "3" else: warning_type = None break elif log_item.action not in subreddit.known_log_types: warning_type = "2" if warning_type is not None: log.warning( f"r/{subreddit.name}: {warning_type}:Mod action by u/{log_item.mod.name}: {log_item.action} {' '.join(warning_items)}") database.session.merge(LogItem(log_item)) discord_logging.flush_discord()
def signal_handler(signal, frame): log.info("Handling interrupt") database.close() discord_logging.flush_discord() sys.exit(0)
def main(reddit): # mark r/fakecollegefootball modmails as read for conversation in reddit.subreddit( 'fakecollegefootball').modmail.conversations(limit=10, state='all'): if utils.conversation_is_unread(conversation): log.info( f"Marking r/fakecollegefootball conversation {conversation.id} as read" ) conversation.read() # export inbox size count_messages, count_comments = 0, 0 for inbox_item in reddit.inbox.unread(limit=None): if inbox_item.fullname.startswith("t1_"): count_comments += 1 elif inbox_item.fullname.startswith("t4_"): count_messages += 1 else: log.warning(f"Unexpected item in inbox: {inbox_item.fullname}") counters.inbox_size.labels(type="messages").set(count_messages) counters.inbox_size.labels(type="comments").set(count_comments) # export folder sizes base_folder = "/home/watchful1" for dir_path, dir_names, file_names in os.walk(base_folder): for dir_name in dir_names: if dir_name.startswith("."): continue folder_bytes = utils.get_size(base_folder + "/" + dir_name) counters.folder_size.labels(name=dir_name).set(folder_bytes / 1024 / 1024) break # export hard drive space total, used, free = shutil.disk_usage("/") counters.hard_drive_size.set(round(used / (2**30), 2)) count_objects_to_track = 25 # get comment scores for reddit_comment in reversed( list(reddit.user.me().comments.new(limit=50))): if datetime.utcfromtimestamp( reddit_comment.created_utc) > datetime.utcnow() - timedelta( hours=24 * 2): utils.process_reddit_object(reddit_comment, "comment", database, counters) # get post scores for reddit_submission in reversed( list(reddit.user.me().submissions.new(limit=10))): if datetime.utcfromtimestamp( reddit_submission.created_utc) > datetime.utcnow() - timedelta( hours=24 * 5): utils.process_reddit_object(reddit_submission, "submission", database, counters) # delete old objects utils.delete_old_objects("comment", database, counters, 24 * 2) utils.delete_old_objects("submission", database, counters, 24 * 5) # get karma totals me = reddit.user.me() me._fetch() counters.karma.labels(type="comment").set(me.comment_karma) counters.karma.labels(type="submission").set(me.link_karma) # check accounts for shadowbans for account in account_list: if account['banned'] or account['checked'] is None or account[ 'checked'] < datetime.utcnow() - timedelta(minutes=60): msg = None try: fullname = reddit.redditor(account['username']).fullname if account['banned']: log.warning(f"u/{account['username']} has been unbanned") account['banned'] = False except prawcore.exceptions.NotFound: if not account[ 'banned'] or account['posted'] is None or account[ 'posted'] < datetime.utcnow() - timedelta( hours=24): msg = f"u/{account['username']} has been shadowbanned" except AttributeError: if not account[ 'banned'] or account['posted'] is None or account[ 'posted'] < datetime.utcnow() - timedelta( hours=24): msg = f"u/{account['username']} has been banned" if msg is not None: log.warning(f"u/{account['username']} has been banned") account['banned'] = True account['posted'] = datetime.utcnow() account['checked'] = datetime.utcnow() database.session.commit() discord_logging.flush_discord()
utils.process_error(f"Error sending notifications", err, traceback.format_exc()) errors += 1 if utils.time_offset(last_comments, minutes=30): try: comments.update_comments(reddit, database) last_comments = utils.datetime_now() except Exception as err: utils.process_error(f"Error updating comments", err, traceback.format_exc()) errors += 1 if not args.no_backup and utils.time_offset(last_backup, hours=12): try: database.backup() last_backup = utils.datetime_now() except Exception as err: utils.process_error(f"Error backing up database", err, traceback.format_exc()) errors += 1 log.debug("Run complete after: %d", int(time.perf_counter() - startTime)) discord_logging.flush_discord() if args.once: break sleep_time = max(30 - actions, 0) + (30 * errors) log.debug(f"Sleeping {sleep_time}") time.sleep(sleep_time)
def signal_handler(signal, frame): log.info("Handling interrupt") database.session.commit() database.engine.dispose() discord_logging.flush_discord() sys.exit(0)