Skip to content

Instantly share code, notes, and snippets.

@NickHurst
Last active October 28, 2015 04:15
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/be325fc5c298a71f667f to your computer and use it in GitHub Desktop.
Save NickHurst/be325fc5c298a71f667f 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.

# Config
scope=submit,read,identity,edit
refreshable=True
# Appinfo
app_key=
app_secret=
# Token
token=None
refresh_token=None
import time
import praw
import OAuth2Util
SUBREDDIT_NAME = ''
def search_for_sunsets(r, o):
submission_stream = praw.helpers.submission_stream(r, 'all')
print("Searching for posts...")
for submission in submission_stream:
o.refresh()
if submission.over_18: # skip if NSFW
continue
if 'imgur' not in submission.url and 'deviantart' not in submission.url:
continue
if 'sunset' in submission.title:
make_post(r, submission)
def make_post(r, subm):
title = "\"{}\" by /u/{} in /r/{}".format(subm.title, subm.author,
subm.subreddit)
comment = '[Original post]({}) in /r/{}'.format(subm.permalink,
subm.subreddit)
print("Making post...")
while True:
try:
xpost = r.submit(SUBREDDIT_NAME, title, url=subm.url, captcha=None)
xpost.add_comment(comment)
break
except praw.errors.AlreadySubmitted as e:
print("Already submitted skipping...")
break
except praw.errors.RateLimitExceeded as e:
print(("ERROR: Rate limit exceeded sleeping for " +
"{} seconds".format(e.sleep_time)))
time.sleep(e.sleep_time)
def main():
print("Logging in...")
r = praw.Reddit('Sunset_Xpost v1.0 /u/cutety')
o = OAuth2Util.OAuth2Util(r, print_log=True)
while True:
try:
search_for_sunsets(r, o)
except praw.errors.HTTPException:
print("Reddit is down. Sleeping...")
time.sleep(360)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment