def check_messages(): ''' Checks for new messages, handles incoming messages Returns: true: New messages altered the database (added/removed filters) false: No messages, or the messages were innocuous ''' has_new_messages = False last_pm_time = Bot.db.get_config('last_pm_time') if last_pm_time == None: last_pm_time = 0 else: last_pm_time = int(float(last_pm_time)) for msg in Reddit.get('/message/inbox'): # TODO unread if msg.created <= last_pm_time or (type(msg) == Message and not msg.new): continue try: response = Filter.parse_pm(msg, Bot.db) while response.endswith('\n'): response = response[:-1] Bot.log('Bot.check_messages: Replying to %s with: %s' % (msg.author, response)) msg.reply(response) has_new_messages = True except Exception, e: # No need to reply Bot.log('Bot.check_messages: %s' % str(e)) last_pm_time = int(float(msg.created)) Bot.db.set_config('last_pm_time', str(last_pm_time)) msg.mark_as_read()
def get_content(url, pages=1): ''' Retrieves and iterates the posts/comments found at 'url' Args: url: URL on reddit containing posts/comments pages: Number of pages to view (loads 'next' page for pages-1 times) Yields: Each post or comment found at 'url' ''' page = 0 #Bot.log('Loading %s' % url) posts = Reddit.get(url) latency = int(strftime('%s', gmtime())) while True: page += 1 for post in posts: yield post if page < pages: #Bot.log('Loading %s (page %d)' % (url, page + 1)) posts = Reddit.next() else: break latency = int(strftime('%s', gmtime())) - latency