def record_bilibili_data(): global statistic_handler try: my_logger.info('记录b站数据') statistic_handler.get_bilibili_stat() except Exception as e: my_logger.exception(e)
def record_pocket_room_data(): global statistic_handler try: my_logger.info('记录口袋房间数据') statistic_handler.pocket_msgs() except Exception as e: my_logger.exception(e)
def record_data(): """ 记录数据 :return: """ global statistic_handler try: my_logger.debug(global_config.POCKET48_LISTEN_TASKS) for task in global_config.POCKET48_LISTEN_TASKS: my_logger.debug('member name: %s', task.member.name) my_logger.debug('记录群人数数据') statistic_handler.update_group_size(task.member.pinyin) my_logger.debug('记录超话数据') statistic_handler.get_super_tag_size(task.member.pinyin) except Exception as exp: my_logger.exception(exp)
def pocket_msgs(self): from pocket48 import others cursor = self.conn.cursor() today = datetime.date.today().strftime('%Y-%m-%d %H:%M:%S') today_timestamp = int(util.convert_timestr_to_timestamp(today)) * 1000 yesterday_timestamp = today_timestamp - 60 * 24 * 60 * 1000 room_list = others.get_room_list() try: for room in room_list: others.member_messages = { 'id': room['id'], 'name': room['name'], 'room_id': room['room_id'], '100': 0, '101': 0, '102': 0, '103': 0, '104': 0, '105': 0, '106': 0, '200': 0, '201': 0, '202': 0, '203': 0 } my_logger.debug(room['name']) if room['id'] in [63, 327683, 327682, 5973]: continue others.get_room_history_msg(room['id'], room['room_id'], today_timestamp - 1, yesterday_timestamp) my_logger.debug(others.member_messages) cur_date = util.convert_timestamp_to_timestr(time.time() * 1000) sum = others.member_messages['100'] + others.member_messages[ '101'] + others.member_messages[ '102'] + others.member_messages[ '103'] + others.member_messages[ '104'] + others.member_messages[ '105'] + others.member_messages[ '106'] + others.member_messages[ '201'] + others.member_messages[ '202'] + others.member_messages[ '203'] cursor.execute( """ INSERT INTO `pocket_message` (`member_id`, `room_id`, `member_name`, `text`, `reply`, `live`, `vote`, `idol_flip`, `red_packet`, `pic`, `voice`, `video`, `total_num`, `update_time`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, (others.member_messages['id'], others.member_messages['room_id'], others.member_messages['name'], others.member_messages['100'], others.member_messages['101'], others.member_messages['102'], others.member_messages['104'], others.member_messages['105'], others.member_messages['106'], others.member_messages['200'], others.member_messages['201'], others.member_messages['202'], sum, cur_date)) self.conn.commit() except Exception as e: my_logger.exception(e) finally: cursor.close()
def get_bilibili_stat(self): """ 获取b站数据 :return: """ cursor = self.conn.cursor() try: bilibili_path = os.path.join(os.path.dirname(BASE_DIR), 'data', 'bilibili.json') member_json = json.load(open(bilibili_path, encoding='utf8'))['stats'] for member in member_json: bilibili_id = member['bid'] member_name = member['name'] my_logger.info(member_name) url1 = 'https://api.bilibili.com/x/relation/stat?vmid={}&jsonp=jsonp'.format( bilibili_id) header = { 'Referer': 'https://space.bilibili.com/{}'.format(bilibili_id), 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36' } rsp = self.session.get(url1, headers=header).json() fan_number = rsp['data']['follower'] try: url2 = 'https://api.bilibili.com/x/space/upstat?mid={}&jsonp=jsonp'.format( bilibili_id) rsp_json = self.session.get( url2, headers=header, cookies={ 'SESSDATA': 'db49e46a%2C1614611003%2C81dfe*91' }).json() view = rsp_json['data']['archive']['view'] print(rsp_json) except Exception as e: my_logger.exception(e) view = 0 url3 = 'https://api.bilibili.com/x/space/acc/info?mid={}&jsonp=jsonp'.format( bilibili_id) rsp = self.session.get(url3, headers=header).json() bilibili_name = rsp['data']['name'] cur_date = util.convert_timestamp_to_timestr(time.time() * 1000) cursor.execute( """ INSERT INTO `bilibili` (`member_name`, `bilibili_id`, `bilibili_name`, `fans_num`, `video_view`, `update_time`) VALUES (?, ?, ?, ?, ?, ?) """, (member_name, bilibili_id, bilibili_name, fan_number, view, cur_date)) self.conn.commit() time.sleep(2) except Exception as e: my_logger.exception(e) finally: cursor.close()