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 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)