def idol_flip_header_args(self): """ 构造收费翻牌请求头信息 :return: """ logger.debug('token: %s', self.token) header = { 'Content-Type': 'application/json;charset=utf-8', 'User-Agent': 'PocketFans201807/6.0.16 (iPad; iOS 13.5.1; Scale/2.00)', 'pa': global_config.POCKET48_PA, 'Host': 'pocketapi.48.cn', 'appInfo': json.dumps({ 'vendor': 'apple', 'deviceId': '0', "appVersion": global_config.POCKET48_VERSION, "appBuild": "200701", "osVersion": "13.5.1", "osType": "ios", "deviceName": "unknow", "os": "ios" }), 'token': self.token } return header
def get_room_msgs(): for task in pocket48_handler.listen_tasks: if task.member.member_id == 0: continue start_t = time.time() r1 = pocket48_handler.get_member_room_msg(task) pocket48_handler.parse_room_msg(r1, task) r2 = pocket48_handler.get_member_room_comment(task) pocket48_handler.parse_room_comment(r2, task) end_t = time.time() my_logger.debug('获取{}房间消息 执行时间: {}'.format(task.member.name, end_t - start_t))
def notify_performance(self): base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) db_path = os.path.join(base_dir, 'data', 'schedule.json') f = open(db_path, encoding='utf8') schedules = json.load(f) for s in schedules['schedules']: logger.debug(s) perform_time = util.convert_timestr_to_timestamp(s['time']) logger.debug('【日程提醒】perform_time: {}, current_time: {}'.format( perform_time, time.time())) diff = perform_time - time.time() if 0 < diff <= 15 * 60: live_link = ''.join(global_config.LIVE_LINK) live_msg = '直播传送门: %s' % live_link notify_str = '%s\n公演: %s\n时间: %s\n队伍: %s\n%s' % ( global_config.PERFORMANCE_NOTIFY, s['name'], s['time'], s['team'], live_msg) logger.info('notify str: %s', notify_str)
def update_conf(): """ 每隔1分钟读取配置文件 :return: """ my_logger.debug('读取配置文件:conf.ini') ConfigReader.read_conf() auto_reply_groups = global_config.AUTO_REPLY_GROUPS test_groups = global_config.TEST_GROUPS pocket48_handler.auto_reply_groups = auto_reply_groups pocket48_handler.test_groups = test_groups my_logger.debug('auto_reply_groups: %s, length: %d', ','.join(global_config.AUTO_REPLY_GROUPS), len(pocket48_handler.auto_reply_groups)) for task in global_config.POCKET48_LISTEN_TASKS: pocket48_handler.listen_tasks.append(task) pocket48_handler.init_msg_queues(task)
def init_msg_queues(self, task): """ 初始化房间消息队列 :param task: :return: """ try: task.member_room_msg_ids = [] task.member_room_comment_ids = [] task.member_live_ids = [] task.unread_msg_amount = 0 r1 = self.get_member_room_msg(task) r2 = self.get_member_room_comment(task) r1_json = json.loads(r1) r2_json = json.loads(r2) for r in r1_json['content']['message']: msg_id = r['msgidClient'] task.member_room_msg_ids.append(msg_id) for r in r2_json['content']['message']: msg_id = r['msgidClient'] task.member_room_comment_ids.append(msg_id) logger.debug('成员{}消息队列: {}'.format(task.member.name, len(task.member_room_msg_ids))) logger.debug('{}房间评论队列: {}'.format( task.member.name, len(task.member_room_comment_ids))) logger.debug('{}房间未读消息数量: {}'.format(task.member.name, task.unread_msg_amount)) except Exception as e: logger.error('初始化{}消息队列失败'.format(task.member.name)) logger.exception(e)
def _get_room_msg(member_id, room_id, last_time, limit): time.sleep(10) url = 'https://pocketapi.48.cn/im/api/v1/chatroom/msg/list/homeowner' params = { "ownerId": member_id, "roomId": room_id, "nextTime": last_time } try: r = requests.post(url, data=json.dumps(params), headers=get_header(), verify=False).text except Exception as e: print(e) rsp_json = json.loads(r) msgs = rsp_json['content']['message'] if len(msgs) == 0: return -1 cursor = conn.cursor() msg_time = last_time try: for msg in msgs: extInfo = json.loads(msg['extInfo']) msg_id = msg['msgidClient'] # 消息id # rst = cursor.execute(""" # SELECT * FROM 'room_message' WHERE message_id=? # """, msg_id) msg_time = util.convert_timestamp_to_timestr(msg["msgTime"]) user_id = extInfo['user']['userId'] user_name = extInfo['user']['nickName'] # print('extInfo.keys():' + ','.join(extInfo.keys())) if msg['msgType'] == MessageType.TEXT: # 文字消息 text_message_type = extInfo['messageType'].strip() if text_message_type == MessageType.TEXT: # 普通消息 print('普通消息: %s' % extInfo['text']) save_msg_to_db(cursor, 100, msg_id, user_id, user_name, msg_time, extInfo['text']) elif text_message_type == TextMessageType.REPLY: # 翻牌消息 print('翻牌') member_msg = extInfo['text'] fanpai_msg = extInfo['replyText'] fanpai_id = extInfo['replyName'] if fanpai_id: message = ('【翻牌】[%s]-%s: %s\n【被翻牌】%s: %s\n' % ( msg_time, user_name, member_msg, fanpai_id, fanpai_msg)) save_msg_to_db(cursor, 101, msg_id, user_id, user_name, msg_time, member_msg, fanpai_msg, fanpai_id) else: message = ('【翻牌】[%s]-%s: %s\n【被翻牌】%s\n' % ( msg_time, user_name, member_msg, fanpai_msg)) save_msg_to_db(cursor, 101, msg_id, user_id, user_name, msg_time, member_msg, fanpai_msg) print(message) elif text_message_type == TextMessageType.LIVEPUSH: # 露脸直播 print('露脸直播') live_title = extInfo['liveTitle'] live_id = extInfo['liveId'] print(live_title + ' ' + live_id) save_msg_to_db(cursor, 102, msg_id, user_id, user_name, msg_time, live_title) elif text_message_type == TextMessageType.VOTE: # 投票 logger.debug('投票消息') vote_content = extInfo['text'] message = '【发起投票】{}: {}\n'.format(user_name, vote_content) print(message) save_msg_to_db(cursor, 104, msg_id, user_id, user_name, msg_time, vote_content) elif text_message_type == TextMessageType.FLIPCARD: print('付费翻牌功能') content = extInfo['question'] question_id = extInfo['questionId'] answer_id = extInfo['answerId'] source = extInfo['sourceId'] answer = extInfo['answer'] fan_name = get_idol_flip_name(answer_id, question_id) if fan_name: print('付费翻牌id: {}'.format(fan_name)) flip_message = ('【问】%s: %s\n【答】%s: %s\n翻牌时间: %s\n' % ( fan_name, content, user_name, answer, msg_time)) save_msg_to_db(cursor, 105, msg_id, user_id, user_name, msg_time, answer, content, fan_name) else: flip_message = ('【问】%s\n【答】%s: %s\n翻牌时间: %s\n' % ( content, user_name, answer, msg_time)) save_msg_to_db(cursor, 105, msg_id, user_id, user_name, msg_time, answer, content) message = flip_message print(message) elif text_message_type == TextMessageType.PASSWORD_REDPACKAGE: print('红包消息') content = '【红包】{}'.format(extInfo['redPackageTitle']) print(content) save_msg_to_db(cursor, 106, msg_id, user_id, user_name, msg_time, content) elif msg['msgType'] == MessageType.IMAGE: # 图片消息 print('图片') bodys = json.loads(msg['bodys']) if 'url' in bodys.keys(): url = bodys['url'] message = ('【图片】[%s]-%s: %s\n' % (msg_time, user_name, url)) print(message) save_msg_to_db(cursor, 200, msg_id, user_id, user_name, msg_time, url) elif msg['msgType'] == MessageType.AUDIO: # 语音消息 print('语音消息') bodys = json.loads(msg['bodys']) if 'url' in bodys.keys(): url = bodys['url'] message = ('【语音】[%s]-%s: %s\n' % (msg_time, user_name, url)) print(message) save_msg_to_db(cursor, 201, msg_id, user_id, user_name, msg_time, url) elif msg['msgType'] == MessageType.VIDEO: # 小视频 print('房间小视频') bodys = json.loads(msg['bodys']) if 'url' in bodys.keys(): url = bodys['url'] message = ('【小视频】[%s]-%s: %s\n' % (msg_time, user_name, url)) print(message) save_msg_to_db(cursor, 202, msg_id, user_id, user_name, msg_time, url) elif msg['msgType'] == MessageType.EXPRESS: # 大表情 logger.debug('大表情') emotion_name = extInfo['emotionName'] print(emotion_name) save_msg_to_db(cursor, 203, msg_id, user_id, user_name, msg_time, emotion_name) msg_time = msg['msgTime'] except Exception as e: print(e) finally: conn.commit() cursor.close() return msg_time
def parse_room_comment(self, response, task): """ 对房间评论进行处理 :param response: :param task: :return: """ rsp_json = json.loads(response) msgs = rsp_json['content']['message'] logger.debug('parse room comment reponse: %s', response) message = '' for msg in msgs: extInfo = json.loads(msg['extInfo']) msg_id = msg['msgidClient'] total_msg_type = msg['msgType'] msg_type = extInfo['messageType'] msg_time = util.convert_timestamp_to_timestr(msg['msgTime']) if msg_id in task.member_room_comment_ids: continue logger.debug('该评论{}不在队列中,需要打印'.format(msg_id)) task.member_room_comment_ids.append(msg_id) user_id = extInfo['user']['nickName'] if total_msg_type == 'TEXT': if msg_type == 'TEXT': logger.debug('房间评论') logger.debug('【房间评论】[%s]-%s: %s\n' % (msg_time, user_id, extInfo['text'])) elif msg_type == 'PRESENT_TEXT': gift_num = extInfo['giftInfo']['giftNum'] gift_name = extInfo['giftInfo']['giftName'] if extInfo['giftInfo']['isVote']: logger.debug('投票') message = '感谢{}送出的{}票,爱你呦~\n【{}】'.format( user_id, gift_num, msg_time) logger.debug(message) if message and len(task.member_room_msg_groups) > 0: pass # QQHandler.send_to_groups(task.member_room_msg_groups, message) else: logger.debug('礼物') logger.debug('感谢{}送出的{}个{},爱你呦~'.format( user_id, gift_num, gift_name)) else: logger.debug('其他类型评论') # logger.info('message: %s', message) # logger.debug('length of comment groups: %d', len(task.member_room_comment_msg_groups)) logger.debug('房间评论队列: %s', len(task.member_room_comment_ids))
def parse_room_msg(self, response, task): """ 对成员消息进行处理 :param response: :param task: :return: """ logger.debug('parse room msg response: %s', response) rsp_json = json.loads(response) msgs = rsp_json['content']['message'] cursor = self.conn.cursor() message = '' try: for msg in msgs: extInfo = json.loads(msg['extInfo']) msg_id = msg['msgidClient'] # 消息id if msg_id in task.member_room_msg_ids: continue logger.debug('session role: {}'.format(extInfo['sessionRole'])) if int(extInfo['sessionRole']) != 2: # 其他成员的消息 task.unread_other_member_msg_amount += 1 member_name = extInfo['user']['nickName'] if member_name == '19岁了还是小可爱': member_name = 'YBY' if member_name not in task.other_members_names: task.other_members_names.append(member_name) else: task.unread_msg_amount += 1 logger.debug('成员消息') task.member_room_msg_ids.append(msg_id) msg_time = util.convert_timestamp_to_timestr(msg["msgTime"]) user_id = extInfo['user']['userId'] user_name = extInfo['user']['nickName'] logger.debug('extInfo.keys():' + ','.join(extInfo.keys())) if msg['msgType'] == MessageType.TEXT: # 文字消息 text_message_type = extInfo['messageType'].strip() if text_message_type == TextMessageType.TEXT: # 普通消息 logger.debug('普通消息') message = ( '【成员消息】[%s]-%s: %s\n' % (msg_time, user_name, extInfo['text'])) + message self.save_msg_to_db(100, msg_id, user_id, user_name, msg_time, extInfo['text']) elif text_message_type == TextMessageType.REPLY: # 翻牌消息 logger.debug('翻牌') member_msg = extInfo['text'] fanpai_msg = extInfo['replyText'] fanpai_id = extInfo['replyName'] if fanpai_id: message = ('【翻牌】[%s]-%s: %s\n【被翻牌】%s: %s\n' % (msg_time, user_name, member_msg, fanpai_id, fanpai_msg)) + message self.save_msg_to_db(101, msg_id, user_id, user_name, msg_time, member_msg, fanpai_msg, fanpai_id) else: message = ('【翻牌】[%s]-%s: %s\n【被翻牌】%s\n' % (msg_time, user_name, member_msg, fanpai_msg)) + message self.save_msg_to_db(101, msg_id, user_id, user_name, msg_time, member_msg, fanpai_msg) elif text_message_type == TextMessageType.LIVEPUSH: # 直播 logger.debug('直播') live_title = extInfo['liveTitle'] live_id = extInfo['liveId'] playStreamPath, playDetail = self.get_live_detail( live_id) self.save_msg_to_db(102, msg_id, user_id, user_name, msg_time, live_title) live_message = '你们的崽崽开直播了\n直播标题: {}\n开始时间: {}'.format( live_title, msg_time) # QQHandler.send_to_groups(task.member_room_msg_groups, live_message) elif text_message_type == TextMessageType.VOTE: # 投票 logger.debug('投票消息') vote_content = extInfo['text'] message = '【发起投票】{}: {}\n'.format( user_name, vote_content) + message self.save_msg_to_db(104, msg_id, user_id, user_name, msg_time, vote_content) elif text_message_type == TextMessageType.FLIPCARD: logger.debug('付费翻牌功能') content = extInfo['question'] question_id = extInfo['questionId'] answer_id = extInfo['answerId'] source = extInfo['sourceId'] answer = extInfo['answer'] fan_name = self.get_idol_flip_name( answer_id, question_id) if fan_name: flip_message = ( '【问】%s: %s\n【答】%s: %s\n翻牌时间: %s\n' % (fan_name, content, user_name, answer, msg_time)) self.save_msg_to_db(105, msg_id, user_id, user_name, msg_time, answer, content, fan_name) else: flip_message = ( '【问】%s\n【答】%s: %s\n翻牌时间: %s\n' % (content, user_name, answer, msg_time)) self.save_msg_to_db(105, msg_id, user_id, user_name, msg_time, answer, content) message = flip_message + message elif text_message_type == TextMessageType.PASSWORD_REDPACKAGE: print('红包消息') content = '【红包】{}'.format(extInfo['redPackageTitle']) self.save_msg_to_db(106, msg_id, user_id, user_name, msg_time, content) elif msg['msgType'] == MessageType.IMAGE: # 图片消息 logger.debug('图片') bodys = json.loads(msg['bodys']) if 'url' in bodys.keys(): url = bodys['url'] if global_config.USING_COOLQ_PRO is True: message = ('【图片】[%s]-%s: [CQ:image,file=%s]\n' % (msg_time, user_name, url)) + message else: message = ('【图片】[%s]-%s: %s\n' % (msg_time, user_name, url)) + message self.save_msg_to_db(200, msg_id, user_id, user_name, msg_time, url) elif msg['msgType'] == MessageType.AUDIO: # 语音消息 logger.debug('语音消息') bodys = json.loads(msg['bodys']) if 'url' in bodys.keys(): url = bodys['url'] if global_config.USING_COOLQ_PRO is True: message3 = ('【语音】[%s]-%s: %s\n' % (msg_time, user_name, url)) logger.info(message3) # 语音消息直接单条发送 message2 = '[CQ:record,file={}]\n'.format(url) # QQHandler.send_to_groups(task.member_room_msg_groups, message2) else: message = ('【语音】[%s]-%s: %s\n' % (msg_time, user_name, url)) + message self.save_msg_to_db(201, msg_id, user_id, user_name, msg_time, url) elif msg['msgType'] == MessageType.VIDEO: # 小视频 logger.debug('房间小视频') bodys = json.loads(msg['bodys']) if 'url' in bodys.keys(): url = bodys['url'] message = ('【小视频】[%s]-%s: %s\n' % (msg_time, user_name, url)) + message self.save_msg_to_db(202, msg_id, user_id, user_name, msg_time, url) elif msg['msgType'] == MessageType.EXPRESS: # 大表情 logger.debug('大表情') emotion_name = extInfo['emotionName'] if global_config.USING_COOLQ_PRO is True: if 'tsj' in emotion_name: express_message = '[%s]-%s: [CQ:image,file=%s]' % ( msg_time, user_name, '{}.gif'.format(emotion_name)) else: express_message = '[%s]-%s: [CQ:image,file=%s]' % ( msg_time, user_name, '{}.png'.format(emotion_name)) message = express_message + message self.save_msg_to_db(203, msg_id, user_id, user_name, msg_time, emotion_name) if message and len(task.member_room_msg_groups) > 0: # express_message = '[CQ:image,file=%s]' % ( # 'express\\tsj000.gif') # express_message2 = '[CQ:image,file=%s]' % ( # 'express\\lt001.png') # QQHandler.send_to_groups(task.member_room_msg_groups, message) self.get_member_room_msg_lite(task) logger.info('message: %s', message) # QQHandler.send_to_groups(task.member_room_msg_groups, express_message) # QQHandler.send_to_groups(task.member_room_msg_groups, express_message2) logger.debug('成员{}消息队列: {}'.format(task.member.name, len(task.member_room_msg_ids))) except Exception as e: logger.exception(e) finally: self.conn.commit() cursor.close()
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分钟内没有未读消息')