def process_stats(self, res, user): if not res: return None stats, last, timestamp = res if not stats: return None if not last: last = {'tweets': 0, 'followers': 0} since = timestamp - timedelta(hours=1) else: since = last['timestamp'] if 'lists' not in last: last['lists'] = 0 re_match_rts = re.compile(u'(([MLR]T|%s|♺)\s*)+@?%s' % (QUOTE_CHARS, user), re.I) rts = self.db['tweets'].find({'channel': self.fact.channel, 'message': re_match_rts, 'timestamp': {'$gte': since}}, snapshot=True, fields=['_id']) nb_rts = rts.count() if rts.count() else 0 if config.TWITTER_API_VERSION == 1: stat = {'user': user, 'timestamp': timestamp, 'tweets': stats.get('updates', last['tweets']), 'followers': stats.get('followers', last['followers']), 'rts_last_hour': nb_rts} else: stat = {'user': user, 'timestamp': timestamp, 'tweets': stats.get('statuses_count', last['tweets']), 'followers': stats.get('followers_count', last['followers']), 'rts_last_hour': nb_rts, 'lists': stats.get('listed_count', last['lists'])} self.db['stats'].insert(stat) weekday = timestamp.weekday() laststats = Stats(self.db, user) if (timestamp.hour == 13 and weekday < 5) or timestamp.hour == 18: self.fact.ircclient._send_message(laststats.print_last(), self.fact.channel) last_tweet = self.db['tweets'].find_one({'channel': self.fact.channel, 'user': user}, fields=['date'], sort=[('timestamp', pymongo.DESCENDING)]) if last_tweet and timestamp - last_tweet['date'] > timedelta(days=3) and (timestamp.hour == 11 or timestamp.hour == 17) and weekday < 5: reactor.callFromThread(reactor.callLater, 3, self.fact.ircclient._send_message, "[FYI] No tweet was sent since %s days." % (timestamp - last_tweet['date']).days, self.fact.channel) reactor.callFromThread(reactor.callLater, 1, laststats.dump_data) return None
def process_stats(self, res, user): if not res: returnD(False) stats, last, timestamp = res if not stats: returnD(False) if not last: last = {'tweets': 0, 'followers': 0} since = timestamp - timedelta(hours=1) else: since = last['timestamp'] if 'lists' not in last: last['lists'] = 0 re_match_rts = re.compile(u'(([MLR]T|%s|♺)\s*)+@?%s' % (QUOTE_CHARS, user), re.I) rts = yield Mongo('tweets', 'find', {'channel': self.fact.channel, 'message': re_match_rts, 'timestamp': {'$gte': since}}, fields=['_id']) nb_rts = len(rts) if config.TWITTER_API_VERSION == 1: stat = {'user': user, 'timestamp': timestamp, 'tweets': stats.get('updates', last['tweets']), 'followers': stats.get('followers', last['followers']), 'rts_last_hour': nb_rts} else: stat = {'user': user, 'timestamp': timestamp, 'tweets': stats.get('statuses_count', last['tweets']), 'followers': stats.get('followers_count', last['followers']), 'rts_last_hour': nb_rts, 'lists': stats.get('listed_count', last['lists'])} yield Mongo('stats', 'insert', stat) weekday = timestamp.weekday() laststats = Stats(user) if chan_displays_stats(self.fact.channel) and ((timestamp.hour == 13 and weekday < 5) or timestamp.hour == 18): stats = yield laststats.print_last() self.fact.ircclient._send_message(stats, self.fact.channel) last_tweet = yield Mongo('tweets', 'find', {'channel': self.fact.channel, 'user': user}, fields=['date'], limit=1, filter=sortdesc('timestamp')) if chan_displays_stats(self.fact.channel) and last_tweet and timestamp - last_tweet[0]['date'] > timedelta(days=3) and (timestamp.hour == 11 or timestamp.hour == 17) and weekday < 5: reactor.callFromThread(reactor.callLater, 3, self.fact.ircclient._send_message, "[FYI] No tweet was sent since %s days." % (timestamp - last_tweet[0]['date']).days, self.fact.channel) reactor.callFromThread(reactor.callLater, 1, laststats.dump_data) returnD(True)
def command_stats(self, rest, channel=None, nick=None): """stats : Prints stats on the Twitter account set for the channel./TWITTER""" channel = self.getMasterChan(channel) conf = chanconf(channel) if conf and "TWITTER" in conf and "USER" in conf["TWITTER"]: stats = Stats(self.db, conf["TWITTER"]["USER"].lower()) return stats.print_last() return "No Twitter account set for this channel."
def process_stats(self, stats, user): # Update followers list conf = chanconf(self.fact.channel) conn = Microblog('twitter', conf, bearer_token=conf["oauth2"]) lost = yield conn.update_followers(self.fact.db) ct = len(lost) if ct: self.fact.ircclient._send_message('[twitter] Lost %s follower%s: %s%s' % (ct, "s" if ct>1 else "", format_4_followers(lost), "…" if ct>4 else ""), self.fact.channel) # Update stats if not stats: returnD(False) stats, last, timestamp = stats if not stats or type(stats) is str: returnD(False) if not last: last = {'tweets': 0, 'followers': 0} since = timestamp - timedelta(hours=1) else: since = last['timestamp'] if 'lists' not in last: last['lists'] = 0 re_match_rts = re.compile(u'(([MLR]T|%s|♺)\s*)+@?%s' % (QUOTE_CHARS, user), re.I) rts = yield self.fact.db['tweets'].find({'channel': self.fact.channel, 'message': re_match_rts, 'timestamp': {'$gte': since}}, fields=['_id']) nb_rts = len(rts) nb_fols = yield count_followers(user) stat = {'user': user, 'timestamp': timestamp, 'tweets': stats.get('statuses_count', last['tweets']), 'followers': nb_fols, 'rts_last_hour': nb_rts, 'lists': stats.get('listed_count', last['lists'])} yield self.fact.db['stats'].insert(stat) weekday = timestamp.weekday() laststats = Stats(user) if chan_displays_stats(self.fact.channel) and ((timestamp.hour == 13 and weekday < 5) or timestamp.hour == 18): stats = yield laststats.print_last() self.fact.ircclient._send_message(stats, self.fact.channel) last_tweet = yield self.fact.db['tweets'].find({'channel': self.fact.channel, 'user': user}, fields=['date'], limit=1, filter=sortdesc('timestamp')) if chan_displays_stats(self.fact.channel) and last_tweet and timestamp - last_tweet[0]['date'] > timedelta(days=3) and (timestamp.hour == 11 or timestamp.hour == 17) and weekday < 5: reactor.callFromThread(reactor.callLater, 3, self.fact.ircclient._send_message, "[FYI] No tweet was sent since %s days." % (timestamp - last_tweet[0]['date']).days, self.fact.channel) reactor.callFromThread(reactor.callLater, 1, laststats.dump_data) returnD(True)