Skip to content

Instantly share code, notes, and snippets.

@NickHurst
Last active October 13, 2015 16:46
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/178272fe7a7ff21250bf to your computer and use it in GitHub Desktop.
Save NickHurst/178272fe7a7ff21250bf to your computer and use it in GitHub Desktop.

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/.

# Config
scope=edit,modconfig,read,save
refreshable=True
# Appinfo
app_key=
app_secret=
# Token
token=None
refresh_token=None
import time
import praw
import OAuth2Util
from datetime import datetime, timedelta
# User config
# --------------------------------------------------------------------
# Don't include the /r/
SUBREDDIT_NAME = ''
# --------------------------------------------------------------------
def create_timestamp():
timestamp = datetime.fromtimestamp(time.time())
# round the minutes to the nearest multiple of 10
timestamp += timedelta(minutes=5)
timestamp -= timedelta(minutes=timestamp.minute % 10)
# the format it
timestamp = timestamp.strftime('%Y-%m-%d %H:%M')
return timestamp
def set_stylesheet_timestamp(r):
timestamp = create_timestamp()
new_selector = '#dms {{ content: "{0}"}}'.format(timestamp)
stylesheet = r.get_stylesheet(SUBREDDIT_NAME)['stylesheet']
# this is assuming the timestamp is the last line and
# formatted on one line like #dms { content: "2015-10-11 17:50"}
stylesheet = stylesheet[:-35] + new_selector
# then set the stylesheet
print('Setting stylesheet...')
r.set_stylesheet(SUBREDDIT_NAME, stylesheet)
def main():
r = praw.Reddit(user_agent='Stylesheet Timestamp Set v1.0 /u/cutety')
r.config.decode_html_entities = True
o = OAuth2Util.OAuth2Util(r, print_log=True)
while True:
try:
o.refresh()
set_stylesheet_timestamp(r)
except Exception as e:
print('ERROR: {}'.format(e))
print('Sleeping...')
time.sleep(10 * 60) # sleep 10 minutes
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment