Example #1
0
def sync_order():
    global modian_handler
    my_logger.info('同步订单')
    for modian in global_config.MODIAN_ARRAY:
        pro_id = modian.group_account_id
        orders = modian_handler.get_all_orders(modian)
        for order in orders:
            user_id = order['user_id']
            nickname = order['nickname']
            pay_time = order['pay_time']
            backer_money = order['backer_money']

            oid = uuid.uuid3(uuid.NAMESPACE_OID, str(user_id) + pay_time)
            # print('oid: %s', oid)

            rst = mysql_util.select_one(
                """
                    select * from `order` where id='%s'
                """, (str(oid), ))
            if len(rst) == 0:
                my_logger.info('该订单不在数据库中')
                # 每次需要更新一下昵称
                mysql_util.query("""
                                        INSERT INTO `supporter` (`id`, `name`) VALUES (%s, '%s')  ON DUPLICATE KEY
                                            UPDATE `name`='%s'
                                        """ % (user_id, nickname, nickname))
                mysql_util.query("""
                        INSERT INTO `order` (`id`, `supporter_id`, `backer_money`, `pay_time`, `pro_id`) VALUES 
                            ('%s', %s, %s, '%s', %s);
                    """ % (oid, user_id, backer_money, pay_time, pro_id))
                msg = '【机器人补播报】感谢 %s 支持了%s元, %s\n' % (
                    nickname, backer_money,
                    util.random_str(global_config.MODIAN_POSTSCRIPTS))
                # QQHandler.send_to_groups(['483548995'], msg)
                modian_handler.order_queues[modian.group_account_id].add(oid)
    def get_member_room_msg_lite(self):
        """
        发送成员房间消息(简易版,只提醒在房间里出现)
        :return:
        """
        time_now = time.time()
        msg = ''

        if self.unread_other_member_msg_amount > 0 and len(
                self.member_room_msg_lite_groups) > 0:
            if self.last_other_member_msg_time < 0 or time_now - self.last_other_member_msg_time >= 10 * 60:
                DEBUG('其他成员出现在房间中')
                member_name = ', '.join(self.other_members_names)
                QQHandler.send_to_groups(self.member_room_msg_lite_groups,
                                         '%s来你们灰的房间里串门啦~' % member_name)
                self.unread_other_member_msg_amount = 0
            self.last_other_member_msg_time = time_now
        if self.unread_msg_amount > 0 and len(
                self.member_room_msg_lite_groups) > 0:
            # 距离上一次提醒时间超过10分钟且有未读消息
            if self.last_msg_time < 0 or time_now - self.last_msg_time >= 10 * 60:
                DEBUG('向大群发送简易版提醒')
                msg = util.random_str(global_config.ROOM_MSG_LITE_NOTIFY)
                QQHandler.send_to_groups(self.member_room_msg_lite_groups, msg)
                INFO(msg)
                self.unread_msg_amount = 0
            else:
                DEBUG('不向大群发送简易版提醒')
            self.last_msg_time = time_now
        else:
            INFO('最近10分钟内没有未读消息')
 def set_password(self, new_password):
     salt = random_str()
     password_md5 = md5(new_password.encode('utf-8')).hexdigest()
     password_final = md5((password_md5 + salt).encode('utf-8')).hexdigest()
     self.salt = salt
     self.password = password_final
     self.save()
Example #4
0
 def set_password(self, new_password):
     salt = random_str()
     password_md5 = md5(new_password.encode('utf-8')).hexdigest()
     password_final = md5((password_md5 + salt).encode('utf-8')).hexdigest()
     self.salt = salt
     self.password = password_final
     self.save()
Example #5
0
 def refresh_key(self):
     session = DBSession()
     self.key = random_str(32)
     self.key_time = int(time.time())
     session.add(self)
     session.commit()
     session.close()
 def refresh_key(self):
     session = DBSession()
     self.key = random_str(32)
     self.key_time = int(time.time())
     session.add(self)
     session.commit()
     session.close()
 def new(username, nickname, password):
     salt = random_str()
     password_md5 = md5(password.encode('utf-8')).hexdigest()
     password_final = md5((password_md5 + salt).encode('utf-8')).hexdigest()
     level = USER_LEVEL.NORMAL  # 首个用户赋予admin权限
     the_time = int(time.time())
     ret = User.create(
         username=username,
         nickname=nickname,
         password=password_final,
         salt=salt,
         level=level,
         key=random_str(32),
         key_time=the_time,
     )
     ret.save()
     return ret
