├── .gitignore ├── content.txt ├── config.txt ├── post-cron.py └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.TEST 3 | -------------------------------------------------------------------------------- /content.txt: -------------------------------------------------------------------------------- 1 | [Sample Title] 2 | [Sample submission body] -------------------------------------------------------------------------------- /config.txt: -------------------------------------------------------------------------------- 1 | username= 2 | password= 3 | client-id= 4 | client-secret= 5 | subreddit= 6 | -------------------------------------------------------------------------------- /post-cron.py: -------------------------------------------------------------------------------- 1 | import praw 2 | import logging 3 | 4 | logging.basicConfig(filename='reddit.log',level=logging.INFO, 5 | format='%(asctime)s %(levelname)s : %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') 6 | 7 | # open the config file as read-only 8 | config_file = open('config.txt', 'r') 9 | config_settings = config_file.readlines() 10 | config_file.close() 11 | 12 | # make sure all of the configuration fields have been filled 13 | for config in config_settings: 14 | if config[-2:] == '=\n' and config[:10] != 'submission': 15 | raise Exception('Configuration field left blank!') 16 | 17 | # begin parsing the information from the configuration file into usable variables 18 | # note the '.rstrip()' method being called on each string - this removes any newlines 19 | username = config_settings[0][9:].rstrip() 20 | password = config_settings[1][9:].rstrip() 21 | client_id = config_settings[2][10:].rstrip() 22 | client_secret = config_settings[3][14:].rstrip() 23 | subreddit = config_settings[4][10:].rstrip() 24 | 25 | # open the content.txt file as read-only 26 | content_file = open('content.txt', 'r') 27 | 28 | # bring all of the lines from the content file into the content variable 29 | content = content_file.readlines() 30 | 31 | # if configured correctly, the title will be the first line in the program 32 | submission_title = content[0] 33 | 34 | submission_body = '' 35 | 36 | # and the subsequent lines will be the body of the submission 37 | for line in content: 38 | # skip the title line 39 | if line == submission_title: 40 | continue 41 | 42 | submission_body += line.rstrip() + '\n' 43 | 44 | try: 45 | # login to reddit... 46 | reddit = praw.Reddit(client_id=client_id, client_secret=client_secret, password=password, username=username, 47 | user_agent="User") 48 | 49 | # create an instance of the subreddit class and submit the post! 50 | target_subreddit = reddit.subreddit(subreddit) 51 | target_subreddit.submit(title=submission_title, selftext=submission_body, url=None, resubmit=True, 52 | send_replies=True) 53 | 54 | # output to the logfile 55 | logging.info('Successful post to /r/{}'.format(subreddit)) 56 | 57 | except Exception as err: 58 | # if something went wrong with reddit, put the exception in the log file 59 | logging.error(err) -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Reddit Easy Post Bot! 2 | 3 | ### About 4 | There have been a few threads in /r/redditdev regarding an easy way to automatically submit a post 5 | at a given time. This is a simple Python script to cover that need! 6 | 7 | ### Setup 8 | 9 | #### Dependencies 10 | This script uses Python 3.6 and has only one dependency: PRAW. This is the API Wrapper used for interfacing with 11 | the Reddit API. It can be installed with pip: 12 | ```commandline 13 | $ pip3 install praw 14 | ``` 15 | 16 | 17 | More information about installing PRAW can be found here: 18 | http://praw.readthedocs.io/en/latest/getting_started/installation.html 19 | 20 | 21 | #### Configuration 22 | Most of the work is done in the `config.txt` file. 23 | This script uses a configuration file to determine the account credentials. The contents of 24 | the `config.txt` file are as follows: 25 | ``` 26 | username= <-- the username of the account 27 | password= <-- the password for that account 28 | client-id= <-- the "client id" for the Reddit account 29 | client-secret= <-- the "client secret" associated with the Reddit account 30 | subreddit= <-- the subreddit where the post should go 31 | ``` 32 | A quick note here: Fields such as `username` and `subreddit` do not include the common 33 | slashes. For example, the username should be `username` instead of `/u/username`, and the 34 | subreddit should be like `redditdev` rather than `/r/redditdev`. 35 | 36 | 37 | #### Client ID and Client Secret 38 | In order to make your account access the Reddit API, it must be a developer account. 39 | This can be done by going into account preferences, then click apps. This panel should allow you 40 | to create a bot for the account and get the ID and Secret. 41 | A good guide can be found here: 42 | 43 | http://progur.com/2016/09/how-to-create-reddit-bot-using-praw4#registering-the-bot 44 | 45 | #### Submission Content! 46 | This script uses the supplied `content.txt` file for the content of the submission. Edit 47 | this file in your favorite text editor. Remember, Reddit's posts use Markdown for formatting! 48 | Plenty of information about can be found online. 49 | The first line of this file will be the title of the post. Anything after that is the content 50 | of the submission! Here's an example `content.txt` file to help: 51 | ```buildoutcfg 52 | Hello world! 53 | This is the body of your submission. Anything following the 54 | first line of this file will be included in your post. Have fun! 55 | ``` 56 | Here the title will be `Hello world!` and the body will be `This is the body...` and everything 57 | after that. 58 | 59 | ### Cron Installation 60 | This script is intended to run on a Linux (or Mac) based computer using the crontab service. This can easily be accomplished using a Desktop, Raspberry Pi, or a service such as Amazon AWS. 61 | 62 | Open a terminal and clone this repository into your working directory: 63 | 64 | ```commandline 65 | $ git clone https://github.com/rleonard21/reddit-easy-post.git 66 | ``` 67 | 68 | Then edit your crontab by running the following command: 69 | ```commandline 70 | $ crontab -e 71 | ``` 72 | When the crontab file opens in the text editor, enter the following at the bottom: 73 | 74 | ```commandline 75 | min hr day mon dow python3 /full/path/post-cron.py 76 | ``` 77 | 78 | Where `min`, `hr`, `day`, `mon`, and `dow` represent the time at which to run the script. 79 | How to set up the time will not be explained here, as it is covered in 80 | many other resources: 81 | http://www.adminschoice.com/crontab-quick-reference 82 | 83 | Once this is setup on crontab, the Reddit account will post by itself at the specific intervals! 84 | 85 | ### Cron Alternative 86 | Something that I've found useful a few times is the ability to post only once at a certain time. 87 | This can be done using the `at` tool in Linux. Installation is as such: 88 | ```commandline 89 | $ sudo apt-get install at 90 | ``` 91 | And setting up the one-time post is just as easy: 92 | ```commandline 93 | $ echo "python3 /full/path/post-cron.py" | at 9:00AM 94 | ``` 95 | A good walkthrough of `at` can be found here: 96 | https://tecadmin.net/one-time-task-scheduling-using-at-commad-in-linux/ 97 | 98 | ### Modifying the Post Content 99 | If you setup the script and wanted it to post something different, all that needs to be done is to modify the material inside of `content.txt`. 100 | When the bot posts next, it will use the updated content in the file. 101 | --------------------------------------------------------------------------------