def print_last(self): now = timestamp_hour(datetime.today()) since = now - timedelta(days=30) stats = yield find_stats({'user': self.user, 'timestamp': {'$gte': since}}, filter=sortdesc('timestamp')) if not len(stats): returnValue() stat = stats[0] stat["followers"] = yield count_followers(self.user) rts = 0 fols = 0 twts = 0 delays = {1: 'hour', 6: '6 hours', 24: 'day', 7*24: 'week', 30*24: 'month'} order = delays.keys() order.sort() olds = {'tweets': {}, 'followers': {}, 'rts': {}} for s in stats: d = now - s['timestamp'] delay = d.seconds / 3600 + d.days * 24 fols = stat['followers'] - s['followers'] twts = stat['tweets'] - s['tweets'] for i in order: if delay == i: if 'stats%sH' % i not in olds['tweets']: olds['tweets']['stats%sH' % i] = twts if twts not in olds['tweets'].values() else 0 if 'stats%sH' % i not in olds['followers']: olds['followers']['stats%sH' % i] = fols if fols not in olds['followers'].values() else 0 if 'stats%sH' % i not in olds['rts']: olds['rts']['stats%sH' % i] = rts if rts not in olds['rts'].values() else 0 rts += s['rts_last_hour'] olds['rts']['stats1H'] = stat['rts_last_hour'] for i in order: if rts and 'stats%sH' % i not in olds['rts'] and rts not in olds['rts'].values(): olds['rts']['stats%sH' % i] = rts rts = 0 if fols and 'stats%sH' % i not in olds['followers'] and fols not in olds['followers'].values(): olds['followers']['stats%sH' % i] = fols fols = 0 if twts and 'stats%sH' % i not in olds['tweets'] and twts not in olds['tweets'].values(): olds['tweets']['stats%sH' % i] = twts twts = 0 res = [] if stat['tweets']: res.append("Tweets: %d total" % stat['tweets'] + " ; ".join([""]+["%d last %s" % (olds['tweets']['stats%sH' % i], delays[i]) for i in order if 'stats%sH' % i in olds['tweets'] and olds['tweets']['stats%sH' % i]])) textrts = ["%d last %s" % (olds['rts']['stats%sH' % i], delays[i]) for i in order if 'stats%sH' % i in olds['rts'] and olds['rts']['stats%sH' % i]] if textrts: res.append("RTs: " + " ; ".join(textrts)) if stat['followers']: res.append("Followers: %d total" % stat['followers'] + " ; ".join([""]+["%+d last %s" % (olds['followers']['stats%sH' % i], delays[i]) for i in order if 'stats%sH' % i in olds['followers'] and olds['followers']['stats%sH' % i]])) recent = yield find_last_followers(self.user) if recent: res.append("Recent follower%s: %s" % ("s include" if len(recent) > 1 else "", format_4_followers(recent))) if self.public_url and res: res.append("More details: %sstatic_stats_%s.html" % (self.public_url, self.user)) returnValue([(True, "[Stats] %s" % m) for m in res])
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)