# -*- coding : utf-8 -*- import json import tqdm import base64 import pymysql import requests from Crypto.Cipher import AES from openpyxl import Workbook, load_workbook def decrypt_cbc(content, encodingAESKey): key = bytearray(encodingAESKey.encode('utf-8')) iv = key[:16] cipher = AES.new(key, AES.MODE_CBC, iv) tmp = base64.b64decode(content) data = cipher.decrypt(tmp).decode('utf-8') return data def get_human_name(robot_id, headers): # url = 'https://api.aquanliang.com/gateway/qopen/GetHostAccountList' url = 'https://api.aquanliang.com/gateway/qopen/GetRobotAccountList' human_dict = dict() for i in range(10): data = { "robot_id": robot_id, "offset": i*100, "limit": 100 } response = requests.post(url, data=json.dumps(data), headers=headers) try: datas = json.loads(response.text)['data']['account_list'] for da in datas: if da['account_id'] not in human_dict: human_dict[da['account_id']] = da['profile']['name'] except Exception: break return human_dict def get_robot_name(): url = 'https://api.aquanliang.com/gateway/qopen/GetHostAccountList' headers = { 'Content-Type': 'application/json;charset=UTF-8', 'Token': 'd4lnuHs7CUBP65ZaektrUBX4R2g_Nbsuj6cmkUhWUG-iipp_2yiUFmFeiDjLa7-LGY4AHcd5I4L_NWSgfUh2vb-KNp8Nax9rWG8Yr41-MLd8TA2', } headers = test_token(url, headers) robot_dict = dict() for i in range(5): data = { "offset": i*100, "limit": 100 } response = requests.post(url, data=json.dumps(data), headers=headers) print( response.text) datas = json.loads(response.text) if datas['data']: datas = json.loads(response.text)['data']['host_account_list'] for da in tqdm.tqdm(datas): if da['robot_id'] not in robot_dict: human_dict = get_human_name(da['robot_id'], headers) robot_dict[da['robot_id']] = [da['name'], human_dict] # print( len(robot_dict) ) return robot_dict def create_dialogue(min_date, max_date): connection = pymysql.connect(host="47.92.193.147", port=3306, user="root", passwd="Moxi123#", db="task_dialogue_config", charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) cursor = connection.cursor() sql = 'select encoding_content, signature ' \ 'from quanliang_quality_test ' \ 'where update_time > "{} 02:00:00" ' \ 'and update_time < "{} 23:59:59"; ' cursor.execute(sql.format(min_date, max_date)) results = cursor.fetchall() i = 0 wb = Workbook() ws = wb.active ws.append(['好友名称', '添加日期', '对话轮次', '对话结束人', '发送者', '接受者', '内容', '聊天时间']) dialogue_dict = dict() robot_dict = get_robot_name() encodingAESKey = "f4809cd6510d492791bcf316686f272a" for res in tqdm.tqdm(results): sign = 0 content = res['encoding_content'] signature = res['signature'] data = decrypt_cbc(content=content, encodingAESKey=encodingAESKey) data = eval('}'.join(data.replace('true', 'True').replace('false', 'False').replace('null', 'None').split('}')[:-1]) + '}') if data['event_type'] == 40023 or data['event_type'] == 40030: sender_id = data['sender_id'] receiver_id = data['receiver_id'] msg = data['msg_content'] msg_time = data['msg_time'] if '_'.join([sender_id, receiver_id]) not in dialogue_dict: if '_'.join([receiver_id, sender_id]) not in dialogue_dict: key = '_'.join([sender_id, receiver_id]) dialogue_dict[key] = list() else: key = '_'.join([receiver_id, sender_id]) else: key = '_'.join([sender_id, receiver_id]) # print( data ) # print(key) # print( type(eval(data)) ) if sender_id in robot_dict: sign = 1 elif receiver_id in robot_dict: sign = 2 else: sign = 0 if sign == 1: dialogue_dict[key].append(['', '', '', '', robot_dict[sender_id][0], robot_dict[sender_id][1][receiver_id] if receiver_id in robot_dict[sender_id][1] else receiver_id, msg, msg_time]) elif sign == 2: dialogue_dict[key].append(['', '', '', '', robot_dict[receiver_id][1][sender_id] if sender_id in robot_dict[receiver_id][1] else sender_id, robot_dict[receiver_id][0], msg, msg_time]) else: dialogue_dict[key].append(['', '', '', '', sender_id, receiver_id, msg, msg_time]) # print( '私聊' ) for key in dialogue_dict: ws.append(['', '', '', dialogue_dict[key][-1][-4]]) for content in dialogue_dict[key]: ws.append(content) # print( dialogue_dict[key] ) wb.save('./樊登文本聊天.xlsx') def test_token(url, headers): data = { "offset": 0, "limit": 100 } response = requests.post(url, data=json.dumps(data), headers=headers) # print( response.text) datas = json.loads(response.text) if not datas['data'] and datas['errcode'] == 10001: token = get_token() headers['Token'] = token return headers def get_token(): url = 'https://api.aquanliang.com/gateway/qopen/GetAccessToken' headers = { 'Content-Type': 'application/json;charset=UTF-8' } data = { 'app_key': 'cof2abc7ceb19e4afa', 'app_secret': 'r2LSDqtxaeV31QP4uX85ISF0CNmY0LZDx8s29QpeKr6HuzjyAqc6', } response = requests.post(url, data=json.dumps(data), headers=headers) result = json.loads(response.text) return result['data']['data']['access_token'] if __name__ == '__main__': create_dialogue('2022-02-07', '2022-02-09') # get_robot_name() # get_token()