# by Koma52 import os import praw import time from getpass import getpass from os.path import exists import sys import datetime from apscheduler.schedulers.background import BackgroundScheduler import logging from prawcore import NotFound from dotenv import load_dotenv def sub_exists(sub): exists = True try: reddit.subreddits.search_by_name(sub, exact=True) except NotFound: exists = False return exists # def test(text): # print(f'It is working {text}. Please press CTRL+C to close the script!') def e(): title = input('What will be the title of your post? ') out_file = open("post.txt", 'w') print('Welcome to the editor. Here you can edit your post. Just start writing and don\'t use linebreaks because RETURN key will save your text.') post = input() out_file.write(post) out_file.close() return title def s(): subreddit = input('Please type in the subbreddits\' name without \'r/\': ') while not sub_exists(subreddit): subreddit = input('This subreddit doesn\'t exists. Please try another one: ') return subreddit def t(): post_date = input('Please type in the date you want to post in the format: yyyy MM dd hh mm: ') post_datetime = datetime.datetime(int(post_date.split()[0]) , int(post_date.split()[1]) , int(post_date.split()[2]) , int(post_date.split()[3]) , int(post_date.split()[4])) return post_datetime def l(): if exists('post.txt'): in_file = open('post.txt') print(in_file.read()) else: print('First you have to edit your post!') input('Press a button to return to the main menu!') pass def post(title, body, subreddit): try: reddit.subreddit(subreddit).submit(title, selftext=body) print('The scheduled post has been posted!') print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) 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, user_secret, secret2): return praw.Reddit(client_id=user_secret , client_secret=secret2 , password=password , user_agent='PrawTut' , username=user) def cred_input(): # username input user = input('Username: ') # password input password = getpass('Password: ') creds = [] creds.append(user) creds.append(password) return creds print('''This is the Reddit scheduler by Koma52 Please type in your login credentials''') credentials = cred_input() # validating credentials while len(credentials[0]) == 0 and len(credentials[1]) == 0: print('The credetials you gave are not valid. Please try again!') credentials = cred_input() # making reddit instance 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()}') except: print(''' There was an exception. Maybe you misstyped your credentials or you are using 2FA. In that case please turn off 2FA because it\'s not compatible with this script! ''') exit() reddit.validate_on_submit = True ready = '0' checker = ['0', '0', '0'] # for checking if all neccesary functions have been run while ready != '1': print(''' Welcome to the main menu. Here are your choices: e -> edit the post you want to schedule- l -> take a look at your scheduled post s -> edit the subreddit you want to post to- t -> edit the time you want to post r -> your are ready to schedule q -> quit the script- ''') choice = input('> ') if choice == 'q': exit() elif choice == 't': date_to_post = t() print(date_to_post) checker[0] = '1' elif choice == 'e': title = e() checker[1] = '1' elif choice == 's': subreddit = s() checker[2] = '1' elif choice == 'r': if checker != ['1', '1', '1']: print('First you have to edit your post, give the subreddit and schedule your post') else: ready = '1' elif choice == 'l': l() print('Please don\'t exit the script until the scheduled time because in that case it wouldn\'t work!') try: file = open("post.txt") except: print('Something went wrong while reading your post data. Please try again!') exit() body = file.read() 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: # 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 sched.shutdown()