From 06b0326f9a5cad001e09937414d4c2cf68b6e135 Mon Sep 17 00:00:00 2001 From: Andrew Alexander Date: Sun, 12 Jul 2015 14:56:59 -0500 Subject: [PATCH 1/2] Add OAuth support; update README --- README.md | 68 ++++++++++++++++++++++++++++++++++--------------- authenticate.py | 11 ++++++++ setup.py | 4 +-- twitchy.py | 2 +- 4 files changed, 61 insertions(+), 24 deletions(-) create mode 100644 authenticate.py diff --git a/README.md b/README.md index 56cb7c9..48fdbe5 100644 --- a/README.md +++ b/README.md @@ -28,33 +28,59 @@ setup.py will also print out a string for you to put into your sidebar to allow with your bots username and the subreddit it's running in (taken from config.py) substituted where marked. +### OAuth configuration +The use of OAuth in PRAW requires a praw.ini file located in your config folder (varies by platform) or in the root directory if this project (overrides global settings) + +No matter where you put it, you need a praw.ini file with the following included: +``` +[DEFAULT] +domain: www.reddit.com +oauth_client_id: +oauth_client_secret: +oauth_redirect_uri: http://127.0.0.1:65010/authorize_callback +oauth_refresh_token: +``` +#### Authenticating with OAuth + +There a few steps to authenticate via OAuth: +1. Get the `client_id` and `secret_access_key` from [Reddit](https://www.reddit.com/prefs/apps/) +2. Copy `client_id` to praw.ini as `oath_client_id` and `secret_access_key` as `oauth_client_secret` in praw.ini +3. Run `authenticate.py`. It will open up your default web browser and present a page to grant access to the application +4. You will be redirected to the url at `oauth_redirect_uri` in praw.ini. If you leave the default value, you will likely get a page not found error. This is okay. All you need is the code at the end of URL: `http://127.0.0.1:65010/authorize_callback?state=obtainingAuthentication&code=THIS_IS_THE_CODE_YOU_WANT` +5. Copy the code into the prompt and press enter +6. The script gets the refresh token and prints it to stdout. Copy the refresh token to praw.ini as `oauth_refresh_token` + +Now you will be good to go! You will not have to re-run authenticate.py unless you de-authorize your application from Reddit or you allow your refresh token to expire. + +See [here](http://praw.readthedocs.org/en/latest/pages/oauth.html) for more details on the PRAW implementation of OAuth + ###twitchbot_config All of the following config is editable in default_wiki_config.json before you run setup.py, and after you've ran setup.py the bot will pull it from `/wiki/twitchbot_config` { - "max_streams_displayed":"12", - "max_title_length":"50", - "thumbnail_size":{ - "width":"80", - "height":"50" - }, - "stream_marker_start":"[](#startmarker)", - "stream_marker_end":"[](#endmarker)", - "string_format":"> 1. **[{name}](http://twitch.tv/{name})** -**{viewercount} Viewers**\n[{title}](http://twitch.tv/{name})\n", - "no_streams_string":"**No streams are currently live.**\n", - "wikipages":{ - "error_log":"twitchbot_error_log", - "stream_list":"streams", - "ban_list":"banned_streams" - }, - "allowed_games":[], - "messages":{ - "success":"Your stream will be added to the list of livestreams in the sidebar, it will display the next time you are live on twitch.tv.\n\nProblems? [Contact the moderators here](http://www.reddit.com/message/compose?to=%2Fr%2F{subreddit})\n\n Do not reply to this message.", - "banned":"Sorry, but that stream is banned from this subreddit. If you feel this is an incorrect ban, [please message the moderators here](http://www.reddit.com/message/compose?to=%2Fr%2F{subreddit})\n\n Do not reply to this message.", - "already_exists":"Your stream is already in the list of livestreams that this bot checks. If you have just messaged your stream, please wait 5-10 minutes for the sidebar to update.\n\n Problems? Contact the moderators [here](http://www.reddit.com/message/compose?to=%2Fr%2F{subreddit})\n\n Do not reply to this message." - } + "max_streams_displayed":"12", + "max_title_length":"50", + "thumbnail_size":{ + "width":"80", + "height":"50" + }, + "stream_marker_start":"[](#startmarker)", + "stream_marker_end":"[](#endmarker)", + "string_format":"> 1. **[{name}](http://twitch.tv/{name})** -**{viewercount} Viewers**\n[{title}](http://twitch.tv/{name})\n", + "no_streams_string":"**No streams are currently live.**\n", + "wikipages":{ + "error_log":"twitchbot_error_log", + "stream_list":"streams", + "ban_list":"banned_streams" + }, + "allowed_games":[], + "messages":{ + "success":"Your stream will be added to the list of livestreams in the sidebar, it will display the next time you are live on twitch.tv.\n\nProblems? [Contact the moderators here](http://www.reddit.com/message/compose?to=%2Fr%2F{subreddit})\n\n Do not reply to this message.", + "banned":"Sorry, but that stream is banned from this subreddit. If you feel this is an incorrect ban, [please message the moderators here](http://www.reddit.com/message/compose?to=%2Fr%2F{subreddit})\n\n Do not reply to this message.", + "already_exists":"Your stream is already in the list of livestreams that this bot checks. If you have just messaged your stream, please wait 5-10 minutes for the sidebar to update.\n\n Problems? Contact the moderators [here](http://www.reddit.com/message/compose?to=%2Fr%2F{subreddit})\n\n Do not reply to this message." + } } ### Config information diff --git a/authenticate.py b/authenticate.py new file mode 100644 index 0000000..1e97903 --- /dev/null +++ b/authenticate.py @@ -0,0 +1,11 @@ +import praw, webbrowser + +r = praw.Reddit('Temporary reddit app to obtain authentication information') + +url = r.get_authorize_url(state='obtainingAuthentication', scope='modconfig modwiki wikiread', refreshable=True) +webbrowser.open(url) +reddit_code = raw_input('Please enter the code from the redirect url: ') + +access_information = r.get_access_information(reddit_code) + +print 'Refresh token: ' + str(access_information.get('refresh_token')) diff --git a/setup.py b/setup.py index 8975ba6..b3e7060 100644 --- a/setup.py +++ b/setup.py @@ -2,14 +2,14 @@ import json import HTMLParser import requests -from config import username, password, subreddit +from config import refresh_token, subreddit def wikilog(r, subreddit, wikipage, error): r.edit_wiki_page(subreddit, wikipage, error, error) if __name__ == "__main__": r = praw.Reddit("Twitch.tv sidebar bot for {} by /u/andygmb".format(subreddit)) - r.login(username=username, password=password) + r.refresh_access_information() sub = r.get_subreddit(subreddit) with open("default_wiki_config.json", "r") as configjson: diff --git a/twitchy.py b/twitchy.py index dd78afe..1deeebe 100644 --- a/twitchy.py +++ b/twitchy.py @@ -42,7 +42,7 @@ def wikilog(self, error): def reddit_setup(self): print "Logging in" r = praw.Reddit("Sidebar livestream updater for /r/{} by /u/andygmb ".format(subreddit)) - r.login(username=username, password=password) + r.refresh_access_information() sub = r.get_subreddit(subreddit) return r, sub From 095049262c1864e0585bdbfa5f2dd94cd83170d0 Mon Sep 17 00:00:00 2001 From: Andrew Alexander Date: Sun, 12 Jul 2015 15:22:00 -0500 Subject: [PATCH 2/2] Add scope for check_inbox; update import config statements --- README.md | 4 ++-- authenticate.py | 2 +- config.py | 1 - setup.py | 44 ++++++++++++++++++++++---------------------- twitchy.py | 18 +++++++++--------- 5 files changed, 34 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 48fdbe5..c32cdb0 100644 --- a/README.md +++ b/README.md @@ -16,10 +16,10 @@ This will create the following wiki pages (customizable in default_wiki_config.j # The imported config from default_wiki_config.json /wiki/twitchbot_config - # A list of banned twitch.tv usernames seperated by newlines + # A list of banned twitch.tv usernames separated by newlines /wiki/banned_streams - # A list of twitch.tv usernames seperated by newlines + # A list of twitch.tv usernames separated by newlines /wiki/streams setup.py will also print out a string for you to put into your sidebar to allow people to PM the bot their livestreams in the correct format: diff --git a/authenticate.py b/authenticate.py index 1e97903..bf48d70 100644 --- a/authenticate.py +++ b/authenticate.py @@ -2,7 +2,7 @@ r = praw.Reddit('Temporary reddit app to obtain authentication information') -url = r.get_authorize_url(state='obtainingAuthentication', scope='modconfig modwiki wikiread', refreshable=True) +url = r.get_authorize_url(state='obtainingAuthentication', scope='modconfig modwiki wikiread privatemessages', refreshable=True) webbrowser.open(url) reddit_code = raw_input('Please enter the code from the redirect url: ') diff --git a/config.py b/config.py index 126e920..3cc35e3 100644 --- a/config.py +++ b/config.py @@ -1,3 +1,2 @@ username = "username" -password = "password" subreddit = "subreddit" \ No newline at end of file diff --git a/setup.py b/setup.py index b3e7060..aef54d0 100644 --- a/setup.py +++ b/setup.py @@ -2,34 +2,34 @@ import json import HTMLParser import requests -from config import refresh_token, subreddit +from config import username, subreddit def wikilog(r, subreddit, wikipage, error): r.edit_wiki_page(subreddit, wikipage, error, error) if __name__ == "__main__": - r = praw.Reddit("Twitch.tv sidebar bot for {} by /u/andygmb".format(subreddit)) - r.refresh_access_information() - sub = r.get_subreddit(subreddit) + r = praw.Reddit("Twitch.tv sidebar bot for {} by /u/andygmb".format(subreddit)) + r.refresh_access_information() + sub = r.get_subreddit(subreddit) - with open("default_wiki_config.json", "r") as configjson: - try: - config = json.load(configjson) - except ValueError: - print "Invalid JSON in local file: default_wiki_config.json" - wikilog(r, sub, "twitchbot_error_log", "Invalid JSON in local file: default_wiki_config.json") + with open("default_wiki_config.json", "r") as configjson: + try: + config = json.load(configjson) + except ValueError: + print "Invalid JSON in local file: default_wiki_config.json" + wikilog(r, sub, "twitchbot_error_log", "Invalid JSON in local file: default_wiki_config.json") - try: - r.edit_wiki_page(sub, "twitchbot_config", json.dumps(config, indent=4), "Initial setup from setup.py") - except requests.exceptions.HTTPError: - print "Couldn't access /wiki/{}, reddit may be down.".format("twitchbot_config") - wikilog(r, sub, config["wikipages"]["error_log"], "Couldn't access wiki page, reddit may be down.") + try: + r.edit_wiki_page(sub, "twitchbot_config", json.dumps(config, indent=4), "Initial setup from setup.py") + except requests.exceptions.HTTPError: + print "Couldn't access /wiki/{}, reddit may be down.".format("twitchbot_config") + wikilog(r, sub, config["wikipages"]["error_log"], "Couldn't access wiki page, reddit may be down.") - for wikipage in config["wikipages"].values(): - try: - r.edit_wiki_page(sub, wikipage, " ", "Initial setup from setup.py") - except requests.exceptions.HTTPError: - print "Couldn't access /wiki/{}, reddit may be down.".format(wikipage) - wikilog(r, sub, wikipage, "Couldn't access wiki page, reddit may be down.") + for wikipage in config["wikipages"].values(): + try: + r.edit_wiki_page(sub, wikipage, " ", "Initial setup from setup.py") + except requests.exceptions.HTTPError: + print "Couldn't access /wiki/{}, reddit may be down.".format(wikipage) + wikilog(r, sub, wikipage, "Couldn't access wiki page, reddit may be down.") - print "http://www.reddit.com/message/compose?to={username}&subject=Twitch.tv+request+%2Fr%2F{subreddit}&message=http%3A%2F%2Fwww.twitch.tv%2F{username}".format(username=username, subreddit=subreddit) \ No newline at end of file + print "http://www.reddit.com/message/compose?to={username}&subject=Twitch.tv+request+%2Fr%2F{subreddit}&message=http%3A%2F%2Fwww.twitch.tv%2F{username}".format(username=username, subreddit=subreddit) \ No newline at end of file diff --git a/twitchy.py b/twitchy.py index 1deeebe..0b1beab 100644 --- a/twitchy.py +++ b/twitchy.py @@ -4,7 +4,7 @@ import praw import HTMLParser import json -from config import username, password, subreddit +from config import subreddit from PIL import Image from StringIO import StringIO @@ -13,14 +13,14 @@ def chunker(seq, size): return (seq[pos:pos + size] for pos in xrange(0, len(seq), size)) class configuration(): - def __init__(self): - self.r, self.subreddit = self.reddit_setup() - self.config = self.get_config() - self.streams = self.wikipage_check(self.config["wikipages"]["stream_list"]) - self.banned = self.wikipage_check(self.config["wikipages"]["ban_list"]) - self.messages = self.check_inbox() - - def get_config(self): + def __init__(self): + self.r, self.subreddit = self.reddit_setup() + self.config = self.get_config() + self.streams = self.wikipage_check(self.config["wikipages"]["stream_list"]) + self.banned = self.wikipage_check(self.config["wikipages"]["ban_list"]) + self.messages = self.check_inbox() + + def get_config(self): try: config = self.r.get_wiki_page(self.subreddit,"twitchbot_config").content_md try: