def main():
    r = praw.Reddit("any platform/1.0 (by /u/dont_call_it_cali)")
    r.login("dont_call_it_cali", "*******")

    target_text = r"^.*\bcali\b.*$"
    response_text = r'''http://i.imgur.com/sdrbmHj.jpeg

The name of the state is California.'''

    blacklist = [
        'CoDCompetitive', 'MMA', 'OpTicGaming', 'SuicideWatch', 'Colombia',
        'leagueoflegends'
    ]
    processed = []

    while True:
        for c in comment_stream(r, 'all'):
            if re.match(target_text, c.body, re.IGNORECASE):
                if c.link_id not in processed:
                    if c.subreddit.display_name in blacklist:
                        # subreddit is on the black list, skip replying to it
                        print "skipping comment because subreddit", c.subreddit.display_name, "is in blacklist..."
                        continue

                    handle_ratelimit(c.reply, response_text)
                    processed.append(c.link_id)
                    print "replied to", c.author.name, "in", c.link_url, "at", datetime.datetime.now(
                    )
                else:
                    print "skipping because we already replied in this thread:", c.link_id, c.link_url, "at", datetime.datetime.now(
                    )
示例#2
0
 def start(self):
     self.reddit.login(self.config['username'], self.config['password'])
     while True:
         try:
             for comment in comment_stream(self.reddit, 'all'):
                 self.processComment(comment)
         except KeyboardInterrupt:
             return
         except:
             traceback.print_exc()
             time.sleep(15)
def getComments():

    user_agent = "User-Agent: CommentGrabber v0.1 (by /u/sjuha and /u/teefour)"
    r = praw.Reddit(user_agent=user_agent)

    comments = list()

    for comment in comment_stream(r, 'all', limit=100000):
        comments.append(comment)

        if len(comments) > 100:
            return comments
            break
示例#4
0
def get_comments():
    com_list = list()

    for comment in comment_stream(r, 'all', limit=100):
        # strip out non-alphabet chars and replace with spaces
        thiscomment = comment.body.lower()
        thiscomment = re.sub("[^a-zA-Z]", " ", thiscomment)

        com_list.append(thiscomment)
        if len(com_list) >= comListLength:
            break

    return com_list
示例#5
0
    def run(self, *args, **kwargs):

        if not self.subreddit:
            raise ValueError('Self.subreddit must be set by configure()')

        try:
            for comment in comment_stream(self.client,
                                          self.subreddit,
                                          verbosity=0):
                self.func(comment, *args, **kwargs)
        except KeyboardInterrupt:
            log.info('Got a KeyboardInterrupt, running exit handler')
            self.exit_handler()
            sys.exit()
示例#6
0
def stream_strategy():
    from queue import Queue
    from threading import Thread
    from praw.helpers import submission_stream, comment_stream

    post_queue = Queue()

    threads = []
    threads.append(Thread(target=lambda: stream_handler(
        post_queue,
        comment_stream(
            r,
            "+".join(SUBREDDIT_LIST),
            limit=100,
            verbosity=0
        ),
        handle_comment
    )))
    threads.append(Thread(target=lambda: stream_handler(
        post_queue,
        submission_stream(
            r,
            "+".join(SUBREDDIT_LIST),
            limit=100,
            verbosity=0
        ),
        handle_submission
    )))

    for thread in threads:
        thread.daemon = True
        thread.start()

    while True:
        try:
            post_receiver(post_queue)
        except Exception as e:
            for thread in threads:
                if not thread.isAlive():
                    raise KeyboardInterrupt from e
            bot_tools.print_exception(e)
示例#7
0
        except APIException as e:
            print e
            if log_activity:
                cursor.execute(
                    "INSERT INTO errors (exception, target_id, subreddit, activity, time) VALUES (%s, %s, %s, %s, NOW())",
                    [str(e), comment.name, subreddit, "Adding comment"])

        if db:
            db.commit()


if False:
    comments = r.get_comments("test", limit=1000)
    handle_comments(comments)
else:
    while True:
        print "Main loop begins"
        try:
            comments = comment_stream(r, "all")
            #comments = comment_stream(r, "test")
            handle_comments(comments)
        except EnvironmentError as e:
            print e
        except OAuthInvalidToken as e:
            print e

if db:
    cursor.close()
    db.close()
示例#8
0
 def test_comment_stream(self):
     generator = helpers.comment_stream(self.r,
                                        "all",
                                        verbosity=self.verbosity)
     for i in range(10001):
         self.assertIsInstance(next(generator), objects.Comment)
