def init_order_queues(self): for modian_entity in self.modian_project_array: try: my_logger.info('初始化%s的订单队列', modian_entity.pro_id) my_logger.debug(self.order_queues) if modian_entity.pro_id not in self.order_queues: self.order_queues[modian_entity.pro_id] = set() my_logger.debug('队列为空,重新初始化队列') else: my_logger.debug('队列不为空,不重新初始化队列') continue # self.order_queues[modian_entity.pro_id] = set() my_logger.debug('项目%s队列长度: %s', modian_entity.pro_id, len(self.order_queues[modian_entity.pro_id])) orders = self.query_project_orders(modian_entity) # retry_time = 0 # while retry_time < 5: # retry_time += 1 # if len(orders) == 0: # my_logger.debug('请求订单失败,第%s次重试', retry_time) # orders = self.query_project_orders(modian_entity) # else: # break for order in orders: user_id = order['user_id'] pay_time = order['pay_time'] oid = uuid.uuid3(uuid.NAMESPACE_OID, str(user_id) + pay_time) self.order_queues[modian_entity.pro_id].add(oid) except Exception as e: my_logger.error('初始化订单队列失败!') my_logger.exception(e)
def get_modian_rankings(self, modian_entity, type0=1, page=1): """ 查询项目集资榜和打卡榜 :param type0: 排名类型,1代表集资榜,2代表打卡榜 :param modian_entity: :param page: 页号,每页默认返回20条 :return: """ if type0 == 1: my_logger.info('查询项目集资榜, page=%s', page) elif type0 == 2: my_logger.info('查询项目打卡榜, page=%s', page) else: my_logger.error('type0参数不合法') raise RuntimeError('type0参数不合法!') api = 'https://wds.modian.com/api/project/rankings' params = {'pro_id': modian_entity.pro_id, 'type': type0, 'page': page} r = requests.post(api, self.make_post_params(params), headers=self.modian_header()).json() if int(r['status']) == 0: # pro_name = r['data']['pro_name'] rankings = r['data'] # my_logger.info('查询项目排名: %s', rankings) return rankings elif int(r['status'] == 2): return [] else: raise RuntimeError('获取项目排名失败, type=%d', type0)
def init_order_queues(self): for modian_entity in self.modian_project_array: try: my_logger.info('初始化%s的订单队列', modian_entity.pro_id) my_logger.debug(self.order_queues) if modian_entity.pro_id not in self.order_queues: self.order_queues[modian_entity.pro_id] = set() my_logger.debug('队列为空,重新初始化队列') else: my_logger.debug('队列不为空,不重新初始化队列') continue my_logger.debug('项目%s队列长度: %s', modian_entity.pro_id, len(self.order_queues[modian_entity.pro_id])) # 先拉一把项目的基本资料 self.get_project_profiles(modian_entity) orders = self.query_project_orders(modian_entity) for order in orders: backer_money_icon = order.find(class_='icon-payment') if not backer_money_icon: # 纯评论,直接跳过 continue comment_id = order.get('data-reply-id') # oid使用项目id和评论id拼装 oid = str(modian_entity.pro_id) + str(comment_id) my_logger.debug('oid: %s', oid) self.order_queues[modian_entity.pro_id].add(oid) except Exception as e: my_logger.error('初始化订单队列失败!') my_logger.exception(e)
def init_order_queues(self): for group_account_entity in self.group_account_project_array: try: my_logger.info('初始化%s的订单队列', group_account_entity.group_account_id) my_logger.debug(self.order_queues) if group_account_entity.group_account_id not in self.order_queues: self.order_queues[ group_account_entity.group_account_id] = set() my_logger.debug('队列为空,重新初始化队列') else: my_logger.debug('队列不为空,不重新初始化队列') continue my_logger.debug( '项目%s队列长度: %s', group_account_entity.group_account_id, len(self.order_queues[ group_account_entity.group_account_id])) orders = self.query_project_orders(group_account_entity) for order in orders: listid = int(order['listid']) self.order_queues[ group_account_entity.group_account_id].add(listid) except Exception as e: my_logger.error('初始化订单队列失败!') my_logger.exception(e)
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_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)
def update_modian_conf(): global modian_handler time0 = time.time() my_logger.info('读取摩点配置') ConfigReader.read_conf() # modian_json = json.load(open("data/modian.json", encoding='utf8')) # modian_json = json.load(open("data/weixin_group_account.json", encoding='utf8')) modian_json = json.load(open("data/taoba_account.json", encoding='utf8')) modian_handler.login(modian_json['taoba_account'], modian_json['taoba_passwd']) global_config.MODIAN_POSTSCRIPTS = modian_json['modian_postscripts'] # 摩点集资PK链接数组初始化 global_config.MODIAN_NEED_DISPLAY_PK = modian_json[ 'modian_need_display_pk'] for modian_pk_j in modian_json['modian_pk_activities']: global_config.MODIAN_PK_ARRAY.append(modian_pk_j) # 是否需要开启抽卡功能 global_config.MODIAN_CARD_DRAW = modian_json['modian_need_card_draw'] # global_config.MODIAN_300_ACTIVITY = modian_json['modian_300_activity'] # 需要适应同时开多个链接的情况 global_config.MODIAN_ARRAY = [] for modian_j in modian_json['monitor_activities']: # if modian_j['modian_need_display_rank'] is False: # modian = ModianEntity(modian_j['modian_link'], modian_j['modian_title'], modian_j['modian_pro_id'], False, # modian_j['broadcast_groups']) # modian = GroupAccountEntity(modian_j['modian_link'], modian_j['modian_title'], modian_j['modian_pro_id'], # modian_j['broadcast_groups'], modian_j['qrcode']) modian = TaoBaEntity(modian_j['modian_link'], modian_j['modian_title'], modian_j['modian_pro_id'], modian_j['broadcast_groups'], modian_j['qrcode']) # elif modian_j['wds_need_display_rank'] is True: # modian = ModianEntity(modian_j['modian_link'], modian_j['modian_title'], modian_j['modian_pro_id'], True, # modian_j['broadcast_groups']) global_config.MODIAN_ARRAY.append(modian) modian_handler.taoba_project_array = global_config.MODIAN_ARRAY # modian_handler.init_order_queues() modian_handler.card_draw_handler.read_config() global_config.JIZI_NOTIFY_GROUPS = ConfigReader.get_property( 'qq_conf', 'jizi_notify_groups').split(';') modian_groups = global_config.JIZI_NOTIFY_GROUPS modian_handler.group_account_notify_groups = modian_groups my_logger.debug('JIZI_NOTIFY_GROUPS: %s, length: %d', ','.join(global_config.JIZI_NOTIFY_GROUPS), len(modian_handler.group_account_notify_groups)) my_logger.debug('读取正在进行中的flag活动') global_config.MODIAN_FLAG_ACTIVITIES = {} flag_json = json.load(open('data/modian_flag.json', encoding='utf8'))['activities'] for modian in global_config.MODIAN_ARRAY: pro_id = modian.taoba_id global_config.MODIAN_FLAG_ACTIVITIES[pro_id] = [] for activity in flag_json: pro_id = activity['pro_id'] end_time = activity['end_time'] if util.convert_timestr_to_timestamp(end_time) > time.time(): flag = ModianFlagEntity(activity['flag_name'], activity['pro_id'], activity['target_flag_amount'], activity['end_time'], activity['remark']) global_config.MODIAN_FLAG_ACTIVITIES[int(pro_id)].append(flag) my_logger.debug('MODIAN_FLAG_ACTIVITIES: %s', global_config.MODIAN_FLAG_ACTIVITIES) my_logger.debug('读取正在进行的人头flag活动') global_config.MODIAN_COUNT_FLAG_ACTIVITIES = {} count_flag_json = json.load( open('data/modian_count_flag.json', encoding='utf8'))['activities'] for modian in global_config.MODIAN_ARRAY: pro_id = modian.taoba_id global_config.MODIAN_COUNT_FLAG_ACTIVITIES[pro_id] = [] for activity in count_flag_json: pro_id = activity['pro_id'] start_time = activity['start_time'] end_time = activity['end_time'] if util.convert_timestr_to_timestamp( start_time) >= util.convert_timestr_to_timestamp(end_time): my_logger.error('人头类flag,起始时间大于结束时间!') raise RuntimeError('起始时间大于结束时间') time0 = time.time() if util.convert_timestr_to_timestamp( end_time) > time0 > util.convert_timestr_to_timestamp( start_time): flag = ModianCountFlagEntity(activity['flag_name'], activity['pro_id'], activity['target_flag_amount'], activity['start_time'], activity['end_time'], activity['remark']) global_config.MODIAN_COUNT_FLAG_ACTIVITIES[int(pro_id)].append( flag) my_logger.debug('MODIAN_COUNT_FLAG_ACTIVITIES: %s', global_config.MODIAN_COUNT_FLAG_ACTIVITIES) # 接棒活动更新,读取json文件中的内容,更新到数据库中 my_logger.debug('接棒活动更新,读取json文件中的内容,更新到数据库中') jiebang_json = json.load(open('data/modian_jiebang.json', encoding='utf8'))['activities'] # conn = sqlite3.connect('data/modian.db', check_same_thread=False) for activity in jiebang_json: end_time = activity['end_time'] my_logger.debug('活动结束时间: {}; 当前时间:{}'.format( util.convert_timestr_to_timestamp(end_time), time.time())) if util.convert_timestr_to_timestamp(end_time) < time.time(): my_logger.debug('活动结束时间早于当前时间,跳过') continue name = activity['jiebang_name'] try: # cursor = conn.cursor() # c = cursor.execute(""" # select * from jiebang WHERE name=? # """, (name, )) # rst = c.fetchall() rst = mysql_util.select_one( """ select * from jiebang WHERE name=%s """, (name, )) my_logger.debug(rst) if rst is not None: my_logger.debug('DB中有相应的接棒活动') else: my_logger.debug('DB中没有对应的接棒活动,需要创建') mysql_util.query( """ INSERT INTO jiebang (name, pro_id, current_stick_num, start_time, end_time, target_stick_num, min_stick_amount, need_detail) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) """, (name, activity['pro_id'], 0, activity['start_time'], activity['end_time'], activity['target_stick_num'], activity['min_stick_amount'], activity['need_detail'])) # conn.commit() # else: # raise RuntimeError('接棒活动名称错误!') except Exception as e: my_logger.error('读取mysql出现错误') my_logger.exception(e) # finally: # cursor.close() # 读取正在进行中的接棒活动 my_logger.debug('读取正在进行中的接棒活动') global_config.MODIAN_JIEBANG_ACTIVITIES = {} for modian in global_config.MODIAN_ARRAY: pro_id = modian.taoba_id global_config.MODIAN_JIEBANG_ACTIVITIES[pro_id] = [] try: # cursor = conn.cursor() rst = mysql_util.select_all( """ SELECT name, pro_id, current_stick_num, last_record_time, start_time, end_time, target_stick_num, min_stick_amount, need_detail FROM jiebang where pro_id=%s and end_time >= NOW() and current_stick_num < target_stick_num """, (pro_id, )) if rst: for jiebang in rst: my_logger.debug('jiebang name: {}'.format( str(jiebang[0], encoding='utf-8'))) # 修正当前棒数 my_logger.info('修正接棒棒数') real_stick_num = 0 rst0 = mysql_util.select_all( """ SELECT backer_money FROM `order` WHERE pro_id = %s and backer_money >= %s and pay_time >= %s and pay_time <= %s """, (pro_id, jiebang[7], jiebang[4], jiebang[5])) my_logger.debug( """ SELECT backer_money FROM `order` WHERE pro_id = %s and backer_money >= %s and pay_time >= %s and pay_time <= %s """ % (pro_id, jiebang[7], jiebang[4], jiebang[5])) my_logger.debug(rst0) if rst0: for order in rst0: my_logger.debug('order: {}'.format(order[0])) real_stick_num += int(order[0] // jiebang[7]) my_logger.info('记录棒数: {}, 实际棒数: {}'.format( jiebang[2], real_stick_num)) mysql_util.query( """ UPDATE jiebang SET current_stick_num = %s WHERE name = %s """, (real_stick_num, jiebang[0])) my_logger.debug( 'jiebang: %s, %s, %s, %s, %s, %s, %s, %s, %s', jiebang[0], jiebang[1], jiebang[2], jiebang[3], jiebang[4], jiebang[5], jiebang[6], jiebang[7], jiebang[8]) jiebang_entity = ModianJiebangEntity( str(jiebang[0], encoding='utf-8'), jiebang[1], jiebang[2], jiebang[3], jiebang[4], jiebang[5], jiebang[6], jiebang[7], jiebang[8]) jiebang_entity.current_stick_num = real_stick_num my_logger.info('修正完成') global_config.MODIAN_JIEBANG_ACTIVITIES[pro_id].append( jiebang_entity) except Exception as e: my_logger.error('读取正在进行中的接棒活动出现错误!') my_logger.exception(e) # finally: # cursor.close() my_logger.debug(global_config.MODIAN_JIEBANG_ACTIVITIES) # conn.close() my_logger.debug('读取摩点配置耗时: %s秒', time.time() - time0) modian_handler.init_order_queues()