diff --git a/Dockerfile b/Dockerfile index ba932d1..69017c6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,5 +14,5 @@ RUN pip install --no-cache-dir praw ENV PYTHONUNBUFFERED=1 -# Run the script -CMD ["python", "Bot/main.py"] +# Always generate config.py from environment before starting +CMD ["/bin/sh", "-c", "python populate_config.py && python Bot/main.py"] diff --git a/populate_config.py b/populate_config.py new file mode 100644 index 0000000..d2f964c --- /dev/null +++ b/populate_config.py @@ -0,0 +1,52 @@ +import os +import json + +CONFIG_KEYS = [ + "client_id", + "client_secret", + "user_agent", + "username", + "password", + "sub_name", + "max_days", + "max_posts", + "sleep_minutes", +] + +DEFAULTS = { + "client_id": "", + "client_secret": "", + "user_agent": "", + "username": "", + "password": "", + "sub_name": "", + "max_days": 180, + "max_posts": 180, + "sleep_minutes": 5, +} + +# Try both plain and DP_ prefix for env vars +config = {} +for key in CONFIG_KEYS: + env_val = os.getenv(key.upper()) + if env_val is None: + env_val = os.getenv(f"DP_{key.upper()}") + if env_val is not None: + # Cast numeric values + if key in ["max_days", "max_posts", "sleep_minutes"]: + try: + config[key] = int(env_val) + except ValueError: + config[key] = DEFAULTS[key] + else: + config[key] = env_val + else: + config[key] = DEFAULTS[key] + +# Write config.py +with open("config/config.py", "w", encoding="utf-8") as f: + f.write('"""\nConfiguration for the DeletedPosts bot.\n"""\n\n') + f.write('config = ' + json.dumps(config, indent=4) + '\n') + f.write('\nTEMPLATE = f"""# configuration for DeletedPosts bot\n# edit the values of the dictionary below and restart the bot\n\nconfig = {config!r}\n"""\n') + +print("config/config.py has been populated from environment variables.")