almost there

This commit is contained in:
Bhupesh-V 2023-05-20 14:05:12 +05:30
parent 9d9db1de7f
commit 589a47a315
3 changed files with 91 additions and 31 deletions

View File

@ -0,0 +1,3 @@
## Learnings
- Make sure the automod file doesn't end with `---`.

View File

@ -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"
}
]
}

View File

@ -1,7 +1,12 @@
import praw import praw
import os import os
import json
import re import re
import ruamel.yaml import ruamel.yaml
from io import StringIO
yaml = ruamel.yaml.YAML()
yaml.default_flow_style = False
sub_name = os.environ["SUBREDDIT_NAME"] sub_name = os.environ["SUBREDDIT_NAME"]
client_id = os.environ["REDDIT_CLIENT_ID"] client_id = os.environ["REDDIT_CLIENT_ID"]
@ -19,43 +24,86 @@ reddit = praw.Reddit(client_id=client_id,
# Get the subreddit object # Get the subreddit object
subreddit = reddit.subreddit(sub_name) subreddit = reddit.subreddit(sub_name)
for wikipage in subreddit.wiki: def find_automod_wiki():
if wikipage == f"{sub_name}/config/automoderator": for wikipage in subreddit.wiki:
content = subreddit.wiki["config/automoderator"] if wikipage == f"{sub_name}/config/automoderator":
break content = subreddit.wiki["config/automoderator"]
break
if content is None: if content is None:
print("AutoModerator configuration page not found in the subreddit's wiki") print("AutoModerator configuration page not found in the subreddit's wiki")
exit(1) exit(1)
# Read the AutoModerator configuration return content
automod_config = content.content_md
config_text = content.content_md # Parse the AutoModerator configuration file
yaml_sections = re.split(r'(?m)^---\n', config_text)[1:] 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 # Parse each YAML section to get the rules
for yaml_text in yaml_sections: for yaml_text in yaml_sections:
rule = {} rule = {}
comment_pattern = r"^\s*#\s*(.*)$" comment_pattern = r"^\s*#\s*(.*)$"
comments = [ comments = [
match.group(1) match.group(1)
for match in re.finditer(comment_pattern, yaml_text, re.MULTILINE) for match in re.finditer(comment_pattern, yaml_text, re.MULTILINE)
] ]
# Load the YAML data using ruamel.yaml # Load the YAML data using ruamel.yaml
yaml_data = ruamel.yaml.safe_load(yaml_text) yaml_data = yaml.load(yaml_text)
if len(comments) > 0: if len(comments) > 0:
rule[comments[0]] = yaml_data rule[comments[0]] = yaml_data
rules.append(rule) rules.append(rule)
for rule in rules: return rules
for key, value in rule.items():
if key == "New post comment": def formatted_announcement(announcements):
rule[key]["comment"] = "This is a test comment" announcement_block = ""
new_content = ruamel.yaml.dump(rule[key], Dumper=ruamel.yaml.RoundTripDumper) announcement_block += f"""{announcements["filler_text"]}\n\n"""
print(new_content) 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 # 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)