🚧 new script to update automod

This commit is contained in:
Bhupesh-V 2023-04-23 23:44:21 +05:30
parent e269052344
commit d76225cf32
4 changed files with 62 additions and 1 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"python.formatting.provider": "black"
}

View File

56
automod_updater/main.py Normal file
View File

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

View File

@ -1,2 +1,4 @@
praw
feedparser
feedparser
pyyaml
ruamel.yaml