Skip to content

Commit

Permalink
Bot can now submit and edit the previous comment. Also, tweaked the t…
Browse files Browse the repository at this point in the history
…emplate a bit. Bot runs as is now.
  • Loading branch information
aperson committed Mar 7, 2012
1 parent 1372da8 commit bf69a40
Showing 1 changed file with 40 additions and 23 deletions.
63 changes: 40 additions & 23 deletions SuggestionBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ def submit(self, subreddit, title, url=None, text=None):
except urllib.error.HTTPError:
print('Failed to submit!')

def edit_submission(self, thing_id, text):
'''Edits the submission text for a given thing_id.'''
body = {'thing_id' : thing_id, 'text' : text, 'uh' : self.modhash}
body = urlencode(body).encode('utf-8')
try:
with self.opener.open('http://www.reddit.com/api/editusertext', body) as w:
return True
except urllib.error.HTTPError:
return False

def get_feed(self, url):
'''Takes a url sans the http://www.reddit.com and without the .json and returns a dict.'''
if not url.startswith('/'): url = '/' + url
Expand All @@ -59,7 +69,7 @@ def get_feed(self, url):
return output['data']['children']
else:
# We don't care about the .self text of the last submission
return output[1]['data']['children']
return output

def bot():
'''This is the main bot function that, when ran, will grab the last submission+comments, edit
Expand All @@ -68,28 +78,24 @@ def bot():
submission_template = '''Hello /r/Minecraft, welcome to the official suggestion post for today \
({date}). This is the place where all [Suggestion], [Idea], [Mod Request], and other \
submissions of the like are to go. If you have an [Idea], post it as a top-level comment \
and if it's a good one, hopefully it'll be upvoted and commented on.
Here's the top three comments from the last submission:
{comment_0}
{comment_1}
and if it's a good one, hopefully it'll be upvoted and commented on.\
\n\nHere's the top three comments from the last submission:\
\n\n{comment_0}\
\n\n{comment_1}\
\n\n{comment_2}\
\n\n----\
\n\nNavigation:\
\n\n[<previous>]({last})'''

{comment_2}
----
Navigation:
[<last>]({last})'''

comment_template = '''**{author}**[{score}][+{ups}/-{downs}]:\n\n>{body}'''
comment_template = '''**{author}** [+{ups}/-{downs}]:\n\n>{body}'''

# login
r = Reddit(USERNAME, PASSWORD)
r.login()
time.sleep(2)

strfdate = time.strftime('%y/%m/%d')

# get prequisite info about last submission
submission_history = r.get_feed('/user/{}/submitted/'.format(USERNAME))
time.sleep(2)
Expand All @@ -101,32 +107,43 @@ def bot():
for i in submission_history:
if i['data']['title'].startswith('[Suggestion]'):
last_url = i['data']['permalink']
last_comments = r.get_feed(last_url)
last_thing_id = i['data']['name']
last_submission = r.get_feed(last_url)
last_text = last_submission[0]['data']['children'][0]['data']['selftext']
last_comments = last_submission[1]['data']['children']
time.sleep(2)
break

# build from the comment_template(s) (sometimes I don't feel like doing this the 'right' way)
comment_0 = comment_template.format(author=last_comments[0]['data']['author'],
score=last_comments[0]['data']['score'],
ups=last_comments[0]['data']['ups'],
downs=last_comments[0]['data']['downs'],
body=last_comments[0]['data']['body']
)

comment_1 = comment_template.format(author=last_comments[1]['data']['author'],
score=last_comments[1]['data']['score'],
ups=last_comments[1]['data']['ups'],
downs=last_comments[1]['data']['downs'],
body=last_comments[1]['data']['body']
)

comment_2 = comment_template.format(author=last_comments[2]['data']['author'],
score=last_comments[2]['data']['score'],
ups=last_comments[2]['data']['ups'],
downs=last_comments[2]['data']['downs'],
body=last_comments[2]['data']['body']
)

submission_title = '''[Suggestion] Post for {date}'''.format(date=time.strftime('%y/%m/%d')
submission_text = submission_template.format(comment_0=comment_0, comment_1=comment_1,
comment_2=comment_2, last=last_url)
submission_title = '''[Suggestion] Post for {}'''.format(strfdate)
submission_text = submission_template.format(date=strfdate, comment_0=comment_0,
comment_1=comment_1, comment_2=comment_2,
last=last_url
)

# Submit!
submission_url = r.submit(SUBREDDIT, submission_title, text=submission_text)

# Edit the last submission so it includes a <next> link
r.edit_submission(last_thing_id, '{}|[<next>]({})'.format(last_text, submission_url))

if __name__ == '__main__':
bot()

0 comments on commit bf69a40

Please sign in to comment.