示例#9
0
    db = sqlite3.connect('{}.db'.format(args.subreddit))
    log.info('Bot database opened/created.')

    stmt = 'SELECT name FROM sqlite_master WHERE type="table" AND name="comments"'
    q = db.execute(stmt).fetchall()
    if not q:
        log.info('Creating DB tables.')
        db.execute(
            'CREATE table comments (cid text, username text, timestamp date)')
        db.execute('CREATE table error (count integer)')

    log.info('Reading new comments from /r/{}.'.format(args.subreddit))
    subreddit = reddit.get_subreddit(args.subreddit)
    while True:
        try:
            for comment in comment_stream(reddit, subreddit):
                log.debug('read comment {} by {}'.format(
                    comment.id, comment.author))
                # log.debug('comment {}'.format(comment.__dict__))
                # log.debug('author {}'.format(comment.author.__dict__))
                count = db.execute('SELECT COUNT(*) FROM comments WHERE cid=?',
                                   (comment.id, )).fetchall()[0][0]
                if not count or count == 0:
                    log.info('logging comment {} by {}'.format(
                        comment.id, comment.author.name))
                    db.execute(
                        'INSERT into comments VALUES (?, ?, ?)',
                        (comment.id, comment.author.name, comment.created_utc))
                    db.commit()
        except Exception as e:
            log.error('Caught exception: {}'.format(e))
示例#10
0
with sqlite3.connect(database_filename) as comment_db:
    cursor = comment_db.cursor()
    if is_new_database:
        print("No prior database found! Creating schema...")
        cursor.execute("CREATE TABLE comments(comment_id TEXT PRIMARY KEY)")
        comment_db.commit()
    else:
        print("Comment database exists: assuming that schema exists...")

    log(str(datetime.datetime.utcnow()) + " forecastBot initialized.\n")

    # main bot engine
    while True:
        subreddit = reddit.get_subreddit(set_subreddit())
        for comment in comment_stream(reddit_session=reddit, subreddit=subreddit):
            print("Checking comment by " + str(comment.author) + ", id: " + comment.id + ": " + comment.body)
            # check if comment contains a call to action
            if contains_call(comment.body):
                # check to see if the comment has already been replied to
                cursor.execute('''SELECT * FROM comments WHERE comment_id=?''', (comment.id,))
                contains = cursor.fetchone()
                if contains is None:
                    # select command didn't return any rows matching comment id, so comment is unique (reply to it)
                    print("Called by comment #: " + comment.id + ": " + comment.body)
                    set_days_to_forecast(comment.body)
                    location = search_for_city_state(comment.body)
                    forecast = format_forecast(location, get_weather(location=location))
                    comment.reply(forecast)
                    print("Posted a comment:\n\n " + forecast)
                    cursor.execute("INSERT INTO comments(comment_id) VALUES (?)", (comment.id,))
示例#11
0
# Main Script
# Track commend ids that have already been processed successfully

# Load list of already processed comment ids
already_processed = loadProcessed()
print("%s - Starting with already processed: %s\n==========\n\n" % (datetime.now(), already_processed))

last = time.time()
count = 0
count_actual = 0
running = True
while running:
    try:
        # Read in comments from accessor and process them
        print("\n\t---\n\t%s - Generating fresh comment stream\n\t---\n\n" % datetime.now())
        comments = comment_stream(r, subreddit, limit=comment_stream_limit)
        for comment in comments:
            if (time.time() - last) > 600:
                print("\n\t---\n\t%s - %d processed comments, %d read\n" % (datetime.now(), count_actual, count))
                # Read in submissions and process
                # Check submissions
                print("\n\t---\n\t%s - Processing submissions \n\t---\n\n" % datetime.now())
                count_actual += checkSubmissions()
                print("\n\t---\n")

                last = time.time()

            if count > comment_stream_limit:
                print("#%d Read(%s): %s" % (count, comment.id, comment))

            count += 1
    reddit.login()
    log.info('Logged in.')

    db = sqlite3.connect('{}.db'.format(args.subreddit))
    log.info('Bot database opened/created.')

    stmt = 'SELECT name FROM sqlite_master WHERE type="table" AND name="comments"'
    q = db.execute(stmt).fetchall()
    if not q:
        log.info('Creating DB tables.')
        db.execute('CREATE table comments (cid text, username text, timestamp date)')
        db.execute('CREATE table error (count integer)')

    log.info('Reading new comments from /r/{}.'.format(args.subreddit))
    subreddit = reddit.get_subreddit(args.subreddit)
    while True:
        try:
            for comment in comment_stream(reddit, subreddit):
                log.debug('read comment {} by {}'.format(comment.id, comment.author))
                # log.debug('comment {}'.format(comment.__dict__))
                # log.debug('author {}'.format(comment.author.__dict__))
                count = db.execute('SELECT COUNT(*) FROM comments WHERE cid=?', (comment.id, )).fetchall()[0][0]
                if not count or count == 0:
                    log.info('logging comment {} by {}'.format(comment.id, comment.author.name))
                    db.execute('INSERT into comments VALUES (?, ?, ?)',
                               (comment.id, comment.author.name, comment.created_utc))
                    db.commit()
        except Exception as e:
            log.error('Caught exception: {}'.format(e))
            db.execute('UPDATE error set count = count + 1')
