Skip to content

Instantly share code, notes, and snippets.

@NickHurst
Last active November 9, 2015 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickHurst/ffc049ced927dad4bd73 to your computer and use it in GitHub Desktop.
Save NickHurst/ffc049ced927dad4bd73 to your computer and use it in GitHub Desktop.

Bot Name

Installations

This script requires python 3, praw, and praw-oauth2util.

To install praw type in your command line:

pip install praw
pip install praw-oauth2util

Reddit OAuth Setup

  • Go here on the account you want the bot to run on
  • Click on create a new app.
  • Give it a name.
  • Select script from the radio buttons.
  • Set the redirect uri to http://127.0.0.1:65010/authorize_callback
  • After you create it you will be able to access the app key and secret.
  • The app key is found here. Add this to the app_key variable in the oauth.txt file.

here (Do not give out to anybody)

  • And the app secret here. Add this to the app_secret variable in the oauth.txt file.

here (Do not give out to anybody)

Note: On the first run of the bot you'll need to run it on a system that has a desktop environment. So, if you're planning on running the bot on a server or VPS somewhere, run it on your computer first. The first run will require you to authenticate it with the associated Reddit account by opening a web page that looks like this:

authentication page

It will list different things it's allowed to do depending on the bots scope. After you authenticate it, that page won't pop up again unless you change the OAuth credentials. And you'll be free to run it on whatever environment you choose.

Config

Set the SUBREDDIT_NAME variable to the name of the subreddit, don't include the /r/.

License

The MIT License (MIT)

Copyright (c) 2015 Nick Hurst

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import time
import praw
import OAuth2Util
# name of the subreddit you want to check titles (don't include the /r/)
SUBREDDIT_NAME = ''
# the keywords you're looking for
KEYWORDS = ['keyword1', 'keyword2']
# the username you want the messages to be sent to
USERNAME = ''
def check_posts(r, o):
submission_stream = praw.helpers.submission_stream(r, SUBREDDIT_NAME)
print('Checking posts...')
for subm in submission_stream:
o.refresh()
for keyword in KEYWORDS:
if keyword.lower() in subm.title.lower():
r.send_message(USERNAME, 'Found word', subm.permalink)
break
def main():
r = praw.Reddit('post title checker v1.0 /u/cutety')
o = OAuth2Util.OAuth2Util(r, print_log=True)
while True:
try:
check_posts(r, o)
except Exception as e:
print('ERROR: {}'.format(e))
time.sleep(60)
if __name__ == '__main__':
main()
# Config
scope=privatemessages,read
refreshable=True
# Appinfo
app_key=
app_secret=
# Token
token=None
refresh_token=None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment