Final changes before merge
This commit is contained in:
22
config/flairconfig.py
Normal file
22
config/flairconfig.py
Normal file
@@ -0,0 +1,22 @@
|
||||
# flairconfig.py
|
||||
# This file defines the list of flair time configs for the bot.
|
||||
# Edit this file to customize flair behaviors.
|
||||
|
||||
flair_times = [
|
||||
{
|
||||
"flair_text": "Waiting for OP",
|
||||
"hours": 48,
|
||||
"comment_message": "This post has had the 'Waiting for OP' flair for 48 hours.",
|
||||
"lock_post": False,
|
||||
"distinguish_sticky": False
|
||||
},
|
||||
|
||||
{
|
||||
"flair_text": "Solved",
|
||||
"hours": 0.01,
|
||||
"comment_message": "This post has had the 'Waiting for OP' flair for 48 hours.",
|
||||
"lock_post": False,
|
||||
"distinguish_sticky": False
|
||||
},
|
||||
# Add more configs as needed
|
||||
]
|
||||
@@ -7,26 +7,16 @@ import time
|
||||
|
||||
|
||||
# Helper to get env var or default
|
||||
|
||||
def env_or_default(var, default):
|
||||
return os.environ.get(var, default)
|
||||
|
||||
# Helper to get flair_times from env
|
||||
def get_flair_times_from_env():
|
||||
flair_times_json = os.environ.get("FLAIR_TIMES_JSON", "")
|
||||
if flair_times_json:
|
||||
try:
|
||||
return json.loads(flair_times_json)
|
||||
except Exception as e:
|
||||
print(f"Could not parse FLAIR_TIMES_JSON: {e}")
|
||||
return None
|
||||
|
||||
# Create config/config.py from environment if missing or empty
|
||||
default_config_path = os.path.join('config', 'config.py')
|
||||
|
||||
|
||||
def write_config_from_env():
|
||||
os.makedirs('config', exist_ok=True)
|
||||
flair_times = get_flair_times_from_env()
|
||||
with open(default_config_path, 'w') as f:
|
||||
f.write(
|
||||
f'username = "{env_or_default("USERNAME", "")}"\n'
|
||||
@@ -39,22 +29,9 @@ def write_config_from_env():
|
||||
f'interval = {env_or_default("INTERVAL", "30")}\n'
|
||||
f'searchlimit = {env_or_default("SEARCHLIMIT", "600")}\n'
|
||||
)
|
||||
# Write flair_times as a Python list
|
||||
if flair_times:
|
||||
f.write(f'flair_times = {json.dumps(flair_times)}\n')
|
||||
else:
|
||||
# Fallback to single flair config from env vars
|
||||
f.write('flair_times = [\n')
|
||||
f.write(' {\n')
|
||||
f.write(f' "flair_text": "{env_or_default("FLAIR_TEXT", "Waiting for OP")}",\n')
|
||||
f.write(f' "hours": {env_or_default("HOURS", "48")},\n')
|
||||
f.write(f' "comment_message": "{env_or_default("COMMENT_MESSAGE", "")}",\n')
|
||||
f.write(f' "lock_post": {env_or_default("LOCK_POST", "False")},\n')
|
||||
f.write(f' "distinguish_sticky": {env_or_default("DISTINGUISH_STICKY", "False")}\n')
|
||||
f.write(' }\n')
|
||||
f.write(']\n')
|
||||
print(f"Configuration file auto-populated from environment variables at {default_config_path}.")
|
||||
|
||||
|
||||
# Check if config file exists and is non-empty, else generate from env
|
||||
def config_needs_populating():
|
||||
if not os.path.exists(default_config_path):
|
||||
@@ -66,18 +43,57 @@ def config_needs_populating():
|
||||
except Exception:
|
||||
return True
|
||||
|
||||
|
||||
if config_needs_populating():
|
||||
write_config_from_env()
|
||||
# Optionally exit after populating, or continue to run
|
||||
|
||||
|
||||
# Import main config
|
||||
import config
|
||||
|
||||
|
||||
# Create default flairconfig.py if missing
|
||||
flair_config_path = os.path.join('config', 'flairconfig.py')
|
||||
|
||||
|
||||
def write_default_flairconfig():
|
||||
if not os.path.exists(flair_config_path):
|
||||
os.makedirs(os.path.dirname(flair_config_path), exist_ok=True)
|
||||
with open(flair_config_path, 'w') as f:
|
||||
f.write('# flairconfig.py\n')
|
||||
f.write('# This file defines the list of flair time configs for the bot.\n')
|
||||
f.write('flair_times = [\n')
|
||||
f.write(' {\n')
|
||||
f.write(' "flair_text": "Waiting for OP",\n')
|
||||
f.write(' "hours": 48,\n')
|
||||
f.write(' "comment_message": "This post has had the \'Waiting for OP\' flair for 48 hours.",\n')
|
||||
f.write(' "lock_post": False,\n')
|
||||
f.write(' "distinguish_sticky": False\n')
|
||||
f.write(' },\n')
|
||||
f.write(']\n')
|
||||
print(f"Default flairconfig.py created at {flair_config_path}.")
|
||||
|
||||
|
||||
write_default_flairconfig()
|
||||
|
||||
|
||||
# Load flair_times from flairconfig.py
|
||||
import importlib.util
|
||||
spec = importlib.util.spec_from_file_location("flairconfig", flair_config_path)
|
||||
flairconfig = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(flairconfig)
|
||||
flair_times = getattr(flairconfig, "flair_times", [])
|
||||
|
||||
|
||||
def authentication():
|
||||
print("Authenticating...")
|
||||
reddit = praw.Reddit(username = config.username,
|
||||
reddit = praw.Reddit(
|
||||
username=config.username,
|
||||
password=config.password,
|
||||
client_id=config.client_id,
|
||||
client_secret=config.client_secret,
|
||||
user_agent = config.user_agent)
|
||||
user_agent=config.user_agent
|
||||
)
|
||||
print("Authenticated as {}.".format(reddit.user.me()))
|
||||
return reddit
|
||||
|
||||
@@ -85,7 +101,7 @@ def authentication():
|
||||
def main(reddit, all_posts: dict):
|
||||
# all_posts structure: {flair_text: {submission_id: timestamp}}
|
||||
while True:
|
||||
for flair_cfg in config.flair_times:
|
||||
for flair_cfg in flair_times:
|
||||
flair_text = flair_cfg["flair_text"]
|
||||
hours = flair_cfg["hours"]
|
||||
comment_message = flair_cfg["comment_message"]
|
||||
@@ -151,7 +167,6 @@ def save_posts(data):
|
||||
json.dump(data, file)
|
||||
|
||||
|
||||
|
||||
while True:
|
||||
try:
|
||||
posts = load_posts()
|
||||
|
||||
Reference in New Issue
Block a user