Skip to content

Commit

Permalink
Merge pull request #38 from mattabullock/praw-update
Browse files Browse the repository at this point in the history
Update to support praw v5.0.1
  • Loading branch information
mattabullock authored Aug 25, 2017
2 parents 3a47f4a + 91c8335 commit 68129d0
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 62 deletions.
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
Baseball GDT Bot by Matt Bullock
Baseball GDT Bot
=====================================

### Current Version: 3.0.2
### Current Version: 3.1.0

The point of this project is to create a bot that will generate a
game discussion thread that contains live linescore and boxscore,
post it in the correct subreddit for that team, and keep it
updated throughout the game.

Version 1.0 was written in a mix of Python and Java, and has been
completely ported to Python for v2.0 and v3.0 (this version).

---

### Set Up OAuth

Go to reddit.com’s app page, click on the “are you a developer? create an app” button. Fill out the name, description and about url. Name must be filled out, but the rest doesn’t. Write whatever you please. For redirect uri set it to `http://127.0.0.1:65010/authorize_callback`. All four variables can be changed later.
Go to reddit.com’s app page, click on the “are you a developer? create an app” button. Fill out the name, description and about url. Name must be filled out, but the rest doesn’t. Write whatever you please. For redirect uri set it to `http://127.0.0.1:8080`. All four variables can be changed later.

Next, open setup.py, fill in the client_id, client_secret and redirect_uri fields and run the script. Your browser will open. Click allow on the displayed web page.
Copy sample_settings.json to the src folder and rename it to settings.json. Fill in the CLIENT_ID, CLIENT_SECRET, and REDIRECT_URI fields in the settings.json file and save.

Enter the uniqueKey&code from the URL into the console -- wrapped in single quotes -- and the access information will be printed. This includes the final bit of info you need, the refresh token.
Run the script. Your browser will open. Click allow on the displayed web page.

Finally, Copy sample_settings.json to the src folder and rename it to settings.json. Fill in the CLIENT_ID, CLIENT_SECRET, REDIRECT_URI and REFRESH_TOKEN fields in the settings.json file and save.
Enter the code from the URL into the console and the access information will be printed. This includes the final bit of info you need, the refresh token.

Fill in the REFRESH_TOKEN field in the settings.json file and save.

### Configuration

