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
|
# Helper to get env var or default
|
||||||
|
|
||||||
def env_or_default(var, default):
|
def env_or_default(var, default):
|
||||||
return os.environ.get(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
|
# Create config/config.py from environment if missing or empty
|
||||||
default_config_path = os.path.join('config', 'config.py')
|
default_config_path = os.path.join('config', 'config.py')
|
||||||
|
|
||||||
|
|
||||||
def write_config_from_env():
|
def write_config_from_env():
|
||||||
os.makedirs('config', exist_ok=True)
|
os.makedirs('config', exist_ok=True)
|
||||||
flair_times = get_flair_times_from_env()
|
|
||||||
with open(default_config_path, 'w') as f:
|
with open(default_config_path, 'w') as f:
|
||||||
f.write(
|
f.write(
|
||||||
f'username = "{env_or_default("USERNAME", "")}"\n'
|
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'interval = {env_or_default("INTERVAL", "30")}\n'
|
||||||
f'searchlimit = {env_or_default("SEARCHLIMIT", "600")}\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}.")
|
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
|
# Check if config file exists and is non-empty, else generate from env
|
||||||
def config_needs_populating():
|
def config_needs_populating():
|
||||||
if not os.path.exists(default_config_path):
|
if not os.path.exists(default_config_path):
|
||||||
@@ -65,27 +42,66 @@ def config_needs_populating():
|
|||||||
return len(content) == 0
|
return len(content) == 0
|
||||||
except Exception:
|
except Exception:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
if config_needs_populating():
|
if config_needs_populating():
|
||||||
write_config_from_env()
|
write_config_from_env()
|
||||||
# Optionally exit after populating, or continue to run
|
|
||||||
|
|
||||||
|
|
||||||
|
# Import main config
|
||||||
import 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():
|
def authentication():
|
||||||
print ("Authenticating...")
|
print("Authenticating...")
|
||||||
reddit = praw.Reddit(username = config.username,
|
reddit = praw.Reddit(
|
||||||
password = config.password,
|
username=config.username,
|
||||||
client_id = config.client_id,
|
password=config.password,
|
||||||
client_secret = config.client_secret,
|
client_id=config.client_id,
|
||||||
user_agent = config.user_agent)
|
client_secret=config.client_secret,
|
||||||
print ("Authenticated as {}.".format(reddit.user.me()))
|
user_agent=config.user_agent
|
||||||
|
)
|
||||||
|
print("Authenticated as {}.".format(reddit.user.me()))
|
||||||
return reddit
|
return reddit
|
||||||
|
|
||||||
|
|
||||||
def main(reddit, all_posts: dict):
|
def main(reddit, all_posts: dict):
|
||||||
# all_posts structure: {flair_text: {submission_id: timestamp}}
|
# all_posts structure: {flair_text: {submission_id: timestamp}}
|
||||||
while True:
|
while True:
|
||||||
for flair_cfg in config.flair_times:
|
for flair_cfg in flair_times:
|
||||||
flair_text = flair_cfg["flair_text"]
|
flair_text = flair_cfg["flair_text"]
|
||||||
hours = flair_cfg["hours"]
|
hours = flair_cfg["hours"]
|
||||||
comment_message = flair_cfg["comment_message"]
|
comment_message = flair_cfg["comment_message"]
|
||||||
@@ -132,7 +148,7 @@ def main(reddit, all_posts: dict):
|
|||||||
|
|
||||||
save_posts(all_posts)
|
save_posts(all_posts)
|
||||||
time.sleep(config.interval)
|
time.sleep(config.interval)
|
||||||
|
|
||||||
|
|
||||||
def load_posts():
|
def load_posts():
|
||||||
if not os.path.exists("config/posts.json"):
|
if not os.path.exists("config/posts.json"):
|
||||||
@@ -144,13 +160,12 @@ def load_posts():
|
|||||||
if not isinstance(data, dict):
|
if not isinstance(data, dict):
|
||||||
return {}
|
return {}
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def save_posts(data):
|
def save_posts(data):
|
||||||
with open('config/posts.json', 'w+') as file:
|
with open('config/posts.json', 'w+') as file:
|
||||||
json.dump(data, file)
|
json.dump(data, file)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user