su-automod/functions/scan_posts.py

99 lines
5.5 KiB
Python
Executable File

import functions.get_newest_count
import var
from classes import post
from datetime import datetime, timedelta
from functions.delete_post import delete_post
from functions.send_message import send_message
from functions.suspend_user import suspend_user_for_post
from beloved_logger import logger
import akismet
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()
post_deleted = False
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 and post_to_analyse.poster_sign_up_time:
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
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)
if var.spam_scanning_is_running and post_to_analyse.poster_sign_up_time:
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")
if current_date > cut_off_date:
akismet_client = await akismet.AsyncClient.validated_client()
if await akismet_client.comment_check(
comment_content=post_to_analyse.post_description,
comment_type="comment",
comment_author=post_to_analyse.poster_username,
user_ip="0.0.0.0"
):
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 spam content.
*If this was a mistake don't worry, your post will be subject to human review and your post will be restored as soon possible if it is not spam.*
❓ If there any questions, problems, or concerns please contact @username
"""
spam_string = f"""A post made by @{post_to_analyse.poster_username} was deleted for tripping the spam filter.
"{post_to_analyse.post_name}" at https://{var.site}/admin/posts/{post_to_analyse.post_id}"""
post_to_analyse.post_will_be_deleted = True
await delete_post(post_to_analyse.post_id)
await send_message(spam_string)
await send_message(message_string, post_to_analyse.poster_username)
var.current_posts_at_start_time = current_posts_now