Example #8
0
 def new(username, nickname, password):
     salt = random_str()
     password_md5 = md5(password.encode('utf-8')).hexdigest()
     password_final = md5((password_md5 + salt).encode('utf-8')).hexdigest()
     level = USER_LEVEL.NORMAL  # 首个用户赋予admin权限
     the_time = int(time.time())
     ret = User.create(username=username, nickname=nickname, password=password_final, salt=salt, level=level, key=random_str(32), key_time = the_time, )
     ret.save()
     return ret
 def set_password(self, new_password):
     salt = random_str()
     password_md5 = md5(new_password.encode('utf-8')).hexdigest()
     password_final = md5((password_md5 + salt).encode('utf-8')).hexdigest()
     session = DBSession()
     self.salt = salt
     self.password = password_final
     session.add(self)
     session.commit()
     session.close()
Example #10
0
 def set_password(self, new_password):
     salt = random_str()
     password_md5 = md5(new_password.encode('utf-8')).hexdigest()
     password_final = md5((password_md5 + salt).encode('utf-8')).hexdigest()
     session = DBSession()
     self.salt = salt
     self.password = password_final
     session.add(self)
     session.commit()
     session.close()
Example #11
0
 def new(cls, username, password):
     salt = random_str()
     password_md5 = md5(password.encode('utf-8')).hexdigest()
     password_final = md5((password_md5 + salt).encode('utf-8')).hexdigest()
     level = USER_LEVEL.ADMIN if cls.count(
     ) == 0 else USER_LEVEL.NORMAL  # 首个用户赋予admin权限
     the_time = int(time.time())
     session = DBSession()
     ret = User(
         username=username,
         password=password_final,
         openid=openid,
         salt=salt,
         level=level,
         key=random_str(32),
         key_time=the_time,
     )
     session.add(ret)
     session.commit()
     session.close()
     return ret
Example #12
0
 def new(cls, username, password):
     salt = random_str()
     password_md5 = md5(password.encode('utf-8')).hexdigest()
     password_final = md5((password_md5 + salt).encode('utf-8')).hexdigest()
     level = USER_LEVEL.ADMIN if cls.count() == 0 else USER_LEVEL.NORMAL  # 首个用户赋予admin权限
     the_time = int(time.time())
     session = DBSession()
     ret = User(username=username, password=password_final,openid=openid , salt=salt, level=level, key=random_str(32),
                       key_time = the_time, )
     session.add(ret)
     session.commit()
     session.close()
     return ret
Example #13
0
    def get_member_room_msg_lite(self, task):
        """
        发送成员房间消息提醒(简易版,只提醒在房间里出现)
        :param task:
        :return:
        """
        time_now = time.time()
        msg = ''
        logger.debug('timenow: %s', time_now)
        logger.debug('unread_other_member_msg_amount=%s',
                     task.unread_other_member_msg_amount)
        logger.debug('last_other_member_msg_time: %s',
                     task.last_other_member_msg_time)
        logger.debug('time_now - self.last_other_member_msg_time: %s',
                     time_now - task.last_other_member_msg_time)

        if task.unread_other_member_msg_amount > 0 and len(
                task.member_room_msg_lite_groups) > 0:
            if task.last_other_member_msg_time < 0 or time_now - task.last_other_member_msg_time >= 10 * 60:
                logger.debug('其他成员出现在房间中')
                member_name = ', '.join(task.other_members_names)
                # QQHandler.send_to_groups(task.member_room_msg_lite_groups, '%s来你们灰的房间里串门啦~' % member_name)
            task.unread_other_member_msg_amount = 0
            task.last_other_member_msg_time = time_now
            task.other_members_names.clear()

        logger.debug('unread_msg_amount=%s', task.unread_msg_amount)
        logger.debug('last_msg_time: %s', task.last_msg_time)
        logger.debug('time_now - self.last_msg_time: %s',
                     time_now - task.last_msg_time)

        if task.unread_msg_amount > 0 and len(
                task.member_room_msg_lite_groups) > 0:
            # 距离上一次提醒时间超过10分钟且有未读消息
            if task.last_msg_time < 0 or time_now - task.last_msg_time >= 10 * 60:
                logger.debug('向大群发送简易版提醒')
                msg = util.random_str(global_config.ROOM_MSG_LITE_NOTIFY)
                if global_config.USING_COOLQ_PRO:
                    msg += '[CQ:image,file=http://wx3.sinaimg.cn/large/789c06f9gy1fq4dl21j0rj20k00k0jsl.jpg]'
                logger.debug(task.member_room_msg_lite_groups)
                # QQHandler.send_to_groups(task.member_room_msg_lite_groups, msg)
                logger.info(msg)

            else:
                logger.debug('不向大群发送简易版提醒')
            task.last_msg_time = time_now
            task.unread_msg_amount = 0
        else:
            logger.info('最近10分钟内没有未读消息')
