Skip to content

Commit

Permalink
Merge pull request #20 from srz2/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
srz2 authored Dec 12, 2020
2 parents bf374f5 + 1f1f4c1 commit 95d18f2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
5 changes: 4 additions & 1 deletion create-init.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
config.read(file_example_ini)

def get_env(key):
return os.environ.get(key)
value = os.environ.get(key)
if value == None:
value = '[' + key.replace('REDDIT_', '') + ']'
return value


config['DEFAULT']['CLIENT_ID'] = get_env('REDDIT_CLIENT_ID')
Expand Down
30 changes: 27 additions & 3 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
ERROR_GENERAL = 1
ERROR_FILE_MISSING = 2
ERROR_LOGIN_FAILED = 3

ERROR_FILE_UPDATE = 4

class ThreadPostChecker(threading.Thread):
''' This thread will check the posts on all queried subreddits '''
Expand All @@ -80,7 +80,11 @@ def run(self):

def create_user_agent():
''' Create the user agent string '''
c = config['DEFAULT']

temp_config = configparser.ConfigParser()
temp_config.read(file_praw_ini)

c = temp_config['DEFAULT']
username = c['username']
version = c['version']
author = c['author']
Expand Down Expand Up @@ -111,14 +115,34 @@ def init_reddit_client():

return reddit

def add_user_agent_to_ini(new_user_agent):
''' Put new user agent text into the ini file '''

temp_config = configparser.ConfigParser()
temp_config.read(file_praw_ini)
temp_config['DEFAULT']['user_agent'] = new_user_agent

with open(file_praw_ini, 'w') as config_writer:
temp_config.write(config_writer)

def init_config_file():
''' Initizalize the config file in the same directory'''

check_file_exists(file_praw_ini, "The config file praw.ini is missing")

# Dynamically create user agent and modify to current INI file
app_user_agent = create_user_agent()
add_user_agent_to_ini(app_user_agent)

global config
config = configparser.ConfigParser()
config.read('praw.ini')
config.read(file_praw_ini)

# Check that defailt user agent is not in INI file, quit if it is
if config['DEFAULT']['user_agent'] == '[USER_AGENT]':
print('Failed to update INI with user_agent')
sys.exit(ERROR_FILE_UPDATE)


def check_file_exists(path, error_msg):
''' Check if file exists, if not then quit application '''
Expand Down
9 changes: 5 additions & 4 deletions src/praw.ini.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[DEFAULT]
client_id=[CLIENT_ID]
client_secret=[CLIENT_SECRET]
user_agent=[USER_AGENT]
client_id=[REDDIT_CLIENT_ID]
client_secret=[REDDIT_CLIENT_SECRET]
user_agent=[REDDIT_USER_AGENT]
username=[REDDIT_USERNAME]
password=[REDDIT_USER_PASSWORD]
version=0.4
version=0.5
author=/u/srz2
repo_url=https://github.com/srz2/BeginnerProjectBot

0 comments on commit 95d18f2

Please sign in to comment.