dotenv does not working

This commit is contained in:
Kónya Márton 2020-07-20 10:29:10 +02:00
parent 399f8d8fa1
commit 2b5d100b7e
1 changed files with 19 additions and 13 deletions

32
run.py
View File

@ -1,12 +1,13 @@
# by Koma52 # by Koma52
import os
import praw import praw
import time import time
from getpass import getpass from getpass import getpass
from os.path import exists from os.path import exists
import sys import sys
import datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler from apscheduler.schedulers.background import BackgroundScheduler
import logging import logging
from prawcore import NotFound from prawcore import NotFound
from dotenv import load_dotenv from dotenv import load_dotenv
@ -58,16 +59,15 @@ def l():
def post(title, body, subreddit): def post(title, body, subreddit):
try: try:
reddit.subreddit(subreddit).submit(title, selftext=body) reddit.subreddit(subreddit).submit(title, selftext=body)
print(f'Please press CTRL+C to close the script!')
pass pass
except: 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!') 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 pass
# sys.exit(f'Succesfully posted your text to r/{subreddit}. Thank you for using my script!') # sys.exit(f'Succesfully posted your text to r/{subreddit}. Thank you for using my script!')
def authenticate(password, user): def authenticate(password, user, user_secret, secret2):
return praw.Reddit(client_id='BeyDq1uf14_vfQ' return praw.Reddit(client_id=user_secret
, client_secret='ogpfdmBvy4T67UYmE6w-2PeIhnU' , client_secret=secret2
, password=password , password=password
, user_agent='PrawTut' , user_agent='PrawTut'
, username=user) , username=user)
@ -97,7 +97,10 @@ while len(credentials[0]) == 0 and len(credentials[1]) == 0:
credentials = cred_input() credentials = cred_input()
# making reddit instance # 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: try:
print(f'You\'re logged in as {reddit.user.me()}') 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() exit()
reddit.validate_on_submit = True reddit.validate_on_submit = True
load_dotenv('creds.env')
USER_SECRET = os.getenv('USERSECRET')
SECRET = os.getenv('SECRET2')
ready = '0' ready = '0'
checker = ['0', '0', '0'] # for checking if all neccesary functions have been run checker = ['0', '0', '0'] # for checking if all neccesary functions have been run
@ -157,13 +157,19 @@ except:
body = file.read() body = file.read()
sched = BlockingScheduler() sched = BackgroundScheduler()
sched.add_job(post, 'date', run_date=date_to_post, args=[title, body, subreddit]) 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: try:
sched.start() # This is here to simulate application activity (which keeps the main thread alive).
except (KeyboardInterrupt): while True:
logging.debug('The script has been closed. Thank you for using!') 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) # schedule.every().monday.at("17:12").do(uzenet)