Example #14
0
    def parse_order_details(self, orders, taoba_entity):
        my_logger.debug('taoba_entity: {}'.format(taoba_entity.taoba_id))
        my_logger.debug('keys: {}'.format(self.order_queues.keys()))
        if len(self.order_queues[taoba_entity.taoba_id]) == 0 and len(
                orders) == 0:
            my_logger.debug('订单队列为空')
            return
        jiebang_activities = global_config.MODIAN_JIEBANG_ACTIVITIES[
            taoba_entity.taoba_id]
        flag_activities = global_config.MODIAN_FLAG_ACTIVITIES[
            taoba_entity.taoba_id]
        count_flag_activities = global_config.MODIAN_COUNT_FLAG_ACTIVITIES[
            taoba_entity.taoba_id]

        # 查询集资情况
        pro_name, current, backer_count = self.get_current_and_target(
            taoba_entity)
        project_info = '当前进度: %s元' % current

        taoba_entity.current = current
        taoba_entity.title = pro_name
        # group_account_entity.target = target
        taoba_entity.support_num = backer_count
        my_logger.debug('size of order %s queue: %s', taoba_entity.taoba_id,
                        len(self.order_queues[taoba_entity.taoba_id]))

        for order in orders:
            user_id = order['userid']
            nickname = order['nickname']
            pay_time = util.convert_timestamp_to_timestr(order['addtime'] *
                                                         1000)

            single_price = order['amount']
            pay_amount = order['nums']

            backer_money = single_price
            listid = int(order['id'])

            my_logger.debug('oid: %s', listid)

            if listid in self.order_queues[taoba_entity.taoba_id]:
                continue
            my_logger.debug('项目%s队列长度: %s', taoba_entity.taoba_id,
                            len(self.order_queues[taoba_entity.taoba_id]))

            # 每次需要更新一下昵称
            try:
                mysql_util.query(
                    """
                                        INSERT INTO `supporter` (`id`, `name`) VALUES (%s, %s)  ON DUPLICATE KEY
                                            UPDATE `id`=%s
                                        """, (user_id, nickname, user_id))
            except Exception as e:
                my_logger.exception(e)

            try:
                # 创建对象
                mysql_util.query(
                    """
                                    INSERT INTO `order` (`id`,`supporter_id`,`backer_money`,`pay_time`, `pro_id`) 
                                    VALUES (%s, %s, %s, %s, %s) ON DUPLICATE KEY
                                            UPDATE `id`=%s
                                """,
                    (str(listid), user_id, backer_money, pay_time,
                     taoba_entity.taoba_id, str(listid)))
            except Exception as e:
                my_logger.exception(e)

            msg = '感谢 %s(%s) 支持了%s元, %s\n' % (
                nickname, user_id, backer_money,
                util.random_str(global_config.MODIAN_POSTSCRIPTS))
            # daka_rank, support_days = self.find_user_daka_rank(user_id, group_account_entity.pro_id)

            # if daka_rank != -1 and support_days:
            #     msg += '当前项目已打卡%s天\n' % support_days

            # if group_account_entity.need_display_rank is True:
            #     jizi_rank, backer_money = self.find_user_jizi_rank(user_id, group_account_entity.pro_id)
            #     if jizi_rank != -1:
            #         msg += '当前项目已集资%s元, 排名: %s' % (backer_money, jizi_rank)
            # else:
            #     pass

            # 统计当前人数
            count = mysql_util.select_one(
                """
                SELECT COUNT(distinct(`supporter_id`)) FROM `order` WHERE `pro_id`=%s
            """, (taoba_entity.taoba_id, ))
            backer_count = count[0]
            msg += '当前集资人数: %s\n' % backer_count

            # 抽卡播报
            card_report = ''
            if global_config.MODIAN_CARD_DRAW:
                card_report = self.card_draw_handler.draw(
                    user_id, nickname, backer_money, pay_time)
            '''接棒相关'''
            my_logger.debug('接棒情况更新')
            for jiebang in jiebang_activities:
                # if jiebang.start_time > time.time():
                #     continue
                my_logger.debug('接棒活动详情: 【%s】', jiebang.name)
                my_logger.debug('集资金额: %s, 接棒最小金额: %s', backer_money,
                                jiebang.min_stick_amount)
                if backer_money >= jiebang.min_stick_amount:

                    stick_num = util.compute_stick_num(
                        jiebang.min_stick_amount, backer_money)
                    jiebang.current_stick_num += stick_num

                    jiebang.last_record_time = util.convert_timestamp_to_timestr(
                        int(time.time() * 1000))
                    # 数据库也要更新
                    try:
                        mysql_util.query(
                            """
                            UPDATE jiebang SET `current_stick_num`=%s, `last_record_time`=%s WHERE `name`=%s
                        """, (jiebang.current_stick_num,
                              jiebang.last_record_time, jiebang.name))
                    except Exception as e:
                        my_logger.error('更新接棒数据失败')
                        my_logger.exception(e)
                    my_logger.debug('数据库接棒数据更新完成')
                    test_msg = ''
                    if jiebang.need_detail == 1:
                        test_msg = '【%s】, 当前第%s棒, 目标%s棒\n' \
                                   % (jiebang.name, jiebang.current_stick_num, jiebang.target_stick_num)
                    elif jiebang.need_detail == 0:
                        test_msg = '【%s】\n' % jiebang.name
                    elif jiebang.need_detail == 2:
                        test_msg = '【%s】, 当前第%s棒\n' \
                                   % (jiebang.name, jiebang.current_stick_num)
                    elif jiebang.need_detail == 3:
                        if stick_num > 1:
                            test_msg = '抽奖号: {}~{}\n'.format(
                                jiebang.current_stick_num - stick_num + 1,
                                jiebang.current_stick_num)
                        else:
                            test_msg = '抽奖号: {}\n'.format(
                                jiebang.current_stick_num)
                    my_logger.debug(test_msg)
                    if len(test_msg) > 0:
                        msg += test_msg
                        # QQHandler.send_to_groups(['483548995'], test_msg)
            '''金额类flag相关'''
            my_logger.debug('flag情况更新')
            flag_test_msgs = ''
            for flag in flag_activities:
                my_logger.debug('Flag活动详情: %s', flag.name)
                my_logger.debug('Flag金额: %s, 结束时间: %s',
                                flag.target_flag_amount, flag.end_time)
                diff = flag.target_flag_amount - current
                test_msgs = '【%s】, 目标金额: %s元, ' % (flag.name,
                                                   flag.target_flag_amount)
                if diff > 0:
                    test_msgs += '距离目标还差%s元\n' % round(diff, 2)
                    flag_test_msgs += test_msgs
                else:
                    test_msgs += '已经达成目标\n'
            my_logger.debug(flag_test_msgs)
            if len(flag_test_msgs) > 0:
                pass
                # QQHandler.send_to_groups(['483548995'], flag_test_msgs)
                # msg += flag_test_msgs
            '''人头类flag相关'''
            my_logger.debug('人头flag情况更新')
            count_flag_test_msgs = ''
            for flag in count_flag_activities:
                my_logger.debug('人头Flag活动详情: %s', flag.name)
                my_logger.debug('人头Flag目标: %s, 开始时间: %s, 结束时间: %s',
                                flag.target_flag_amount, flag.start_time,
                                flag.end_time)
                target = flag.target_flag_amount

                # 统计当前人数
                rst = mysql_util.select_one(
                    """
                    select count(distinct(`supporter_id`)) from `order` 
                    where `pro_id` = %s and `pay_time` <= %s and `pay_time` >= %s
                """, (taoba_entity.taoba_id, flag.end_time, flag.start_time))

                # 目标人数为0,代表特殊类flag,只报人数
                if target == 0:
                    count_flag_test_msgs += '【%s】, 当前人数: %s ' % (flag.name,
                                                                 rst[0])
                else:
                    count_flag_test_msgs += '【%s】, 当前人数: %s, 目标人数: %s ' % (
                        flag.name, rst[0], flag.target_flag_amount)

            my_logger.debug(count_flag_test_msgs)
            if len(count_flag_test_msgs) > 0:
                # QQHandler.send_to_groups(['483548995'], count_flag_test_msgs)
                pass
                # msg += flag_test_msgs

            msg += '%s\n集资项目: %s\n集资方式: %s\n' % (project_info, pro_name,
                                                 taoba_entity.link)
            # msg += jizi_pk_report

            # if global_config.MODIAN_NEED_DISPLAY_PK:
            #     msg += self.pk_modian_activity()

            if global_config.USING_COOLQ_PRO is True:
                my_logger.debug('使用酷Q PRO发送图片')
                msg += '\n[CQ:image,file={}]\n'.format(taoba_entity.qrcode)

            # QQHandler.send_to_groups(taoba_entity.broadcast_groups, msg)
            if card_report:
                pass
                # QQHandler.send_to_groups(taoba_entity.broadcast_groups, card_report)
                # QQHandler.send_to_groups(['483548995'], card_report)

            # 集五福
            # wufu_report = modian_wufu_handler.draw(user_id, nickname, backer_money, pay_time)
            # if wufu_report:
            #     QQHandler.send_to_groups(group_account_entity.broadcast_groups, wufu_report)

            self.order_queues[taoba_entity.taoba_id].add(listid)

        # 更新接棒的数据库
        try:
            my_logger.debug('更新接棒活动信息:')
            for jiebang in jiebang_activities:
                my_logger.debug(
                    'current_stick_num: %s, last_record_time: %s, name: %s',
                    jiebang.current_stick_num, jiebang.last_record_time,
                    jiebang.name)
                mysql_util.query(
                    """
                    UPDATE jiebang SET current_stick_num=%s WHERE name=%s
                """, (jiebang.current_stick_num, jiebang.name))

        except Exception as e:
            my_logger.exception(e)