示例#13
0
def stream_comments():
    """streams new comments from r/soccer"""
    
    return (Comment(c) for c in comment_stream(r, "soccer"))
示例#14
0
# Main Script
# Track commend ids that have already been processed successfully

# Load list of already processed comment ids
already_processed = loadProcessed()
print("%s - Starting with already processed: %s\n==========\n\n" % (datetime.now(), already_processed))

last = time.time()
count = 0
count_actual = 0
running = True
while running:
  try:    
    # Read in comments from accessor and process them
    print ("\n\t---\n\t%s - Generating fresh comment stream\n\t---\n\n" % datetime.now())
    comments = comment_stream(r, subreddit, limit=comment_stream_limit)
    for comment in comments:
      if ((time.time() - last) > 600):
        print("\n\t---\n\t%s - %d processed comments, %d read\n" % (datetime.now(), count_actual, count))
        # Read in submissions and process
        # Check submissions
        print ("\n\t---\n\t%s - Processing submissions \n\t---\n\n" % datetime.now())
        count_actual += checkSubmissions()
        print ("\n\t---\n")

        last = time.time()
      
      if (count > comment_stream_limit):
        print("#%d Read(%s): %s" % (count, comment.id, comment))

      count += 1
示例#15
0
import praw
import time
from praw.helpers import comment_stream

r = praw.Reddit("Just a regular bot.")
r.login()

target_text = "aurigabot"
response_text = "Auriga is here!! Do you want to message my creater? [Click right here!](http://reddit.com/u/smarttechadvice)"

processed = []
while True:
    for c in comment_stream(r, 'pcmasterrace'):
        if target_text in c.body and c.id not in processed:
            c.reply(response_text)
            processed.append(c.id)
示例#16
0
'''


import praw
from praw.helpers import comment_stream
from HaikuGenerator import generate_haiku

user_agent = "User-Agent: HaikuGen 0.2 (by /u/FreshHaikuBot)"

r = praw.Reddit(user_agent=user_agent)

r.login('FreshHaikuBot', '[password here]', disable_warning=True)
subreddit = 'all'

while True:
    for comment in comment_stream(r, subreddit, limit=100):
        # if comment.body == "FreshHaikuBot! Write me a haiku.":
        if "FreshHaikuBot! Write me a haiku." in comment.body:
            # print to console to know its working
            print('\n*****************************')
            print('request received. Working...')
            print('*****************************\n')

            # get a haiku
            haiku = generate_haiku()

            # send the haiku to the requester and print it to console
            comment.reply(haiku)
            print(haiku)
示例#17
0
    except APIException as e:
      print e
      if log_activity:
        cursor.execute(
          "INSERT INTO errors (exception, target_id, subreddit, activity, time) VALUES (%s, %s, %s, %s, NOW())",
          [str(e), comment.name, subreddit, "Adding comment"])

    if db:
      db.commit()


if False:
  comments = r.get_comments("test", limit=1000)
  handle_comments(comments)
else:
  while True:
    print "Main loop begins"
    try:
      comments = comment_stream(r, "all")
      #comments = comment_stream(r, "test")
      handle_comments(comments)
    except EnvironmentError as e:
      print e
    except OAuthInvalidToken as e:
      print e

if db:
  cursor.close()
  db.close()
示例#18
0
import praw
from praw.helpers import comment_stream

r = praw.Reddit("luisbravo1 test")
r.login()

target_text = "Hello"
response_text = "Welcome to our subreddit!"

processed = []
while True:
    for c in comment_stream(r, 'BotTestBravo'):
        if target_text in c.body and c.id not in processed:
            c.reply(response_text)
            processed.append(c.id)

示例#19
0
try:
    with open(save_file, 'rb') as handle:
        processed = pickle.load(handle)
except EOFError:
    print("Empty save file.")


def process_comment(c):
    text = c.body.lower()
    if re.search(r'\b(gg\s*ez)\b', text):
        print(c.body)
        response = random.choice(RESPONSES)
        print("\n" + response + "\n---\n")
        c.reply(response + FOOTER)


def dump_ids():
    with open(save_file, 'wb') as handle:
        pickle.dump(processed, handle)


while True:
    try:
        for comment in comment_stream(r, 'overwatch'):
            if comment.id not in processed:
                processed.add(comment.id)
                process_comment(comment)
                dump_ids()
    except:
        traceback.print_exc()
示例#20
0
 def test_comment_stream(self):
     generator = helpers.comment_stream(self.r, "all",
                                        verbosity=self.verbosity)
     for i in range(10001):
         self.assertIsInstance(next(generator), objects.Comment)