su-automod/functions/scan_posts.py

68 lines
3.5 KiB
Python
Executable File

import functions.get_newest_count
import var
from classes import post
from datetime import datetime, timedelta
from functions.send_message import send_message
from functions.suspend_user import suspend_user_for_post
from beloved_logger import logger
async def scan_posts():
logger.debug("Start post scan")
if var.most_recently_deleted_post_id > var.current_posts_at_start_time:
var.current_posts_at_start_time = var.most_recently_deleted_post_id
current_posts_now = var.current_posts_at_start_time
new_posts = await functions.get_newest_count.get_newest_count()
if new_posts != 0:
current_posts_now = new_posts
logger.debug(f"posts now: {current_posts_now}")
logger.debug(f"posts previous: {var.current_posts_at_start_time}")
if current_posts_now != var.current_posts_at_start_time:
for i in range(int(var.current_posts_at_start_time) + 1, int(current_posts_now) + 1):
post_to_analyse = post.Post(i)
await post_to_analyse.get_values()
if post_to_analyse.response:
if var.text_scanning_is_running:
await post_to_analyse.analyse_text()
if len(post_to_analyse.infringing_words) > 0:
await send_message(await post_to_analyse.get_as_text_detection_text())
if var.image_scanning_is_running:
if var.scan_new_users_only:
cut_off_date = datetime.now() - timedelta(days=3)
current_date = datetime.strptime(post_to_analyse.poster_sign_up_time, "%Y-%m-%d %H:%M:%S")
logger.debug(f"User signed up at {current_date}, we are checking for "
f"users that signed up after {cut_off_date}")
if current_date < cut_off_date:
logger.info(f"User was signed up before {cut_off_date}, so checks won't occur")
var.most_recently_deleted_post_id = i
return
else:
logger.info(f"User was signed up after {cut_off_date}, so checks will occur")
logger.info(f"Analysing {i} for image")
await post_to_analyse.analyse_image()
if post_to_analyse.ratings:
if post_to_analyse.deletion_status:
message_string = f"""👋 Hello, I am an automatic moderation bot for Sketchers United.
Your most recent post titled '{post_to_analyse.post_name}' was removed automatically as it was detected to have inappropriate content.
As a result, your account was suspended for one day.
*If this was a mistake don't worry, all deletions and suspensions are subject to human review and corrections to automatic moderation actions (removal of suspensions and restoration of deleted posts) will be made as soon as possible if they are not warranted.*
❓ If there any questions, problems, or concerns please contact @username
"""
await suspend_user_for_post(post_to_analyse)
await send_message(await post_to_analyse.get_as_image_deletion_text())
await send_message(message_string, post_to_analyse.poster_username)
var.current_posts_at_start_time = current_posts_now