2026-02-24 22:28:39 +00:00
import os
import json
CONFIG_KEYS = [
" client_id " ,
" client_secret " ,
" user_agent " ,
" username " ,
" password " ,
" sub_name " ,
" max_days " ,
" max_posts " ,
" sleep_minutes " ,
2026-03-04 22:05:49 +00:00
" ban_template " ,
2026-02-24 22:28:39 +00:00
]
DEFAULTS = {
" client_id " : " " ,
" client_secret " : " " ,
" user_agent " : " " ,
" username " : " " ,
" password " : " " ,
" sub_name " : " " ,
" max_days " : 180 ,
" max_posts " : 180 ,
" sleep_minutes " : 5 ,
2026-03-04 22:05:49 +00:00
" ban_template " : " " ,
2026-02-24 22:28:39 +00:00
}
# 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 ( ' " " " \n Configuration for the DeletedPosts bot. \n " " " \n \n ' )
f . write ( ' config = ' + json . dumps ( config , indent = 4 ) + ' \n ' )
f . write ( ' \n TEMPLATE = f " " " # configuration for DeletedPosts bot \n # edit the values of the dictionary below and restart the bot \n \n config = {config!r} \n " " " \n ' )
print ( " config/config.py has been populated from environment variables. " )