From d76225cf323c10e5ec326af3d6c65cbfc5c28d8f Mon Sep 17 00:00:00 2001 From: Bhupesh-V Date: Sun, 23 Apr 2023 23:44:21 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20new=20script=20to=20update=20aut?= =?UTF-8?q?omod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 ++ automod_updater/__init__.py | 0 automod_updater/main.py | 56 +++++++++++++++++++++++++++++++++++++ requirements.txt | 4 ++- 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json create mode 100644 automod_updater/__init__.py create mode 100644 automod_updater/main.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..de288e1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.formatting.provider": "black" +} \ No newline at end of file diff --git a/automod_updater/__init__.py b/automod_updater/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/automod_updater/main.py b/automod_updater/main.py new file mode 100644 index 0000000..3b5a2e6 --- /dev/null +++ b/automod_updater/main.py @@ -0,0 +1,56 @@ +import praw +import os +import re +import ruamel.yaml + +sub_name = os.environ["SUBREDDIT_NAME"] +client_id = os.environ["REDDIT_CLIENT_ID"] +client_secret = os.environ["REDDIT_CLIENT_SECRET"] +reddit_pass = os.environ["REDDIT_PASSWORD"] +username = os.environ["REDDIT_USERNAME"] + +# Create a Reddit instance +reddit = praw.Reddit(client_id=client_id, + client_secret=client_secret, + user_agent=f"Automod reader by u/{username}", + username=username, + password=reddit_pass) + +# Get the subreddit object +subreddit = reddit.subreddit(sub_name) + +for wikipage in subreddit.wiki: + if wikipage == f"{sub_name}/config/automoderator": + content = subreddit.wiki["config/automoderator"] + break + +if content is None: + print("AutoModerator configuration page not found in the subreddit's wiki") + exit(1) + +# Read the AutoModerator configuration +automod_config = content.content_md + +config_text = content.content_md +yaml_sections = re.split(r'(?m)^---\n', config_text)[1:] + +rules = [] + +# Parse each YAML section to get the rules +for yaml_text in yaml_sections: + rule = {} + comment_pattern = r"^\s*#\s*(.*)$" + comments = [ + match.group(1) + for match in re.finditer(comment_pattern, yaml_text, re.MULTILINE) + ] + # Load the YAML data using ruamel.yaml + yaml_data = ruamel.yaml.safe_load(yaml_text) + + if len(comments) > 0: + rule[comments[0]] = yaml_data + rules.append(rule) + +for rule in rules: + for key, value in rule.items(): + print("rule name: ", key) diff --git a/requirements.txt b/requirements.txt index 102d7f2..af66743 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ praw -feedparser \ No newline at end of file +feedparser +pyyaml +ruamel.yaml \ No newline at end of file