From 2b5d100b7e91cb47b5f676b4a91d16eb3ea66d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=B3nya=20M=C3=A1rton?= Date: Mon, 20 Jul 2020 10:29:10 +0200 Subject: [PATCH] dotenv does not working --- run.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/run.py b/run.py index e15137f..430183c 100644 --- a/run.py +++ b/run.py @@ -1,12 +1,13 @@ # by Koma52 +import os import praw import time from getpass import getpass from os.path import exists import sys import datetime -from apscheduler.schedulers.blocking import BlockingScheduler +from apscheduler.schedulers.background import BackgroundScheduler import logging from prawcore import NotFound from dotenv import load_dotenv @@ -58,16 +59,15 @@ def l(): def post(title, body, subreddit): try: reddit.subreddit(subreddit).submit(title, selftext=body) - print(f'Please press CTRL+C to close the script!') pass except: print(f'Something went wrong. Maybe you don\'t have permission to post in r/{subreddit}. Please press CTRL+C to close the script!') pass # sys.exit(f'Succesfully posted your text to r/{subreddit}. Thank you for using my script!') -def authenticate(password, user): - return praw.Reddit(client_id='BeyDq1uf14_vfQ' - , client_secret='ogpfdmBvy4T67UYmE6w-2PeIhnU' +def authenticate(password, user, user_secret, secret2): + return praw.Reddit(client_id=user_secret + , client_secret=secret2 , password=password , user_agent='PrawTut' , username=user) @@ -97,7 +97,10 @@ while len(credentials[0]) == 0 and len(credentials[1]) == 0: credentials = cred_input() # making reddit instance -reddit = authenticate(credentials[1], credentials[0]) +load_dotenv('creds.env') +USER_SECRET = str(os.getenv('USERSECRET')) +SECRET = str(os.getenv('SECRET2')) +reddit = authenticate(credentials[1], credentials[0], USER_SECRET, SECRET) try: print(f'You\'re logged in as {reddit.user.me()}') @@ -109,9 +112,6 @@ In that case please turn off 2FA because it\'s not compatible with this script! exit() reddit.validate_on_submit = True -load_dotenv('creds.env') -USER_SECRET = os.getenv('USERSECRET') -SECRET = os.getenv('SECRET2') ready = '0' checker = ['0', '0', '0'] # for checking if all neccesary functions have been run @@ -157,13 +157,19 @@ except: body = file.read() -sched = BlockingScheduler() +sched = BackgroundScheduler() sched.add_job(post, 'date', run_date=date_to_post, args=[title, body, subreddit]) +sched.start() +print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) + try: - sched.start() -except (KeyboardInterrupt): - logging.debug('The script has been closed. Thank you for using!') + # This is here to simulate application activity (which keeps the main thread alive). + while True: + time.sleep(2) +except (KeyboardInterrupt, SystemExit): + # Not strictly necessary if daemonic mode is enabled but should be done if possible + scheduler.shutdown() # schedule.every().monday.at("17:12").do(uzenet)