From 589a47a315c61ad042ba8a3c2abd88af396bfcb8 Mon Sep 17 00:00:00 2001 From: Bhupesh-V Date: Sat, 20 May 2023 14:05:12 +0530 Subject: [PATCH] almost there --- automod_updater/README.md | 3 + automod_updater/announcement.json | 9 +++ automod_updater/main.py | 110 +++++++++++++++++++++--------- 3 files changed, 91 insertions(+), 31 deletions(-) create mode 100644 automod_updater/README.md create mode 100644 automod_updater/announcement.json diff --git a/automod_updater/README.md b/automod_updater/README.md new file mode 100644 index 0000000..a7e0b96 --- /dev/null +++ b/automod_updater/README.md @@ -0,0 +1,3 @@ +## Learnings + +- Make sure the automod file doesn't end with `---`. \ No newline at end of file diff --git a/automod_updater/announcement.json b/automod_updater/announcement.json new file mode 100644 index 0000000..2f839be --- /dev/null +++ b/automod_updater/announcement.json @@ -0,0 +1,9 @@ +{ + "filler_text": ">Namaste!\nThanks for submitting to r/developersIndia. Make sure to follow the subreddit [Code of Conduct](https://developersindia.in/code-of-conduct/) while participating in this thread.", + "announcements": [ + { + "title": "Announcemnt 1", + "url": "http://www.google.com" + } + ] +} \ No newline at end of file diff --git a/automod_updater/main.py b/automod_updater/main.py index b922b85..61e2f60 100644 --- a/automod_updater/main.py +++ b/automod_updater/main.py @@ -1,7 +1,12 @@ import praw import os +import json import re import ruamel.yaml +from io import StringIO + +yaml = ruamel.yaml.YAML() +yaml.default_flow_style = False sub_name = os.environ["SUBREDDIT_NAME"] client_id = os.environ["REDDIT_CLIENT_ID"] @@ -19,43 +24,86 @@ reddit = praw.Reddit(client_id=client_id, # 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 +def find_automod_wiki(): + 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) + 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 + return content -config_text = content.content_md -yaml_sections = re.split(r'(?m)^---\n', config_text)[1:] +# Parse the AutoModerator configuration file +def get_automod_rules(content): + # Split the content into sections + config_text = content.content_md + yaml_sections = re.split(r'(?m)^---\n', config_text)[1:] -rules = [] + 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) + # 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 = yaml.load(yaml_text) - if len(comments) > 0: - rule[comments[0]] = yaml_data - rules.append(rule) + if len(comments) > 0: + rule[comments[0]] = yaml_data + rules.append(rule) -for rule in rules: - for key, value in rule.items(): - if key == "New post comment": - rule[key]["comment"] = "This is a test comment" - new_content = ruamel.yaml.dump(rule[key], Dumper=ruamel.yaml.RoundTripDumper) - print(new_content) + return rules + +def formatted_announcement(announcements): + announcement_block = "" + announcement_block += f"""{announcements["filler_text"]}\n\n""" + announcement_block += "## Recent Announcements\n\n" + for announcement in announcements["announcements"]: + announcement_block += f"- **[{announcement['title']}]({announcement['url']})**\n" + + return announcement_block + +def update_automod_rule(rules, announcements): + for rule in rules: + for rulename, config in rule.items(): + if rulename == "New post comment": + # print(config) + config["comment"] = announcements + # new_content = yaml.dump(config) + stream = StringIO() + yaml.dump(rules, stream) + yaml_text = stream.getvalue() + stream.close() + print(yaml_text) # TODO: Update the wiki page with the new content + # subreddit.wiki["config/automoderator"].edit(yaml_text, reason="Update automod comment") + +def get_comment(): + with open('new_post_automod.yaml', 'r') as file: + yaml_content = file.read() + + # Load the YAML content into a Python object + yaml_object = yaml.load(yaml_content, ruamel.yaml.RoundTripLoader) + return yaml_object + +# read the announcements from announcements.json +def read_announcements(): + with open('announcement.json', 'r') as file: + announcements = json.load(file) + return announcements + + +content = find_automod_wiki() +announcement = read_announcements() +form_anno = formatted_announcement(announcement) + +rules = get_automod_rules(content) +update_automod_rule(rules, form_anno) \ No newline at end of file