2

So, I'm making a small reddit bot that just scrapes for a term in comments, but I'm getting weird results. I'm very new to python, so this code might be a bit messy and amature-ish.

#! /usr/bin/python

import praw

import pprint

user_agent = ("simple praw script for searching post terms in comments by /u/shadowfire452")
reddit = praw.Reddit(user_agent = user_agent)
reddit.login()
v_fixed = []
subreddit = reddit.get_subreddit('politics' and 'worldnews')

for submission in subreddit.get_hot(limit = 100):
    title = submission.title
    if " " in title.lower(): 
        v_fixed.append(title)
print "The following %d posts might not make much sense ..." % (len(v_fixed))
for fixed in v_fixed:
    print "\t%s" % (fixed)




flat_comment_generator = praw.helpers.flatten_tree(submission.comments)

for comment in flat_comment_generator:
    if "you" in comment.body:
        a = []
        commentz = comment.body
        a.append(commentz)
        print comment.body
        print ("I found %s comments with 'you' in it out of 100 posts") % (len(a))
    else:
           print "I found no comments with 'you' in it"

When I run it, I get:

I found 1 comments with ' ' in it out of 100 posts
I found no comments with ' ' in it
I found no comments with ' ' in it
I found no comments with ' ' in it
I found no comments with ' ' in it

Obviously this is an issue, since I'm getting conflicting answers and 5 replies to 1 request.

1
  • Did you figure this out? Commented Feb 26, 2014 at 4:17

1 Answer 1

1
import praw # simple interface to the reddit API, also handles rate limiting of requests
import re
from collections import deque 
from time import sleep

USERNAME  = ""
PASSWORD  = ""
USERAGENT = "bot/1.0 by USERNAME"

r = praw.Reddit(USERAGENT)
r.login(USERNAME, PASSWORD) # necessary if your bot will talk to people

cache = deque(maxlen = 200) # To make sure we don't duplicate effort

# Set of words to find in the comment body.
# I have changed this to a set.
words = set(["these", "are", "the", "words", "to", "find"])

def word_check(comment_body, words):
    # Will split the comment_body into individual words and check each for membership in words

    # Split comment into words
    comment_words = comment_body.split()

    # Check each word for hot word and return True if found
    for word in comment_words:
        if word in words:
            return True

    # Return false if no words in words
    return False

def bot_action(comment, reply):
    print "Body:", comment.body
    print "Found word in:", comment.subreddit.display_name
    comment.reply(reply)

# Loop through comments
running = True
while running:
    all = r.get_comments('politics', limit = None)
    for comment in all:
        # if comment id exists in cache, break
        if comment.id in cache:
            break
        cache.append(comment.id) # cache already found comment id
        # execute method for comment body and hotword(s)
        if word_check(comment.body, words):
            try:
                # action the bot to reply
                bot_action(comment, "Hello world")
            except KeyboardInterrupt:
                running = False
            except praw.errors.APIException, e:
                print "[ERROR]:", e
                print "Sleeping for 30 seconds"
                sleep(30)
            except Exception, e: # In reality you don't want to just catch everything like this, but this is toy code.
                print "[ERROR]:", e
                print "Blindly handling error"
                continue
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.