Example #15
0
 def generate_session_id():
     return random_str(16)
 def generate_session_id():
     return random_str(16)
Example #17
0
 def refresh_key(self):
     self.key = random_str(32)
     self.key_time = int(time.time())
     self.save()
Example #18
0
 def refresh_key(self):
     self.key = random_str(32)
     self.key_time = int(time.time())
     self.save()
Example #19
0
def onQQMessage(bot, contact, member, content):
    # 当收到 QQ 消息时被调用
    # bot     : QQBot 对象,提供 List/SendTo/GroupXXX/Stop/Restart 等接口,详见文档第五节
    # contact : QContact 对象,消息的发送者
    # member  : QContact 对象,仅当本消息为 群或讨论组 消息时有效,代表实际发消息的成员
    # content : str 对象,消息内容
    # DEBUG('member: %s', str(getattr(member, 'uin')))
    # DEBUG('content: %s', content)
    # DEBUG('contact: %s', contact.ctype)
    global pocket48_handler

    if contact.ctype == 'group':
        if '@ME' in content:  # 在群中@机器人
            strs = content.split('[@ME]')[1].strip()
            if strs :
                id = contact.uin
                bot.SendTo(contact, pocket48_handler.get_tuling_ai(id, strs))
            else:
                bot.SendTo(contact, util.random_str(global_config.AT_AUTO_REPLY))
        elif content.startswith('-'):  # 以'-'开头才能触发自动回复
            if content == '-version':
                bot.SendTo(contact, 'QQbot-' + bot.conf.version)
            elif content == '-票务' or content == '-公演':
                pocket48_handler.get_current_ticket_info_msg(global_config.TICKET_INFO)
            elif content == '-屏蔽全部消息' or content == '-屏蔽所有消息':
                if len(global_config.ACTIVE_MEMBER_ROOM_ID_LIST) > 0:
                    global_config.DEACTIVE_MEMBER_ROOM_ID_LIST = ConfigReader.get_all_member_room_id_list()
                    global_config.ACTIVE_MEMBER_ROOM_ID_LIST = []
                    QQHandler.send_to_groups(pocket48_handler.member_room_msg_groups, '已屏蔽全部房间消息')

                else:
                    QQHandler.send_to_groups(pocket48_handler.member_room_msg_groups, '当前已屏蔽全部房间消息,请不要重复输入命令')
            elif content == '-开启全部消息' or content == '-开启全部消息':
                if len(global_config.DEACTIVE_MEMBER_ROOM_ID_LIST) > 0:
                    global_config.ACTIVE_MEMBER_ROOM_ID_LIST = ConfigReader.get_all_member_room_id_list()
                    global_config.DEACTIVE_MEMBER_ROOM_ID_LIST = []
                    pocket48_handler.init_msg_queues(global_config.ACTIVE_MEMBER_ROOM_ID_LIST)
                    QQHandler.send_to_groups(pocket48_handler.member_room_msg_groups, '已开启全部房间消息')
                else:
                    QQHandler.send_to_groups(pocket48_handler.member_room_msg_groups, '当前已开启全部房间消息,请不要重复输入命令')
            elif content == '-关闭':
                bot.SendTo(contact, 'QQ机器人已关闭')
                bot.Stop()
            elif '-屏蔽' in content and '消息' in content:
                if len(global_config.ACTIVE_MEMBER_ROOM_ID_LIST) > 0:
                    name = util.get_order_name(content)
                    # 命令是否包含成员名字
                    if util.is_name_in_member_list(name):
                        # 屏蔽逻辑
                        pocket48_handler.deactive_member_by_name(name)
                    else:
                        QQHandler.send_to_groups(pocket48_handler.member_room_msg_groups, '屏蔽失败, 无法识别该成员,请确保命令输入正确')
                else:
                     QQHandler.send_to_groups(pocket48_handler.member_room_msg_groups, '当前已屏蔽全部房间消息,无法再进行屏蔽')
            elif '-开启' in content and '消息' in content:
                if len(global_config.DEACTIVE_MEMBER_ROOM_ID_LIST) > 0:
                    name = util.get_order_name(content)
                    # 命令是否包含成员名字
                    if util.is_name_in_member_list(name):
                        # 开启逻辑
                        pocket48_handler.active_member_by_name(name)
                    else:
                        QQHandler.send_to_groups(pocket48_handler.member_room_msg_groups, '开启失败, 无法识别该成员,请确保命令输入正确')
                else:
                    QQHandler.send_to_groups(pocket48_handler.member_room_msg_groups, '当前已开启全部房间消息,无法再进行开启')
            elif content == '-直播':
                r = pocket48_handler.get_member_live_msg()
                pocket48_handler.parse_member_live_now(r, global_config.LIVING_MEMBER_ID_LIST)
            elif content == '-小黑屋':
                msg = util.get_black_room_list()
                QQHandler.send_to_groups(pocket48_handler.member_room_msg_groups, msg)
            elif content == '-小白屋':
                msg = util.get_white_room_list()
                QQHandler.send_to_groups(pocket48_handler.member_room_msg_groups, msg)
            elif content == '-使用说明':
                msg = '''
机器人使用说明:
一、命令类型(输入一次即可, 没回复可能是机器人没开启 或者 机器人卡住了):
    (1)'-票务':  查看尚未公演的票务信息.
    (2)'-直播':  查看所有正在直播的直播间.
    (3)'-使用说明':  机器人使用说明.
    (4)'-屏蔽全部(所有)消息':  屏蔽全部成员房间消息.
    (5)'-开启全部(所有)消息':  开启全部成员房间消息.
    (6)'-屏蔽' + 成员名字 + '消息':  屏蔽某个成员房间消息.
    (7)'-开启' + 成员名字 + '消息':  开启某个成员房间消息.
    (8)'-小黑屋':  查看被屏蔽房间消息的成员.
    (9)'-小白屋':  查看已开启房间消息的成员
    其余命令均无法识别
二、聊天
    '@机器人' + 聊天内容:  与图灵机器人聊天 (每天上限1000次, 河内梗完善中...)'''
                QQHandler.send_to_groups(pocket48_handler.member_room_msg_groups, msg)
            else:  # 无法识别命令
                no_such_command = ConfigReader.get_property('profile', 'no_such_command')
                bot.SendTo(contact, no_such_command)
