Skip to content

Commit

Permalink
Replace thread with submission.
Browse files Browse the repository at this point in the history
  • Loading branch information
bboe committed Jul 21, 2016
1 parent 5399c32 commit e7345c6
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions docs/pages/reply_bot.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ complexity), among other things.
PRAW is the simplest way to build your own bot using Python.

This tutorial will show you how to build a bot that monitors a particular
subreddit (/r/AskReddit), and replies to new threads that contain simple
subreddit, /r/AskReddit, that replies to new submissions that contain simple
questions with a link to "lmgtfy.com" (Let Me Google This For You).

To do this, there are 3 key components we'll have to address:

1. A way to monitor new threads.
1. A way to monitor new submissions.

2. A way to analyze the titles of those threads and see if they contain a
2. A way to analyze the titles of those submissions and see if they contain a
simple question.

3. A way make the desired reply.
Expand Down Expand Up @@ -58,10 +58,10 @@ Additionally, you'll need to supply the credentials of your bot's account in
the form of the "username" and "password" variables passed to the main Reddit
instance.

Step 1: Monitor New Threads and Grab the titles
Step 1: Monitor New Submissions and Grab the titles
-----------------------------------------------

To get the threads (submissions) to a subreddit, we simply need to call the
To get the submissions (submissions) to a subreddit, we simply need to call the
subreddit method of our "r" object, like so:

.. code-block:: python
Expand All @@ -73,30 +73,30 @@ it if desired.

This code creates a lazy-loaded subreddit object.

Next, we'll need to extract the thread ids for each submission in the
Next, we'll need to extract the submission ids for each submission in the
subreddit.

Here's how:

.. code-block:: python
ids=[]
for thread in subreddit:
ids.append(thread.id)
for submission in subreddit:
ids.append(submission.id)
Once we have the ids, we can create a submission object for each thread, and
Once we have the ids, we can create a submission object for each submission, and
extract/store the titles.

.. code-block:: python
for id_number in ids:
thread = r.submission(id=id_number)
title = thread.title.lower()
submission = r.submission(id=id_number)
title = submission.title.lower()
Step 2: Analyze the Titles
--------------------------

Now that we have the titles of the threads in the "new" feed of /r/AskReddit,
Now that we have the titles of the submissions in the "new" feed of /r/AskReddit,
it's time to see if they contain a simple question in them.

This might mean that they contain strings like:
Expand All @@ -120,8 +120,8 @@ contain any of these:
.. code-block:: python
for id_number in ids:
thread = r.submission(id=id_number)
title = thread.title.lower()
submission = r.submission(id=id_number)
title = submission.title.lower()
for question_type in questions:
if question_type in title:
#make the reply
Expand Down Expand Up @@ -154,15 +154,15 @@ Reddit formatting guidelines), and make the reply:
.. code-block:: python
for id_number in ids:
thread = r.submission(id=id_number)
title = thread.title.lower()
submission = r.submission(id=id_number)
title = submission.title.lower()
for question_type in questions:
if question_type in title:
# make the reply
correct_url = fixurl(title)
reply_text="[Here's a link that might help](http://lmgtfy.com/?q=%s)" % (correct_url)
# send the actual reply request
thread.reply(reply_text)
submission.reply(reply_text)
If all went well, your post should have been made. Keep in mind that if your
bot account is brand new, you'll be limited in how many posts you can make
Expand All @@ -180,11 +180,11 @@ not do the same work twice.
In order to do that, we'll place all the main code inside a 'while' loop.

As for the second part, when your 'subreddit' object returns the information
about the AskReddit threads, they are returned in order, just like you would
about the AskReddit submissions, they are returned in order, just like you would
see if you visited /r/AskReddit/new yourself.

So in order to prevent our bot from checking the same threads twice, we only
need to record the most recent thread ID, and check it when the while loop is
So in order to prevent our bot from checking the same submissions twice, we only
need to record the most recent submission ID, and check it when the while loop is
executed next.

.. code-block:: python
Expand All @@ -197,21 +197,21 @@ executed next.
latest_id=''
This checks to make sure that the code has been run before ("if ids"), and then
assigns the most recent thread ID (newest submitted) to the variable
assigns the most recent submission ID (newest submitted) to the variable
"latest_id".

Finally, one more loop before the main code is executed will prevent any
duplicate work:

.. code-block:: python
# remove any already examined threads
# remove any already examined submissions
if latest_id in ids:
position = ids.index(latest_id)
ids=ids[0:position]
This checks to see if we've already checked any threads in our newly created
list of ids before, and cleaves off those old threads if we have.
This checks to see if we've already checked any submissions in our newly created
list of ids before, and cleaves off those old submissions if we have.

Completed Code
--------------
Expand Down Expand Up @@ -251,19 +251,19 @@ The final code will show you how all these pieces fit together.
for x in subreddit:
ids.append(x.id)
# Remove any already examined threads
# Remove any already examined submissions
if latest_id in ids:
position = ids.index(latest_id)
ids = ids[0:position]
# Identify title strings that match conditions
for id_number in ids:
thread = r.submission(id=id_number)
title = thread.title.lower()
submission = r.submission(id=id_number)
title = submission.title.lower()
for question_type in questions:
if question_type in title:
# make the reply
correct_url = fixurl(title)
reply_text = "[Here's a link that might help]\(http://lmgtfy.com/?q=%s)" % (correct_url)
# send the actual reply request
thread.reply(reply_text)
submission.reply(reply_text)

0 comments on commit e7345c6

Please sign in to comment.