def _check_inbox(self): """ Evaluate new messages in inbox """ lg.debug("> _check_inbox()") try: # Try to fetch some messages messages = ctb_misc._praw_call(self._redditcon.get_unread, limit=self._REDDIT_BATCH_LIMIT) # Process messages for m in messages: lg.info("_check_inbox(): %s from %s", "comment" if m.was_comment else "message", m.author.name) # Ignore self messages if bool(m.author) and m.author.name.lower() == self._config['reddit']['user'].lower(): lg.debug("_check_inbox(): ignoring message from self") ctb_misc._praw_call(m.mark_as_read) continue # Ignore messages from banned users if bool(m.author) and self._config['reddit'].has_key('banned_users'): lg.debug("_check_inbox(): checking whether user '%s' is banned..." % m.author) u = ctb_user.CtbUser(name = m.author.name, redditobj = m.author, ctb = self) if u._IS_BANNED: lg.info("_check_inbox(): ignoring banned user '%s'" % m.author) ctb_misc._praw_call(m.mark_as_read) continue action = None if m.was_comment: # Attempt to evaluate as comment / mention action = ctb_action._eval_comment(m, self) else: # Attempt to evaluate as inbox message action = ctb_action._eval_message(m, self) # Perform action, if found if bool(action): lg.info("_check_inbox(): %s from %s (m.id %s)", action._TYPE, action._FROM_USER._NAME, str(m.id)) action.do() else: lg.info("_check_inbox(): no match") if self._config['reddit']['messages']['sorry']: user = ctb_user.CtbUser(name=m.author.name, redditobj=m.author, ctb=self) tpl = self._jenv.get_template('didnt-understand.tpl') msg = tpl.render(user_from=user._NAME, what='comment' if m.was_comment else 'message', source_link=m.permalink if hasattr(m, 'permalink') else None, ctb=self) lg.debug("_check_inbox(): %s", msg) user.tell(subj='What?', msg=msg, msgobj=m if not m.was_comment else None) # Mark message as read ctb_misc._praw_call(m.mark_as_read) except Exception as e: lg.error("_check_inbox(): %s", str(e)) raise lg.debug("< check_inbox() DONE") return True
def _check_inbox(self): """ Evaluate new messages in inbox """ lg.debug("> _check_inbox()") while True: try: # Try to fetch some messages messages = self._redditcon.get_unread(limit=self._REDDIT_BATCH_LIMIT) # Process messages for m in messages: lg.info("_check_inbox(): %s from %s", "comment" if m.was_comment else "message", m.author.name) # Ignore self messages if bool(m.author) and m.author.name.lower() == self._config['reddit']['user'].lower(): lg.debug("_check_inbox(): ignoring message from self") m.mark_as_read() continue action = None if m.was_comment: # Attempt to evaluate as comment / mention action = ctb_action._eval_comment(m, self) m.upvote() else: # Attempt to evaluate as inbox message action = ctb_action._eval_message(m, self) # Perform action, if found if bool(action): lg.info("_check_inbox(): %s from %s (m.id %s)", action._TYPE, action._FROM_USER._NAME, str(m.id)) action.do() else: lg.info("_check_inbox(): no match") # Mark message as read m.mark_as_read() break except (HTTPError, RateLimitExceeded) as e: lg.warning("_check_inbox(): Reddit is down (%s), sleeping...", str(e)) time.sleep(self._DEFAULT_SLEEP_TIME) pass except timeout: lg.warning("_check_inbox(): Reddit is down (timeout), sleeping...") time.sleep(self._DEFAULT_SLEEP_TIME) pass except Exception as e: lg.error("_check_inbox(): %s", str(e)) raise lg.debug("< check_inbox() DONE") return True
def _check_subreddits(self): """ Evaluate new comments from configured subreddits """ lg.debug("> _check_subreddits()") try: # Process comments until old comment reached # Get last_processed_comment_time if necessary if self._last_processed_comment_time <= 0: self._last_processed_comment_time = ctb_misc._get_value(conn=self._mysqlcon, param0='last_processed_comment_time') _updated_last_processed_time = 0 # Fetch comments from subreddits my_comments = ctb_misc._praw_call(self._subreddits.get_comments, limit=self._REDDIT_BATCH_LIMIT) # Match each comment against regex counter = 0 for c in my_comments: # Stop processing if old comment reached #lg.debug("_check_subreddits(): c.id %s from %s, %s <= %s", c.id, c.subreddit.display_name, c.created_utc, self._last_processed_comment_time) if c.created_utc <= self._last_processed_comment_time: lg.debug("_check_subreddits(): old comment reached") break counter += 1 if c.created_utc > _updated_last_processed_time: _updated_last_processed_time = c.created_utc # Ignore comments from banned users if bool(c.author) and self._config['reddit'].has_key('banned_users'): lg.debug("_check_subreddits(): checking whether user '%s' is banned..." % c.author) u = ctb_user.CtbUser(name = c.author.name, redditobj = c.author, ctb = self) if u._IS_BANNED: lg.info("_check_subreddits(): ignoring banned user '%s'" % c.author) continue # Attempt to evaluate comment action = ctb_action._eval_comment(c, self) # Perform action, if found if bool(action): lg.info("_check_subreddits(): %s from %s (m.id %s)", action._TYPE, action._FROM_USER._NAME, str(c.id)) action.do() else: lg.info("_check_subreddits(): no match") lg.debug("_check_subreddits(): %s comments processed", counter) if counter >= self._REDDIT_BATCH_LIMIT - 1: lg.warning("_check_subreddits(): _REDDIT_BATCH_LIMIT (%s) was not large enough to process all comments", self._REDDIT_BATCH_LIMIT) except Exception as e: lg.error("_check_subreddits(): coudln't fetch comments: %s", str(e)) raise # Save updated last_processed_time value if _updated_last_processed_time > 0: self._last_processed_comment_time = _updated_last_processed_time ctb_misc._set_value(conn=self._mysqlcon, param0='last_processed_comment_time', value0=self._last_processed_comment_time) lg.debug("< _check_subreddits() DONE") return True
def _check_subreddits(self): """ Evaluate new comments from configured subreddits """ lg.debug("> _check_subreddits()") try: # Process comments until old comment reached # Get last_processed_comment_time if necessary if self._last_processed_comment_time <= 0: self._last_processed_comment_time = ctb_misc._get_value(conn=self._mysqlcon, param0='last_processed_comment_time') _updated_last_processed_time = 0 # Fetch comments from subreddits my_comments = self._subreddits.get_comments(limit=self._REDDIT_BATCH_LIMIT) # Match each comment against regex counter = 0 for c in my_comments: # Stop processing if old comment reached #lg.debug("_check_subreddits(): c.id %s from %s, %s <= %s", c.id, c.subreddit.display_name, c.created_utc, self._last_processed_comment_time) if c.created_utc <= self._last_processed_comment_time: lg.debug("_check_subreddits(): old comment reached") break counter += 1 if c.created_utc > _updated_last_processed_time: _updated_last_processed_time = c.created_utc # Attempt to evaluate comment action = ctb_action._eval_comment(c, self) # Perform action if necessary if action != None: lg.info("_check_subreddits(): %s (%.6g %s) from %s (c.id %s)", action._TYPE, action._COIN_VAL, action._COIN, action._FROM_USER._NAME, str(c.id)) action.do() lg.debug("_check_subreddits(): %s comments processed", counter) if counter >= self._REDDIT_BATCH_LIMIT - 1: lg.warning("_check_subreddits(): _REDDIT_BATCH_LIMIT (%s) was not large enough to process all comments", self._REDDIT_BATCH_LIMIT) except (HTTPError, RateLimitExceeded) as e: lg.warning("_check_subreddits(): Reddit is down (%s), sleeping...", str(e)) time.sleep(self._DEFAULT_SLEEP_TIME) pass except timeout: lg.warning("_check_subreddits(): Reddit is down (timeout), sleeping...") time.sleep(self._DEFAULT_SLEEP_TIME) pass except Exception as e: lg.error("_check_subreddits(): coudln't fetch comments: %s", str(e)) raise # Save updated last_processed_time value if _updated_last_processed_time > 0: self._last_processed_comment_time = _updated_last_processed_time ctb_misc._set_value(conn=self._mysqlcon, param0='last_processed_comment_time', value0=self._last_processed_comment_time) lg.debug("< _check_subreddits() DONE") return True