Example #20
0
    def parse_order_details(self, orders, modian_entity):
        if len(self.order_queues[modian_entity.pro_id]) == 0 and len(
                orders) == 0:
            my_logger.debug('订单队列为空')
            return
        jiebang_activities = global_config.MODIAN_JIEBANG_ACTIVITIES[
            modian_entity.pro_id]
        flag_activities = global_config.MODIAN_FLAG_ACTIVITIES[
            modian_entity.pro_id]
        count_flag_activities = global_config.MODIAN_COUNT_FLAG_ACTIVITIES[
            modian_entity.pro_id]

        # 查询集资情况
        target, current, pro_name, backer_count = self.get_current_and_target(
            modian_entity)
        project_info = '当前进度: %s元, 目标金额: %s元' % (current, target)

        modian_entity.current = current
        modian_entity.title = pro_name
        modian_entity.target = target
        modian_entity.support_num = backer_count
        my_logger.debug('size of order %s queue: %s', modian_entity.pro_id,
                        len(self.order_queues[modian_entity.pro_id]))

        for order in orders:
            user_id = order['user_id']
            nickname = order['nickname']
            pay_time = order['pay_time']
            backer_money = order['backer_money']

            oid = uuid.uuid3(uuid.NAMESPACE_OID, str(user_id) + pay_time)
            my_logger.debug('oid: %s', oid)

            if oid in self.order_queues[modian_entity.pro_id]:
                continue
            my_logger.debug('项目%s队列长度: %s', modian_entity.pro_id,
                            len(self.order_queues[modian_entity.pro_id]))

            # 每次需要更新一下昵称
            try:
                # supporter = Supporter(id=user_id, name=nickname)
                # self.alchemy_session.merge(supporter)
                mysql_util.query(
                    """
                                        INSERT INTO `supporter` (`id`, `name`) VALUES (%s, %s)  ON DUPLICATE KEY
                                            UPDATE `name`=%s
                                        """, (user_id, nickname, nickname))
            except Exception as e:
                my_logger.exception(e)

            try:
                # 创建对象
                # modian_order = ModianOrder(id=str(oid), supporter_id=user_id, backer_money=backer_money, pay_time=pay_time,
                #                            pro_id=modian_entity.pro_id)
                # self.alchemy_session.merge(modian_order)
                mysql_util.query(
                    """
                                    INSERT INTO `order` (`id`,`supporter_id`,`backer_money`,`pay_time`, `pro_id`) 
                                    VALUES (%s, %s, %s, %s, %s) ON DUPLICATE KEY
                                            UPDATE `id`=%s
                                """,
                    (str(oid), user_id, backer_money, pay_time,
                     modian_entity.pro_id, str(oid)))
            except Exception as e:
                my_logger.exception(e)

            msg = '感谢 %s(%s) 支持了%s元, %s\n' % (
                nickname, user_id, backer_money,
                util.random_str(global_config.MODIAN_POSTSCRIPTS))
            daka_rank, support_days = self.find_user_daka_rank(
                user_id, modian_entity.pro_id)

            if daka_rank != -1 and support_days:
                msg += '当前项目已打卡%s天\n' % support_days

            if modian_entity.need_display_rank is True:
                jizi_rank, backer_money = self.find_user_jizi_rank(
                    user_id, modian_entity.pro_id)
                if jizi_rank != -1:
                    msg += '当前项目已集资%s元, 排名: %s' % (backer_money, jizi_rank)
            else:
                pass
            # 统计当前人数
            msg += '当前集资人数: %s\n' % backer_count

            # 抽卡播报
            card_report = ''
            if global_config.MODIAN_CARD_DRAW:
                card_report = self.card_draw_handler.draw(
                    user_id, nickname, backer_money, pay_time)
            '''接棒相关'''
            my_logger.debug('接棒情况更新')
            for jiebang in jiebang_activities:
                # if jiebang.start_time > time.time():
                #     continue
                my_logger.debug('接棒活动详情: 【%s】', jiebang.name)
                my_logger.debug('集资金额: %s, 接棒最小金额: %s', backer_money,
                                jiebang.min_stick_amount)
                if backer_money >= jiebang.min_stick_amount:

                    stick_num = util.compute_stick_num(
                        jiebang.min_stick_amount, backer_money)
                    jiebang.current_stick_num += stick_num

                    jiebang.last_record_time = util.convert_timestamp_to_timestr(
                        int(time.time() * 1000))
                    # 数据库也要更新
                    try:
                        mysql_util.query(
                            """
                            UPDATE jiebang SET `current_stick_num`=%s, `last_record_time`=%s WHERE `name`=%s
                        """, (jiebang.current_stick_num,
                              jiebang.last_record_time, jiebang.name))
                    except Exception as e:
                        my_logger.error('更新接棒数据失败')
                        my_logger.exception(e)
                    my_logger.debug('数据库接棒数据更新完成')
                    test_msg = ''
                    if jiebang.need_detail == 1:
                        test_msg = '【%s】, 当前第%s棒, 目标%s棒\n' \
                                   % (jiebang.name, jiebang.current_stick_num, jiebang.target_stick_num)
                    elif jiebang.need_detail == 0:
                        test_msg = '【%s】\n' % jiebang.name
                    elif jiebang.need_detail == 2:
                        test_msg = '【%s】, 当前第%s棒\n' \
                                   % (jiebang.name, jiebang.current_stick_num)
                    elif jiebang.need_detail == 3:
                        if stick_num > 1:
                            test_msg = '抽奖号: {}~{}\n'.format(
                                jiebang.current_stick_num - stick_num + 1,
                                jiebang.current_stick_num)
                        else:
                            test_msg = '抽奖号: {}\n'.format(
                                jiebang.current_stick_num)
                    my_logger.debug(test_msg)
                    if len(test_msg) > 0:
                        msg += test_msg
                        QQHandler.send_to_groups(['483548995'], test_msg)
            '''金额类flag相关'''
            my_logger.debug('flag情况更新')
            flag_test_msgs = ''
            for flag in flag_activities:
                my_logger.debug('Flag活动详情: %s', flag.name)
                my_logger.debug('Flag金额: %s, 结束时间: %s',
                                flag.target_flag_amount, flag.end_time)
                diff = flag.target_flag_amount - current
                test_msgs = '【%s】, 目标金额: %s元, ' % (flag.name,
                                                   flag.target_flag_amount)
                if diff > 0:
                    test_msgs += '距离目标还差%s元\n' % round(diff, 2)
                    flag_test_msgs += test_msgs
                else:
                    test_msgs += '已经达成目标\n'
            my_logger.debug(flag_test_msgs)
            if len(flag_test_msgs) > 0:
                QQHandler.send_to_groups(['483548995'], flag_test_msgs)
                # msg += flag_test_msgs
            '''人头类flag相关'''
            my_logger.debug('人头flag情况更新')
            count_flag_test_msgs = ''
            for flag in count_flag_activities:
                my_logger.debug('人头Flag活动详情: %s', flag.name)
                my_logger.debug('人头Flag目标: %s, 开始时间: %s, 结束时间: %s',
                                flag.target_flag_amount, flag.start_time,
                                flag.end_time)
                target = flag.target_flag_amount

                # 统计当前人数
                rst = mysql_util.select_one(
                    """
                    select count(distinct(`supporter_id`)) from `order` 
                    where `pro_id` = %s and `pay_time` <= %s and `pay_time` >= %s
                """, (modian_entity.pro_id, flag.end_time, flag.start_time))

                # 目标人数为0,代表特殊类flag,只报人数
                if target == 0:
                    count_flag_test_msgs += '【%s】, 当前人数: %s ' % (flag.name,
                                                                 rst[0])
                else:
                    count_flag_test_msgs += '【%s】, 当前人数: %s, 目标人数: %s ' % (
                        flag.name, rst[0], flag.target_flag_amount)

            my_logger.debug(count_flag_test_msgs)
            if len(count_flag_test_msgs) > 0:
                QQHandler.send_to_groups(['483548995'], count_flag_test_msgs)
                # msg += flag_test_msgs

            msg += '%s\n集资项目: %s\n链接: %s\n' % (project_info, pro_name,
                                               modian_entity.link)
            # msg += jizi_pk_report

            my_logger.info(msg)
            if global_config.USING_COOLQ_PRO is True:
                my_logger.debug('使用酷Q PRO发送图片')
                msg += '\n[CQ:image,file=http://wx1.sinaimg.cn/large/439a9f3fgy1fpllweknr6j201i01g0lz.jpg]\n'

            # if global_config.MODIAN_NEED_DISPLAY_PK:
            #     msg += self.pk_modian_activity()

            QQHandler.send_to_groups(modian_entity.broadcast_groups, msg)
            if card_report:
                QQHandler.send_to_groups(modian_entity.broadcast_groups,
                                         card_report)
                # QQHandler.send_to_groups(['483548995'], card_report)

            self.order_queues[modian_entity.pro_id].add(oid)

        # 更新接棒的数据库
        try:
            my_logger.debug('更新接棒活动信息:')
            for jiebang in jiebang_activities:
                my_logger.debug(
                    'current_stick_num: %s, last_record_time: %s, name: %s',
                    jiebang.current_stick_num, jiebang.last_record_time,
                    jiebang.name)
                mysql_util.query(
                    """
                    UPDATE jiebang SET current_stick_num=%s WHERE name=%s
                """, (jiebang.current_stick_num, jiebang.name))

        except Exception as e:
            my_logger.exception(e)
    def parse_wds_comment2(self, r, wds):
        """
        对评论进行处理(微打赏接口变更,r变为html)
        :param r:
        :param wds:
        :return:
        """
        PAGE_SIZE = 20
        soup = BeautifulSoup(r, 'lxml')
        # print soup.prettify()
        comment_list = soup.findAll(name='li')
        support_num, current, target = self.get_current_and_target(wds)
        # project_info = '当前进度: %s元, 目标金额: %s元\n当前集资人数: %s\n' % (current, target, support_num)

        page_num = int(support_num) / PAGE_SIZE + 1
        wds_rank_list = []
        for i in range(page_num):
            rank_html = self.get_wds_rank(wds, page=i + 1)
            if rank_html is None:
                break
            soup2 = BeautifulSoup(rank_html, 'lxml')
            wds_rank_list.extend(soup2.findAll(name='li'))

        project_info = '当前进度: %s元, 目标金额: %s元\n当前集资人数: %s\n' % (
            current, target, len(wds_rank_list))

        for comment in comment_list:
            comment_id = comment.find(class_='add-jubao').get('to_comid')
            # print comment_id

            comment_id_queue = self.wds_queue_map[wds]
            if comment_id in comment_id_queue:
                continue
            comment_id_queue.append(comment_id)

            nickname = comment.find(class_='nick').string
            nick_sup = comment.find(class_='nick_sup').string
            user_id = comment.find(class_='add-jubao').get('to_user')

            msg = '感谢 %s %s, %s\n' % (nickname, nick_sup,
                                      util.random_str(
                                          global_config.WDS_POSTSCRIPTS))

            rank_msg = ''
            DEBUG('WDS USER ID: %s', user_id)
            for rank in wds_rank_list:
                user_a = rank.a['href']
                uid = re.findall(r"\d+\.?\d*", user_a)[0]
                DEBUG('user_a: %s, uid: %s', user_a, uid)
                if uid == user_id:
                    cur_rank = rank.find(class_='suport_ran').string
                    total_amount = rank.find(class_='money').string
                    total_amount = total_amount.replace('¥ ', '')

                    if wds.need_display_rank is True:
                        rank_msg = "当前累计集资%s元,当前排名: %s\n" % (total_amount,
                                                             cur_rank)
                    else:
                        rank_msg = "当前累计集资%s元\n" % total_amount
                    break
            if rank_msg and len(rank_msg) > 0:
                msg += rank_msg

            if msg and len(msg) > 0:
                msg += project_info
                msg += '集资项目: %s\n链接: %s' % (wds.title, wds.link)
                QQHandler.send_to_groups(self.wds_notify_groups, msg)
                INFO('wds_message: %s', msg)
                DEBUG('集资评论队列: %d', len(comment_id_queue))

            time.sleep(3)