示例#1
0
def stat(bot, update):
    chat_id = update.message.chat_id
    chat_type = update.message.chat.type
    chat_title = update.message.chat.title

    # Anti-spam
    last_call = cache.get('last_{}'.format(chat_id))

    # First request
    if not last_call:
        last_call = int(time.time()) - 5
        cache.set('last_{}'.format(chat_id), last_call)

    # If last request was over 5 seconds ago
    if (int(time.time()) - last_call) >= 5:
        if chat_type == 'group' or chat_type == 'supergroup':
            # Get stats for group
            info = Stats.get_for_chat(chat_id)

            if info:
                # Get msg text for /stat
                msg = Stats.stat_format(chat_id, info['msg_count'],
                                        info['current_users'],
                                        info['top_users'], chat_title)
                update.message.reply_text(msg, parse_mode=ParseMode.MARKDOWN)

                # Update last call
                cache.set('last_{}'.format(chat_id), int(time.time()))
                logger.info('Group {} requested stats'.format(chat_id))
示例#2
0
    def send(self):
        cids_list = self.stack
        all_messages = 0

        # WTF???
        # TODO: fix this
        counter = {x['cid']: {'msgs': 0, 'usrs': 0} for x in cids_list}
        for cid in counter.keys():
            counter[cid]['msg'] = sum([a['msg_count'] for a in cids_list if a['cid'] == cid])
            counter[cid]['usr'] = sum([a['users_count'] for a in cids_list if a['cid'] == cid])

        for x in counter.items():
            cid = x[0]
            msg_count = x[1]['msg']
            users_count = x[1]['usr']
            all_messages += msg_count

            ChatStat().add(cid,
                           users_count,
                           msg_count,
                           int(time.time()))

        # Statistics for admin panel
        c = cache.get('today_messages')

        if c:
            cache.incr('today_messages', all_messages)
        else:
            cache.set('today_messages', all_messages, 86400)

        cids_list.clear()
示例#3
0
    def get(uid, cid, db):
        cached = cache.get('ustat_{}_{}'.format(uid, cid))

        if cached:
            return cached
        else:
            q = db.query(UserStat) \
                .filter(UserStat.cid == cid,
                        UserStat.uid == uid) \
                .limit(1) \
                .all()
            if q:
                cache.set('ustat_{}_{}'.format(uid, cid), q[0])
                return q[0]
            else:
                return False
示例#4
0
    def get(cid, db):
        cached = cache.get('cstat_{}'.format(cid))

        if cached:
            return cached
        else:
            q = db.query(ChatStat) \
                .filter(ChatStat.cid == cid) \
                .order_by(ChatStat.id.desc()) \
                .limit(1) \
                .all()
            if q:
                cache.set('cstat_{}'.format(cid), q[0])
                return q[0]
            else:
                return False
示例#5
0
    def get(uid, cid, db):
        cached = cache.get('ustat_{}_{}'.format(uid, cid))

        if cached:
            return cached
        else:
            q = db.query(UserStat) \
                .filter(UserStat.cid == cid,
                        UserStat.uid == uid) \
                .limit(1) \
                .all()
            if q:
                cache.set('ustat_{}_{}'.format(uid, cid), q[0])
                return q[0]
            else:
                return False
示例#6
0
    def get(cid, db):
        cached = cache.get('cstat_{}'.format(cid))

        if cached:
            return cached
        else:
            q = db.query(ChatStat) \
                .filter(ChatStat.cid == cid) \
                .order_by(ChatStat.id.desc()) \
                .limit(1) \
                .all()
            if q:
                cache.set('cstat_{}'.format(cid), q[0])
                return q[0]
            else:
                return False
示例#7
0
    def get(uid, db):
        cached = cache.get('user_{}'.format(uid))

        if cached:
            return cached
        else:
            q = db.query(User) \
                .filter(User.uid == uid) \
                .limit(1) \
                .all()
            if q:
                cache.set('user_{}'.format(uid), q[0])
                cache.delete('web_user_{}'.format(uid))
                return q[0]
            else:
                return False
示例#8
0
    def add(self, cid, title, db, public_link=''):
        chat = self.get(cid)
        add_time = round(time())

        if chat:
            if chat.title != title:
                self.update(cid, {'title': title})

            if chat.public_link != public_link:
                self.update(cid, {'public_link': public_link})
        else:
            # Statistics for admin panel
            c = cache.get('today_chats')

            if c:
                cache.incr('today_chats')
            else:
                cache.set('today_chats', 1, 86400)

            # Add new chat to database
            db.add(
                Chat(cid=cid,
                     title=title,
                     public_link=public_link,
                     add_time=add_time,
                     chat_hash=self.generate_hash(cid)))

        # Add new/updated chat to memcache
        cache.set(
            'chat_{}'.format(cid),
            Chat(cid=cid,
                 title=title,
                 public_link=public_link,
                 add_time=add_time,
                 chat_hash=self.generate_hash(cid)))

        cache.delete('web_chat_{}'.format(cid))
        db.commit()
示例#9
0
    def add(self, cid, title, db, public_link=''):
        chat = self.get(cid)
        add_time = round(time())

        if chat:
            if chat.title != title:
                self.update(cid, {'title': title})

            if chat.public_link != public_link:
                self.update(cid, {'public_link': public_link})
        else:
            # Statistics for admin panel
            c = cache.get('today_chats')

            if c:
                cache.incr('today_chats')
            else:
                cache.set('today_chats', 1, 86400)

            # Add new chat to database
            db.add(Chat(cid=cid,
                        title=title,
                        public_link=public_link,
                        add_time=add_time,
                        chat_hash=self.generate_hash(cid)))

        # Add new/updated chat to memcache
        cache.set('chat_{}'.format(cid),
                  Chat(cid=cid,
                       title=title,
                       public_link=public_link,
                       add_time=add_time,
                       chat_hash=self.generate_hash(cid)))

        cache.delete('web_chat_{}'.format(cid))
        db.commit()