Changes to enable always on and trigger emthod. also updated to yaml config structure
This commit is contained in:
@@ -1,14 +1,81 @@
|
||||
# config.py
|
||||
# Fetches config from subreddit wiki page
|
||||
import json
|
||||
# Fetches config from subreddit wiki page in YAML format
|
||||
import yaml
|
||||
|
||||
def fetch_config_from_wiki(reddit, subreddit_name, wiki_page):
|
||||
"""
|
||||
Fetches config from the wiki page as YAML.
|
||||
Returns the parsed config dict, or empty dict if error.
|
||||
"""
|
||||
subreddit = reddit.subreddit(subreddit_name)
|
||||
try:
|
||||
wiki = subreddit.wiki[wiki_page]
|
||||
config_text = wiki.content_md
|
||||
config = json.loads(config_text)
|
||||
config = yaml.safe_load(config_text)
|
||||
|
||||
# If config is None or empty, return empty dict
|
||||
if not config:
|
||||
config = {'posts': []}
|
||||
|
||||
return config
|
||||
except yaml.YAMLError as e:
|
||||
print(f"Error parsing YAML config from wiki: {e}")
|
||||
return {'posts': []}
|
||||
except Exception as e:
|
||||
print(f"Error fetching config from wiki: {e}")
|
||||
return {'posts': []}
|
||||
|
||||
|
||||
def validate_config_from_wiki(reddit, subreddit_name, wiki_page):
|
||||
"""
|
||||
Validates the config from the wiki page.
|
||||
Returns True if the config is valid YAML with 'posts' key, False otherwise.
|
||||
"""
|
||||
subreddit = reddit.subreddit(subreddit_name)
|
||||
try:
|
||||
wiki = subreddit.wiki[wiki_page]
|
||||
config_text = wiki.content_md
|
||||
config = yaml.safe_load(config_text)
|
||||
|
||||
# Validate required structure
|
||||
if not isinstance(config, dict) or 'posts' not in config:
|
||||
print("Wiki config missing required 'posts' key or is not a dict.")
|
||||
return False
|
||||
|
||||
return True
|
||||
except yaml.YAMLError as e:
|
||||
print(f"Error parsing YAML config from wiki: {e}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"Error fetching config from wiki: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def get_trigger_posts(reddit, subreddit_name, wiki_page, trigger_name):
|
||||
"""
|
||||
Gets the posts associated with a specific trigger.
|
||||
Returns a list of post dicts, or empty list if trigger not found.
|
||||
"""
|
||||
config = fetch_config_from_wiki(reddit, subreddit_name, wiki_page)
|
||||
|
||||
posts_config = config.get('posts', [])
|
||||
if not isinstance(posts_config, list):
|
||||
print("Config 'posts' is not a list.")
|
||||
return []
|
||||
|
||||
for post_config in posts_config:
|
||||
if not isinstance(post_config, dict):
|
||||
continue
|
||||
|
||||
if post_config.get('trigger', '').lower() == trigger_name.lower():
|
||||
# Get the posts for this trigger
|
||||
trigger_posts = post_config.get('posts', [])
|
||||
if not isinstance(trigger_posts, list):
|
||||
print(f"Trigger '{trigger_name}' posts is not a list.")
|
||||
return []
|
||||
|
||||
return trigger_posts
|
||||
|
||||
# Trigger not found
|
||||
print(f"Trigger '{trigger_name}' not found in config.")
|
||||
return []
|
||||
|
||||
Reference in New Issue
Block a user