def RowLine(): ''' 统计聊天条数走势 ''' chatrooms = getChat.GetChatrooms(typename=2) chatrooms_inuse = [] for chatroom in chatrooms: if toMySQL.GetRowNum(chatroom, "mysql") >= 5000: chatrooms_inuse.append(chatroom) id_time_dict = {} for i in range(len(chatrooms_inuse)): temp_arr = np.array(getChat.GetData(chatrooms_inuse[i], ["id", "CreateTime"], 2), dtype="int") id_time_dict[chatrooms_inuse[i]] = np.append( temp_arr[temp_arr[:, 0] % 20 == 1], [temp_arr[-1, :]], axis=0) f = plt.figure(figsize=(16, 9)) plt.grid(True) for key, value in id_time_dict.items(): dateframe_x = [datetime.fromtimestamp(i) for i in value[:, 1]] x = md.date2num(dateframe_x) y = value[:, 0] ax = plt.gca() xfmt = md.DateFormatter('%Y-%m-%d') ax.xaxis.set_major_formatter(xfmt) plt.plot(x, y, label=key) plt.legend(loc='upper left') f.savefig("../../output/RowLine.pdf", bbox_inches='tight')
def TimeSingle(chatroom, chartname="", filename="time_ana_single", Des=2): ''' chatroom:str,聊天对象 chartname:str,图表名 filename:str,文件名,存储在output文件夹下 Des:0:发出,1:接收,2:全部 ''' message_list = [] for row in getChat.GetData(chatroom=chatroom, columns=["id", "CreateTime"], Desname=Des): message_list.append(row) Normal(message_list, chartname=chartname, filename=filename)
def MostDay(filename="mostday_to", Des=0): ''' 发/收信息最多的一天 ''' chatrooms_group = getChat.GetChatrooms(typename=1) chatrooms_single = getChat.GetChatrooms(typename=2) chatrooms_all = chatrooms_group + chatrooms_single CreateTime_counter = {} for i in chatrooms_single: for j in getChat.GetData(i, ["CreateTime"], Desname=Des): time_array = time.localtime(j[0]) CreateTime = time.strftime("%Y-%m-%d", time_array) if CreateTime in CreateTime_counter: CreateTime_counter[CreateTime] += 1 else: CreateTime_counter[CreateTime] = 1 sorted_list = sorted(CreateTime_counter.items(), key=operator.itemgetter(1), reverse=True) format_time1 = sorted_list[0][0] + ' 00:00:00' format_time2 = sorted_list[0][0] + ' 23:59:59' time1 = int(time.mktime(time.strptime(format_time1, "%Y-%m-%d %H:%M:%S"))) time2 = int(time.mktime(time.strptime(format_time2, "%Y-%m-%d %H:%M:%S"))) chat_with = {} my_message = [] with sqlInit.MysqlInit() as mysql_cur: for i in chatrooms_all: temp_list = [] sql = "select Message,Des,Type from " + i + " where CreateTime>=" + str( time1) + " and CreateTime<=" + str(time2) mysql_cur.execute(sql) result = mysql_cur.fetchall() for row in result: if i != "Chat_b7ebbe67d8f64c77cda5415f4d749cc6" and row[ 1] == Des and row[2] == 1: temp_list.append(row[0]) if row[1] == Des and row[2] == 1: my_message.append(row[0]) if len(temp_list) > 0: chat_with[i] = temp_list with open("../../output/" + filename + ".txt", "w+", encoding="utf-8") as f: for key, value in chat_with.items(): for i in value: f.write(key + "," + i + "\n")
def WordcloudAll(filename="wc_all", maxwords=50, Des=2, typename=0, title=""): ''' filename:str,文件名,存储在output文件夹下 maxwords:int,最大词云量 typename:int,0:全部,1:群组,2:个人,3:公众号 Des:0:发出,1:接收,2:全部 ''' chatrooms = getChat.GetChatrooms(typename=typename) message_list = [] for chatroom in chatrooms: for row in getChat.GetData(chatroom=chatroom, columns=["Message", "Type"], Desname=Des): if row[1] == 1: message_list.append(row[0]) Normal(message_list, filename=filename, maxwords=maxwords, title="")
def TimeAll(chartname="", filename="time_ana_all", typename=0, Des=2): ''' chartname:str,图表名 filename:str,文件名,存储在output文件夹下 typename:int,0:全部,1:群组,2:个人,3:公众号 Des:0:发出,1:接收,2:全部 ''' chatrooms = getChat.GetChatrooms(typename) message_list = [] for chatroom in chatrooms: for row in getChat.GetData(chatroom=chatroom, columns=["id", "CreateTime"], Desname=Des): message_list.append(row) Normal(message_list, chartname=chartname, filename=filename)
def GeoAll(chartname="", filename="geo_ana_all", typename=0, Des=2): ''' chartname:str,图表名 filename:str,文件名,存储在output文件夹下 typename:int,0:全部,1:群组,2:个人,3:公众号 Des:0:发出,1:接收,2:全部 ''' message_list = [] counter0 = 0 chatrooms = getChat.GetChatrooms(typename=1) chatrooms.extend(getChat.GetChatrooms(typename=2)) for chatroom in chatrooms: for row in getChat.GetData(chatroom=chatroom, columns=["Message", "Type"], Desname=Des): if row[1] == 1: message_list.append(row[0]) counter0 += 1 print("数据量:", counter0) joined_message = ",".join(message_list) cutted_message = [] for i in jieba.lcut(joined_message): if len(i) > 1: cutted_message.append(i) name_id_dict = {} sql = "select id,name from Geodata" with sqlInit.MysqlInit() as mysql_cur: mysql_cur.execute(sql) result = mysql_cur.fetchall() for row in result: name_id_dict[row[1]] = row[0] word_counter_dict = {} counter1 = 0 for word in cutted_message: if word in name_id_dict.keys(): counter1 += 1 if not word in word_counter_dict: word_counter_dict[word] = 1 else: word_counter_dict[word] += 1 sorted_list = sorted(word_counter_dict.items(), reverse=True, key=operator.itemgetter(1)) return sorted_list
def UsageSingle(chatroom,chartname="",filename="usage_ana_single",Des=2): ''' chatroom:str,聊天对象 chartname:str,图表名 filename:str,文件名,存储在output文件夹下 Des:0:发出,1:接收,2:全部 ''' CreateTime_counter = {} for i in getChat.GetData(chatroom=chatroom,columns=["CreateTime"],Desname=Des): time_array = time.localtime(i[0]) CreateTime = time.strftime("%Y-%m-%d", time_array) if CreateTime in CreateTime_counter: CreateTime_counter[CreateTime] += 1 else: CreateTime_counter[CreateTime] = 1 sorted_list = sorted(CreateTime_counter.items(), key=operator.itemgetter(0),reverse=False) Normal(sorted_list,chartname=chartname,filename=filename)
def MostGroup(): ''' 发信息最多的群聊 ''' chatrooms_group = getChat.GetChatrooms(typename=1) group_row_dict = {} for i in chatrooms_group: group_row_dict[i] = toMySQL.GetRowNum(i, db="mysql", Des=0) sorted_list = sorted(group_row_dict.items(), key=operator.itemgetter(1), reverse=True) print(sorted_list[0]) message_list = [] for row in getChat.GetData(sorted_list[0][0], ["Message", "Type"], Desname=0): if row[1] == 1: message_list.append(row[0]) wordcloudAnalyse.Normal(message_list, filename="WC_to_MostGroup", maxwords=50, title="")
def WordCloudSingle(chatroom, filename="wc_single", maxwords=200, Des=2, from_user="", title=""): ''' filename:str,文件名,存储在output文件夹下 maxwords:int,最大词云量 Des:0:发送,1:接收,2:全部 ''' message_list = [] for row in getChat.GetData(chatroom=chatroom, columns=["Message", "Type", "SentFrom"], Desname=Des): if row[1] == 1: if from_user == "": counter2 += 1 message_list.append(row[0]) else: if row[2] == from_user: counter2 += 1 message_list.append(row[0]) Normal(message_list, filename=filename, maxwords=maxwords, title=title)
def UsageAll(chartname="",filename="usage_ana_all",typename=0,Des=0): ''' chartname:str,图表名 filename:str,文件名,存储在output文件夹下 typename:int,0:全部,1:群组,2:个人,3:公众号 Des:0:发出,1:接收,2:全部 ''' if typename==0: chatrooms_group = getChat.GetChatrooms(typename=1) chatrooms_single = getChat.GetChatrooms(typename=2) chatrooms = chatrooms_group + chatrooms_single else: chatrooms = getChat.GetChatrooms(typename=typename) CreateTime_counter = {} for i in chatrooms: for j in getChat.GetData(i,["CreateTime"],Desname=Des): time_array = time.localtime(j[0]) CreateTime = time.strftime("%Y-%m-%d", time_array) if CreateTime in CreateTime_counter: CreateTime_counter[CreateTime] += 1 else: CreateTime_counter[CreateTime] = 1 sorted_list = sorted(CreateTime_counter.items(), key=operator.itemgetter(0),reverse=False) Normal(sorted_list,chartname=chartname,filename=filename)
def LateChat(): ''' 返回深夜的聊天内容 ''' # 发出 chatrooms_single = getChat.GetChatrooms(typename=2) my_message = [] with open("../../output/latechat.txt", "w+", encoding="utf-8") as f: for i in chatrooms_single: for j in getChat.GetData(i, ["CreateTime", "Message", "Des", "Type"], Desname=2): time_array = time.localtime(j[0]) if 1 <= time_array[3] <= 6: CreateTime = time.strftime("%Y-%m-%d %H:%M:%S", time_array) Message = j[1] if j[2] == 0 and j[3] == 1: my_message.append(Message) f.write(i + "," + str(j[2]) + "," + CreateTime + "," + Message + "\n") wordcloudAnalyse.Normal(my_message, filename="WC_to_LateChat", maxwords=30, title="")
def Lonelydude(filename="lonelydude",typename=2): ''' 用于获取发出但没有收到回复的消息和收到但没有回复对方的消息 filename:str,文件名,存储在output文件夹下 typename:int,0:全部,1:群组,2:个人,3:公众号 ''' if typename==0: chatrooms_group = getChat.GetChatrooms(typename=1) chatrooms_single = getChat.GetChatrooms(typename=2) chatrooms = chatrooms_group + chatrooms_single else: chatrooms = getChat.GetChatrooms(typename=typename) CreateTime_counter_to = {} CreateTime_counter_from = {} for i in chatrooms: for j in getChat.GetData(i,["CreateTime"],Desname=0): time_array = time.localtime(j[0]) CreateTime = time.strftime("%Y-%m-%d", time_array) if CreateTime in CreateTime_counter_to: CreateTime_counter_to[CreateTime] += 1 else: CreateTime_counter_to[CreateTime] = 1 for k in getChat.GetData(i,["CreateTime"],Desname=1): time_array = time.localtime(k[0]) CreateTime = time.strftime("%Y-%m-%d", time_array) if CreateTime in CreateTime_counter_from: CreateTime_counter_from[CreateTime] += 1 else: CreateTime_counter_from[CreateTime] = 1 no_response = [] no_reply = [] no_response = [i for i in CreateTime_counter_to.keys() if i not in CreateTime_counter_from.keys()] no_reply = [i for i in CreateTime_counter_from.keys() if i not in CreateTime_counter_to.keys()] print(no_response) print(no_reply) no_response_with = {} for i in no_response: format_time1 = i+' 00:00:00' format_time2 = i+' 23:59:59' time1 = int(time.mktime(time.strptime(format_time1, "%Y-%m-%d %H:%M:%S"))) time2 = int(time.mktime(time.strptime(format_time2, "%Y-%m-%d %H:%M:%S"))) with sqlInit.MysqlInit() as mysql_cur: for j in chatrooms: temp_list = [] sql = "select Message from "+j+" where CreateTime>="+str(time1)+" and CreateTime<="+str(time2)+" and Des=0" mysql_cur.execute(sql) result = mysql_cur.fetchall() for row in result: temp_list.append(row[0]) if len(temp_list)>0: no_response_with[j] = temp_list no_reply_with = {} for i in no_reply: format_time1 = i+' 00:00:00' format_time2 = i+' 23:59:59' time1 = int(time.mktime(time.strptime(format_time1, "%Y-%m-%d %H:%M:%S"))) time2 = int(time.mktime(time.strptime(format_time2, "%Y-%m-%d %H:%M:%S"))) with sqlInit.MysqlInit() as mysql_cur: for j in chatrooms: temp_list = [] sql = "select Message from "+j+" where CreateTime>="+str(time1)+" and CreateTime<="+str(time2)+" and Des=1" mysql_cur.execute(sql) result = mysql_cur.fetchall() for row in result: temp_list.append(row[0]) if len(temp_list)>0: no_reply_with[j] = temp_list with open("../../output/"+filename+".txt","w+",encoding="utf-8") as f: f.write("未获得回复:\n") for key,value in no_response_with.items(): for i in value: f.write(key+","+i+"\n") f.write("\n未回复对方:\n") for key,value in no_reply_with.items(): for i in value: f.write(key+","+i+"\n")