Expand Down Expand Up @@ -56,26 +58,29 @@ To use the default settings, copy `sample_settings.json` into `src/settings.json
* `THREAD_SETTINGS` - what to include in game threads, example footer: "**Remember to sort by new to keep up!**"

* `POST_THREAD_SETTINGS` - what to include in postgame threads, example footer: "**Remember to sort by new to keep up!**"
---

---

If something doesn't seem right, feel free to message me or post it as a bug here.

This was written in Python 2.7, so beware if you are running Python 3 or
above that it may not work correctly. Also make sure you install
praw and simplejson before running!

Modules being used:

praw - interfacing reddit
praw v5.0.1 - interfacing reddit
simplejson - JSON parsing
urllib2 - pulling data from MLB servers
ElementTree - XML parsing

### Updates

#### v3.1.0
* Updated to praw version v5.0.1

#### v3.0.2
* GUI added.
* GUI added.

#### v3.0.1
* Now uses OAuth!
Expand Down
3 changes: 2 additions & 1 deletion sample_settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"CLIENT_ID": "XXX",
"CLIENT_SECRET": "XXX",
"REDIRECT_URI": "http://127.0.0.1:65010/authorize_callback",
"USER_AGENT": "",
"REDIRECT_URI": "http://127.0.0.1:8080",
"REFRESH_TOKEN": "XXX",
"BOT_TIME_ZONE": "ET",
"TEAM_TIME_ZONE": "ET",
Expand Down
40 changes: 30 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
import praw
r = praw.Reddit('OAuth Baseball-GDT Ver. 3.0.0 Setup')
r.set_oauth_app_info(client_id='xxx',
client_secret='xxx',
redirect_uri='http://127.0.0.1:65010/authorize_callback')

url = r.get_authorize_url('uniqueKey', 'submit edit read modposts privatemessages', True)
import os
import sys
import simplejson as json
import webbrowser

cwd = os.path.dirname(os.path.realpath(__file__))
with open(cwd + '/src/settings.json') as data:
settings = json.load(data)

CLIENT_ID = settings.get('CLIENT_ID')
if CLIENT_ID == None: sys.exit("Missing CLIENT_ID")

CLIENT_SECRET = settings.get('CLIENT_SECRET')
if CLIENT_SECRET == None: sys.exit("Missing CLIENT_SECRET")

USER_AGENT = settings.get('USER_AGENT')
if USER_AGENT == None: sys.exit("Missing USER_AGENT")

REDIRECT_URI = settings.get('REDIRECT_URI')
if REDIRECT_URI == None: sys.exit("Missing REDIRECT_URI")


r = praw.Reddit(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri=REDIRECT_URI,
user_agent=USER_AGENT)

url = r.auth.url(['identity', 'submit', 'edit', 'read', 'modposts', 'privatemessages'], '...', 'permanent')
webbrowser.open(url)

var_input = input("Enter uniqueKey&code: ")
access_information = r.get_access_information(var_input)
print access_information
raw_input()
var_input = raw_input("Enter code: ")
access_information = r.auth.authorize(var_input)
print "Refresh token: " + access_information
60 changes: 23 additions & 37 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,7 @@
class Bot:

def __init__(self):
self.BOT_TIME_ZONE = None
self.TEAM_TIME_ZONE = None
self.POST_TIME = None
self.USERNAME = None
self.PASSWORD = None
self.SUBREDDIT = None
self.TEAM_CODE = None
self.PREGAME_THREAD = None
self.POST_GAME_THREAD = None
self.STICKY = None
self.SUGGESTED_SORT = None
self.MESSAGE = None
self.INBOXREPLIES = None
self.PRE_THREAD_SETTINGS = None
self.THREAD_SETTINGS = None
self.POST_THREAD_SETTINGS = None
return

def read_settings(self):
import os
Expand All @@ -52,6 +37,9 @@ def read_settings(self):
self.CLIENT_SECRET = settings.get('CLIENT_SECRET')
if self.CLIENT_SECRET == None: return "Missing CLIENT_SECRET"

self.USER_AGENT = settings.get('USER_AGENT')
if self.USER_AGENT == None: return "Missing USER_AGENT"

self.REDIRECT_URI = settings.get('REDIRECT_URI')
if self.REDIRECT_URI == None: return "Missing REDIRECT_URI"

Expand Down Expand Up @@ -126,12 +114,10 @@ def run(self):
print error_msg
return

r = praw.Reddit('OAuth Baseball-GDT-Bot V. 3.0.1'
'https://github.com/mattabullock/Baseball-GDT-Bot')
r.set_oauth_app_info(client_id=self.CLIENT_ID,
r = praw.Reddit(client_id=self.CLIENT_ID,
client_secret=self.CLIENT_SECRET,
redirect_uri=self.REDIRECT_URI)
r.refresh_access_information(self.REFRESH_TOKEN)
refresh_token=self.REFRESH_TOKEN,
user_agent=self.USER_AGENT)

if self.TEAM_TIME_ZONE == 'ET':
time_info = (self.TEAM_TIME_ZONE,0)
Expand Down Expand Up @@ -190,8 +176,8 @@ def run(self):
while True:
try:
posted = False
subreddit = r.get_subreddit(self.SUBREDDIT)
for submission in subreddit.get_new():
subreddit = r.subreddit(self.SUBREDDIT)
for submission in subreddit.new():
if submission.title == title:
print "Pregame thread already posted, getting submission..."
submission.edit(edit.generate_pre_code(directories))
Expand All @@ -201,14 +187,14 @@ def run(self):
print "Submitting pregame thread..."
if self.STICKY and 'sub' in locals():
try:
sub.unsticky()
sub.mod.sticky(state=False)
except Exception, err:
print "Unsticky failed, continuing."
sub = r.submit(self.SUBREDDIT, title, edit.generate_pre_code(directories), send_replies=self.INBOXREPLIES)
sub = subreddit.submit(title, selftext=edit.generate_pre_code(directories), send_replies=self.INBOXREPLIES)
print "Pregame thread submitted..."
if self.STICKY:
print "Stickying submission..."
sub.sticky()
sub.mod.sticky()
print "Submission stickied..."
print "Sleeping for two minutes..."
print datetime.strftime(datetime.today(), "%d %I:%M %p")
Expand All @@ -226,8 +212,8 @@ def run(self):
check = datetime.today()
try:
posted = False
subreddit = r.get_subreddit(self.SUBREDDIT)
for submission in subreddit.get_new():
subreddit = r.subreddit(self.SUBREDDIT)
for submission in subreddit.new():
if submission.title == title:
print "Thread already posted, getting submission..."
sub = submission
Expand All @@ -236,27 +222,27 @@ def run(self):
if not posted:
if self.STICKY and 'sub' in locals():
try:
sub.unsticky()
sub.mod.sticky(state=False)
except Exception, err:
print "Unsticky failed, continuing."

print "Submitting game thread..."
sub = r.submit(self.SUBREDDIT, title, edit.generate_code(d,"game"), send_replies=self.INBOXREPLIES)
sub = subreddit.submit(title, selftext=edit.generate_code(d,"game"), send_replies=self.INBOXREPLIES)
print "Game thread submitted..."

if self.STICKY:
print "Stickying submission..."
sub.sticky()
sub.mod.sticky()
print "Submission stickied..."

if self.SUGGESTED_SORT != None:
if self.SUGGESTED_SORT != "":
print "Setting suggested sort to " + self.SUGGESTED_SORT + "..."
sub.set_suggested_sort(self.SUGGESTED_SORT)
print "Suggested sort set..."

if self.MESSAGE:
print "Messaging Baseballbot..."
r.send_message('baseballbot', 'Gamethread posted', sub.short_link)
r.redditor('baseballbot').message('Gamethread posted', sub.shortlink)
print "Baseballbot messaged..."

print "Sleeping for two minutes..."
Expand Down Expand Up @@ -315,23 +301,23 @@ def run(self):
if pgt_submit:
if self.STICKY and 'sub' in locals():
try:
sub.unsticky()
sub.mod.sticky(state=False)
except Exception, err:
print "Unsticky failed, continuing."

if self.POST_GAME_THREAD:
print "Submitting postgame thread..."
posttitle = edit.generate_title(d,"post")
sub = r.submit(self.SUBREDDIT, posttitle, edit.generate_code(d,"post"), send_replies=self.INBOXREPLIES)
sub = subreddit.submit(posttitle, selftext=edit.generate_code(d,"post"), send_replies=self.INBOXREPLIES)
print "Postgame thread submitted..."

if self.STICKY:
print "Stickying submission..."
sub.sticky()
sub.mod.sticky()
print "Submission stickied..."
time.sleep(10)
break
else:
else:
print "Sleeping for one minute..."
print datetime.strftime(check, "%d %I:%M %p")
time.sleep(60)
Expand Down

0 comments on commit 68129d0

Please sign in to comment.