Files

134 lines
3.8 KiB
Python
Raw Permalink Normal View History

2026-02-23 22:48:25 +00:00
# mypy: disable-error-code=attr-defined
import os
import datetime as dt
from bot import Posts
from pathlib import Path
from enum import Enum
from typing import List
from logger import Logger
from sqlitewrapper import Row
__all__ = (
'Flair',
'get_flair',
'modmail_removal_notification',
'parse_cmd_line_args',
'submission_is_older',
'string_to_dt',
)
class Flair(Enum):
SOLVED = 'Solved'
ABANDONED = 'Abandoned'
UKNOWN = 'Uknown'
def get_flair(flair: str) -> Flair:
try:
return Flair(flair)
except ValueError:
return Flair('Uknown')
def modmail_removal_notification(submission: Row, method: str) -> str:
2026-03-04 21:29:58 +00:00
try:
from config import config as bot_config
2026-03-04 21:56:37 +00:00
ban_template = bot_config.get("ban_template", "Your custom ban message is not configured. Please contact the moderators for more information.")
2026-03-04 21:29:58 +00:00
except Exception:
2026-03-04 21:56:37 +00:00
ban_template = "Your custom ban message is not configured. Please contact the moderators for more information."
2026-03-04 21:29:58 +00:00
ban_text = ban_template.format(post_id=submission.post_id)
2026-02-23 22:48:25 +00:00
return f"""A post has been removed
OP: `{submission.username}`
Title: {submission.title}
2026-03-04 21:29:58 +00:00
Post ID: https://sh.reddit.com/comments/{submission.post_id}
2026-02-23 22:48:25 +00:00
Date created: {submission.record_created}
Date found: {submission.record_edited}
Ban Template;
2026-03-04 21:29:58 +00:00
{ban_text}
"""
2026-02-23 22:48:25 +00:00
2026-02-23 23:40:37 +00:00
# default template used when resetting the configuration. this mirrors
# the template defined in ``config/config.py``; keeping a copy here avoids
# depending on the module itself being importable (which can fail if the
# directory is newly mounted or otherwise not part of sys.path).
DEFAULT_TEMPLATE = """# configuration for DeletedPosts bot
# edit the values of the dictionary below and restart the bot
config = {
"client_id": "",
"client_secret": "",
"user_agent": "",
"username": "",
"password": "",
"sub_name": "",
2026-03-04 21:29:58 +00:00
"ban_template": "",
2026-02-23 23:40:37 +00:00
# numeric settings are stored as integers here rather than strings
"max_days": 180,
"max_posts": 180,
"sleep_minutes": 5,
}
"""
2026-02-23 22:48:25 +00:00
def parse_cmd_line_args(args: List[str], logger: Logger, config_file: Path, posts: Posts) -> bool:
2026-02-23 23:31:15 +00:00
"""Parse a very small set of operations from ``sys.argv``.
``config_file`` now refers to the Python configuration module path
(typically ``.../config/config.py``). ``reset_config`` will overwrite the
2026-02-23 23:40:37 +00:00
file with a default template; the template is defined above to avoid
importing the configuration module directly.
2026-02-23 23:31:15 +00:00
"""
2026-02-23 22:48:25 +00:00
help_msg = """Command line help prompt
Command: help
Args: []
2026-02-23 23:31:15 +00:00
Description: Prints the help prompt
2026-02-23 22:48:25 +00:00
Command: reset_config
Args: []
2026-02-23 23:31:15 +00:00
Description: Overwrite the Python configuration file with default values
2026-02-23 22:48:25 +00:00
Command: reset_db
Args: []
2026-02-23 23:31:15 +00:00
Description: Reset the database
2026-02-23 22:48:25 +00:00
"""
if len(args) > 1:
if args[1] == 'help':
logger.info(help_msg)
elif args[1] == 'reset_config':
2026-02-23 23:40:37 +00:00
# write the default template text back to the configuration file.
2026-02-23 22:48:25 +00:00
try:
2026-02-23 23:40:37 +00:00
config_file.write_text(DEFAULT_TEMPLATE)
2026-02-23 23:31:15 +00:00
except Exception:
logger.error("Unable to reset configuration file")
2026-02-23 22:48:25 +00:00
elif args[1] == 'reset_db':
try:
os.remove(posts.path)
except FileNotFoundError:
logger.error("No database found")
else:
logger.info(help_msg)
return True
return False
def submission_is_older(submission_date: dt.date, max_days: int) -> bool:
current_date = dt.datetime.now().date()
time_difference = current_date - submission_date
if time_difference.days > max_days:
return True
return False
def string_to_dt(date_string: str) -> dt.datetime:
return dt.datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f')