config via docker environment variables
This commit is contained in:
@@ -3,6 +3,8 @@ services:
|
|||||||
image: ghcr.io/slfhstd/flairtimercomment:v0.0.1
|
image: ghcr.io/slfhstd/flairtimercomment:v0.0.1
|
||||||
container_name: bot-ftc-dev
|
container_name: bot-ftc-dev
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
volumes:
|
volumes:
|
||||||
- /docker/data/flairtimercomment:/app/config
|
- /docker/data/flairtimercomment:/app/config
|
||||||
|
|
||||||
|
|||||||
17
example.env
Normal file
17
example.env
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# Example .env file for FlairTimerComment
|
||||||
|
# Fill in your Reddit API and bot settings
|
||||||
|
|
||||||
|
USERNAME=your_reddit_username
|
||||||
|
PASSWORD=your_reddit_password
|
||||||
|
CLIENT_ID=your_client_id
|
||||||
|
CLIENT_SECRET=your_client_secret
|
||||||
|
USER_AGENT=Flair Timer Comment Bot
|
||||||
|
|
||||||
|
SUBREDDIT=your_subreddit
|
||||||
|
FLAIR_TEXT=Waiting for OP
|
||||||
|
INTERVAL=30
|
||||||
|
HOURS=48
|
||||||
|
SEARCHLIMIT=600
|
||||||
|
COMMENT_MESSAGE=Your comment message here
|
||||||
|
LOCK_POST=False
|
||||||
|
DISTINGUISH_STICKY=False
|
||||||
@@ -1,33 +1,52 @@
|
|||||||
import praw
|
|
||||||
import praw
|
import praw
|
||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
|
||||||
# Create default config/config.py if it doesn't exist
|
|
||||||
|
# Helper to get env var or default
|
||||||
|
def env_or_default(var, default):
|
||||||
|
return os.environ.get(var, default)
|
||||||
|
|
||||||
|
# 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')
|
||||||
if not os.path.exists(default_config_path):
|
def write_config_from_env():
|
||||||
os.makedirs('config', exist_ok=True)
|
os.makedirs('config', exist_ok=True)
|
||||||
with open(default_config_path, 'w') as f:
|
with open(default_config_path, 'w') as f:
|
||||||
f.write(
|
f.write(
|
||||||
'# Reddit API credentials\n'
|
f'username = "{env_or_default("USERNAME", "")}"\n'
|
||||||
'username = ""\n'
|
f'password = "{env_or_default("PASSWORD", "")}"\n'
|
||||||
'password = ""\n'
|
f'client_id = "{env_or_default("CLIENT_ID", "")}"\n'
|
||||||
'client_id = ""\n'
|
f'client_secret = "{env_or_default("CLIENT_SECRET", "")}"\n'
|
||||||
'client_secret = ""\n'
|
f'user_agent = "{env_or_default("USER_AGENT", "Flair Timer Comment Bot")}"\n'
|
||||||
'user_agent = "Flair Timer Comment Bot" # Must be unique and descriptive\n'
|
|
||||||
'\n'
|
'\n'
|
||||||
'# Subreddits\n'
|
f'subreddit = "{env_or_default("SUBREDDIT", "")}"\n'
|
||||||
'subreddit = ""\n'
|
f'flair_text = "{env_or_default("FLAIR_TEXT", "Waiting for OP")}"\n'
|
||||||
'flair_text = "Waiting for OP"\n'
|
f'interval = {env_or_default("INTERVAL", "30")}\n'
|
||||||
'interval = 30\n'
|
f'hours = {env_or_default("HOURS", "48")}\n'
|
||||||
'hours = 48\n'
|
f'searchlimit = {env_or_default("SEARCHLIMIT", "600")}\n'
|
||||||
'searchlimit = 600\n'
|
f'comment_message = "{env_or_default("COMMENT_MESSAGE", "")}"\n'
|
||||||
'comment_message = ""\n'
|
f'lock_post = {env_or_default("LOCK_POST", "False")}\n'
|
||||||
'lock_post = False\n'
|
f'distinguish_sticky = {env_or_default("DISTINGUISH_STICKY", "False")}\n'
|
||||||
'distinguish_sticky = False\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):
|
||||||
|
return True
|
||||||
|
try:
|
||||||
|
with open(default_config_path, 'r') as f:
|
||||||
|
content = f.read().strip()
|
||||||
|
return len(content) == 0
|
||||||
|
except Exception:
|
||||||
|
return True
|
||||||
|
|
||||||
|
if config_needs_populating():
|
||||||
|
write_config_from_env()
|
||||||
|
# Optionally exit after populating, or continue to run
|
||||||
|
|
||||||
import config
|
import config
|
||||||
def authentication():
|
def authentication():
|
||||||
|
|||||||
Reference in New Issue
Block a user